I recently discovered a blog article from Vitali Baruh on the PowerCLI QE team in regards to waiting for the guest OS customization to complete following a deployment of a virtual machine from a template.
http://blogs.vmware.com/PowerCLI/2012/08/waiting-for-os-customization-to-complete.html
As Vitali discusses this process can be difficult to determine if this has completed in the guest operating system and if the task had completed successfully.
Previously, following the power on of a deployed virtual machine from a template I have performed a Do…Until loop to process a condition until the ‘Guest.HostName’ value retrieved from the Get-View cmdlet matches the name of the virtual machine as below:
Do { $GuestHostName = (Get-View $VM.Id).Guest.HostName Start-Sleep -Seconds 10 } Until ($GuestHostName -eq $VM.Name)
Whilst I have received no issues from this, there is an argument agaisnt using this method as we are not retrieving the guest OS customization task status. Now, this is where Vitali has produced an excellent blog post to describe the steps of this function which is based on a number of virtual machine events generated by the vCenter server.
The script workflow, works as follows:
1. For each VM we will look for the last VmStarting event, because the customization process starts after the VM has been powered on
1.1. If such an event is found, change the status to “CustomizationNotStarted” 1.2. If such an event is not found, change the status to “VmNotStarted” 2. Start a loop
2.1. For each VM with status “CustomizationNotStarted”
2.1.1. Check for posted CustomizationStartedEvent after the last power-on operation
2.1.1.1. If such an event is found, change the status to “CustomizationStarted” 2.2. For each VM with status “CustomizationStarted”
2.2.1. Check for Succeded or Failed Event
2.2.1.1. If such an event is found, change the status to the corresponding “CustomizationSucceeded” or“CustomizationFailed” values 2.3. Check is there more VMs with status “CustomizationNotStarted” or “CustomizationStarted” (we should continue to monitor these VMs)
2.3.1. If no, break the loop 2.4. Check whether the specified timeout has elapsed
2.4.1. If yes, break the loop 3. Return result
For those who are provisioning virtual machines to include a guest OS customization with PowerCLI, I would definitely recommend a read of this article.