Sonntag, 16. Juni 2013

vCenter Orchestrator: Mount ISO image into virtual CDrom

Hey there,
it's Sunday and nothing to do, so I decided to solve a task which was ignored for a long time.

The task is to mount an ISO image to a virtual pc, maybe to deploy virtual machines and configure them with prepared images of SCCM for example. I could imagine many use cases to automtically mount images to many machines.
Anyway, I created 3 scripts to find the CD drive, find the correct ISO file at your datastore and finally mount it.

Part 1: find the ISO and its path.

vCO expects a path like : "[datastore1] ISOs/Win8Alpha1.iso"

var ISOpaths = new Array(); //if you have more then one ISO file
var fileQuery = new Array(new VcIsoImageFileQuery());
var querySpec = new VcHostDatastoreBrowserSearchSpec();
querySpec.query = fileQuery;
var myISO = "Win8.iso"; //the ISO you are looking for
var myISOtemp = null; //temp parameter, it is just use to cut the string
var myISOpath = null; // your final path

var task = datastore.browser.searchDatastoreSubFolders_Task("[" + + "]", querySpec);
var searchResults = System.getModule("").vim3WaitTaskEnd(task,false,5);
for (var i in searchResults) {
for (var j in searchResults[i].file) {
ISOpaths.push(searchResults[i].folderPath + searchResults[i].file[j].path);
for (k in ISOpaths)
myISOtemp = ISOpaths[k].substring(ISOpaths[k].search("] ")+2,300)
myISOtemp = myISOtemp.substring("/")+1,300)
if(myISOtemp == "Win8.iso" )
myISOpath = ISOpaths[k];
System.log("found :" + myISOpath);

Ok that's the script to find the ISO file, you are free to modify this to your own needs. Maybe you have serveral datastores with the same ISO file or different vCenters.

Part 2: Get the CD drive

This is easy, so there are no comments or explanations needed.

var devices = vm.config.hardware.device;

for ( i in devices )  {
if ( devices[i] instanceof VcVirtualCdrom )  {
System.log("Key: " + devices[i].key);
System.log("Label: " + devices[i].deviceInfo.label);
System.log("controllerKey: " + devices[i].controllerKey);
System.log("unitNumber: " + devices[i].unitNumber);
System.log (devices[i]); // will return the scripting class
Every information you need for part 3 is in there.

Part 3: Mount the ISO image

var spec = new VcVirtualMachineConfigSpec();
var deviceChange = new Array (new VcVirtualDeviceConfigSpec());
var VirtualDeviceConfigSpec = new VcVirtualDeviceConfigSpec();
VirtualDeviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
var device = new VcVirtualCdrom();
device.key = 3002; //this could be an input parameter
deviceInfo = new VcDescription();
deviceInfo.label = "CD/DVD drive 1";
deviceInfo.summary = "ISO";
device.deviceInfo = new VcDescription();
device.deviceInfo = deviceInfo;
device.controllerKey = 201; //this could be an input parameter
device.unitNumber = 0; //this could be an input parameter
var CdromIsoBackingInfo = new VcVirtualCdromIsoBackingInfo();
CdromIsoBackingInfo.fileName = myISOpath;
var DeviceConnectInfo = new VcVirtualDeviceConnectInfo();
DeviceConnectInfo.startConnected = false;
DeviceConnectInfo.allowGuestControl = true;
DeviceConnectInfo.connected = true;
DeviceConnectInfo.status = "untried";

VirtualDeviceConfigSpec.device = new VcVirtualCdrom();
VirtualDeviceConfigSpec.device = device;

VirtualDeviceConfigSpec.device.backing = CdromIsoBackingInfo;
VirtualDeviceConfigSpec.device.connectable = DeviceConnectInfo;
deviceChange[0] = VirtualDeviceConfigSpec;
spec.deviceChange = deviceChange;
task = vm.reconfigVM_Task(spec);  //task is an output parameter if you want to start a waiting task
The only important thing you have to know is that when you want to add an attribute which is also an object you cannot do something like this: "device.deviceInfo.label" DeviceInfo has to defined before you add a values.

If you have questions, send me an email or post a commend.

Keine Kommentare:

Kommentar veröffentlichen