12-01-2015 04:07 AM - edited 03-01-2019 06:39 AM
Task Name | Change the SCSI controller on a VM |
Description | |
Prerequisites |
|
Category | Workflow |
Components | vSphere 5.x |
User Inputs | VM ID vCenter IP and ID and Password |
Output |
Instructions for Regular Workflow Use:
Thank you for creating this a big Thank you goes out to: Tejeswar Sahu
Workflow Execution:
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()
really thanks!
Thanks for the info.
Is the download gone? I am not seeing it.
No download, plz fix it
Good thing I keep a backup copy. Not sure how the code and the file disappeared.
Code and file are posted.
-
Orf Gelbrich
Check out UCS Director Workflow INDEX<https://communities.cisco.com/docs/DOC-56419> on Cisco Communities (External)
Thank you, it's really helpful script
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: