Remove CD Drive from VM using vRO workflow

 Input:- payload (type properties)

var machine = payload.get("machine");
var vmProperties = machine.get("properties");
vmname = machine.get("name");
lifecycleState = payload.get("lifecycleState");
System.log(vmname)
var debugProps = "";
VirtualMachineProperties = machine.get("properties");
if(VirtualMachineProperties != null){
var keys = VirtualMachineProperties.keys.sort();
    for each(var k in keys)
        debugProps += "   " + k + ": " + VirtualMachineProperties.get(k) + "\r\n";
}
else{
    debugProps = "null";
}
System.debug("virtualMachineProperties :\r\n" + debugProps);
var disclaimer = VirtualMachineProperties.get("PropertyName");
    if (disclaimer.toLowerCase() == "false"){
        throw "Please check the disclaimer which is mandatory"
    }
    var vms = VcPlugin.getAllVirtualMachines(null, vmname);
virtualMachine = vms[0];
var configSpec = new VcVirtualMachineConfigSpec();
var deviceConfigSpecs = new Array();
var deviceConfigSpec = new VcVirtualDeviceConfigSpec();
var devices = virtualMachine.config.hardware.device;
 var pollRate = "20";
var progress = "";
var task = virtualMachine.powerOffVM_Task();//var task = vm.powerOnVM_Task();//
    taskEnd = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,progress,pollRate);
    System.log("PowerOff task has been completed");
   
for ( var ii in devices) {
var device = devices[ii];
if ( device instanceof VcVirtualCdrom) {
deviceConfigSpec.device = device;
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;
deviceConfigSpecs.push (deviceConfigSpec);
configSpec.deviceChange = deviceConfigSpecs
}
}

virtualMachine.reconfigVM_Task(configSpec);

//var task = virtualMachine.powerOnVM_Task();//var task = vm.powerOnVM_Task();//
//    taskEnd = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,progress,pollRate);
//    System.log("PowerOn task has been completed");
   

System.sleep(3000);
Post a Comment (0)
Previous Post Next Post