Customize a Virtual Machine
For John’s question in the VMware Communities about customizing a VM here:
http://communities.vmware.com/thread/311899
I took the challenge and created a small example how to customize a Virtual Machine using the CustomizeVM_Task().
The network settings, licensing, domain stuff and so on should come from a sysprep.inf text file.
Creating the proper CustomizationSpecification is really a hard job, especially since a couple of settings are mandatory even if the used information is already given via the SysprepText.
I got help using the API Reference, and the UML charts in Steve Jin’s indispensable book (http://www.amazon.com/VMware-VI-vSphere-SDK-Infrastructure/dp/0137153635 , I cannot mention it often enough… and no, that’s not an affiliate-link…yet… 😀 ), page 242.
When testing it, I got a lot of not helpful errors like “Cannot complete customization”. If you increase the loglevel in vCenter, in the vCenter logs there are a bit more information. Just don’t forget to reset it afterwards to avoid “logflooding”.
At the end I ended up with this code:
var sysprepMime = sysprepFile.getContentAsMimeAttachment(); var sysprepText = sysprepMime.content System.debug("sysprep: " + sysprepText); //create the custSpec objects var custSpec = new VcCustomizationSpec(); System.debug("custSpec: " + custSpec); var myVcCustomizationSysprepText = new VcCustomizationSysprepText() ; System.debug("myVcCustomizationSysprepText: " + myVcCustomizationSysprepText); myVcCustomizationSysprepText.value = sysprepText //from Resource element above custSpec.identity = myVcCustomizationSysprepText; System.debug("custSpec.identity: " + custSpec.identity); //fill the mandatory GlobalIPSettings var myVcCustomizationGlobalIPSettings = new VcCustomizationGlobalIPSettings() ; custSpec.globalIPSettings = myVcCustomizationGlobalIPSettings; System.debug("custSpec.globalIPSettings: " + custSpec.globalIPSettings); //fill in the NIC-settings-map for one nic with unknownIPGenerator (IP-spec is in sysprep-text): var mappingMap = new Array(); var myVcCustomizationAdapterMapping = new VcCustomizationAdapterMapping() ; var myVcCustomizationIPSettings = new VcCustomizationIPSettings() ; var myIpGenerator; //myIpGenerator = new VcCustomizationUnknownIpGenerator() ; //myIpGenerator = new VcCustomizationFixedIp(); //myIpGenerator.ipAddress = "1.2.3.4"; myIpGenerator = new VcCustomizationDhcpIpGenerator() ; myVcCustomizationIPSettings.ip = myIpGenerator; myVcCustomizationAdapterMapping.adapter = myVcCustomizationIPSettings; System.debug("myVcCustomizationIPSettings.ip:" + myVcCustomizationIPSettings.ip); System.debug("myVcCustomizationAdapterMapping.adapter: " + myVcCustomizationAdapterMapping.adapter); mappingMap.push(myVcCustomizationAdapterMapping); System.debug("mappingMap: " + mappingMap); //adding map to custSpec custSpec.nicSettingMap = mappingMap; vmToCustomize.customizeVM_Task(custSpec);
Download the full workflow example here: http://www.vcoportal.de/download/workflow/customizeVM.workflow