UCSD Modify SCSI Controller in vCenter for a VM

Version 3
    Task Name

    Change the SCSI controller on a VM

    Description

     

    Prerequisites
    1. Tested on 5.4
    CategoryWorkflow
    ComponentsvSphere 5.x
    User Inputs

    VM ID

    vCenter IP and ID and Password

    Output

    Instructions for Regular Workflow Use:

    1. Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
    2. Unzip the file on your computer. Should end up with a .WFD file.
    3. Log in to UCS Director as a user that has "system-admin" privileges.
    4. Navigate to "Policies-->Orchestration" and click on "Import".
    5. Click "Browse" and navigate to the location on your computer where the .WFDX file resides. Choose the .WFDX file and click "Open".
    6. Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
    7. Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
    8. Click "Submit".
    9. A new folder should appear in "Policies-->Orchestration" that contains the imported workflow. You will now need to update the included tasks with information about the specific environment

     

    Thank you for creating this a big Thank you goes out to:  Tejeswar Sahu

     

     

    Workflow Execution:

     

    Unknown.png

     

    The Code:

     

    importPackage(java.net);

    importPackage(java.lang);

    importPackage(com.vmware.vim25);

    importPackage(com.vmware.vim25.mo);

    function modifySCSIController() {  

      try{

      var si = new ServiceInstance(new URL("https://"+input.ipaddress+"/sdk"), input.username, input.password,true);

      logger.addInfo("input.ipaddress =" +input.ipaddress +", username ="+input.username+" , password ="+input.password)

      var rootFolder = si.getRootFolder();

      var vmid=input.vmname;

          logger.addInfo("VMID" + vmid);

         var api=ctxt.getAPI();

         var gvm=api.getVMBasicInfo(vmid);

         var vmwarename=gvm.getInstanceId();

      var vm = new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmwarename);

      if(vm==null)

      {

      logger.addError("No VM " + vmwarename + " found");

      throw new Exception("NO VM found with specified name");

      }

      logger.addInfo(" VM " + vmwarename + " found");

      var state = vm.getRuntime().powerState;

      logger.addInfo("vm Power state "+state.toString());

     

     

      var scsiSpec;

      var vmConfigSpec = new VirtualMachineConfigSpec();

     

      var scsi = findSCSIDevice(vm.getConfig());

      logger.addInfo("SCSI name is:"+scsi.getDeviceInfo().getSummary())

      if(scsi != null){

     

      if (state.toString().equals("poweredOn")){

      var powerofftask = vm.powerOffVM_Task();

      if(powerofftask.waitForMe()==Task.SUCCESS){

      logger.addInfo(vm + " powered off");

      }

      }

     

      logger.addInfo("Changing SCSI Controller Type:");

      scsiSpec = [modifySCSIConfigSpec(vm,  scsi)];

      logger.addInfo("Array length is:"+scsiSpec.length);

      }else{

      throw new Exception("No SCSI Controller found...");

      }

      logger.addInfo("***Config Spec: "+scsiSpec);

      vmConfigSpec.setDeviceChange(scsiSpec);

      var task = vm.reconfigVM_Task(vmConfigSpec);

      var status =task.waitForTask();

      logger.addInfo("reconfig VM task status : "+ status);

      if(status == Task.SUCCESS) {

      if (state.toString().equals("poweredOff")){

      task = vm.powerOnVM_Task(null);

      if(task.waitForMe()==Task.SUCCESS)

      {

      logger.addInfo(input.vmname + " powered on");

      }

      }

      }else{

      var info = task.getTaskInfo();

      logger.addError(info.getError().getFault());

      throw new Exception("Invalid configuration specification");

      }

      logger.addInfo("Task successfull to change SCSI Controller type");

      si.getServerConnection().logout();

      }catch(e){

      logger.addError("Error: "+e);

      si.getServerConnection().logout();

      ctxt.setFailed("Task failed to change SCSI Controller type");

      ctxt.exit();

      }

    }

     

     

    function modifySCSIConfigSpec(vm,  scsiCtrl)

      {

      logger.addInfo("*********Modifying virtual device*******");

       var scsiSpec = new VirtualDeviceConfigSpec();

        scsiSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);

        var scsiCtrl =  new VirtualLsiLogicSASController();

        scsiCtrl.setKey(1000);

        scsiCtrl.setBusNumber(0);

        scsiCtrl.setSharedBus(VirtualSCSISharing.noSharing);

        scsiCtrl.setUnitNumber(3);

        scsiSpec.setDevice(scsiCtrl);

        return scsiSpec;

     

     

      }

     

     

    function findSCSIDevice(vmConfig)

    {

      var scsiController = null;

        var devices = vmConfig.getHardware().getDevice();

        for(var i=0;i<devices.length;i++)

        {

      if(devices[i] instanceof VirtualSCSIController){

              logger.addInfo("scsi found at index:"+i);

      scsiController = devices[i];

      break;

      }

        }

        return scsiController;

    }

     

     

    modifySCSIController()