Determining When Guest OS Customisations Are Complete

When spinning up a few VM from a template in a vSphere environment, along with the usual guest OS customisations, it’s not always clear when the customisations are done, and, to a lesser extent, if they’ve been successful.

On numerous occasions I’ve been less than patient, I started logging into the VM and having it reboot on me as the customisation process was still in progress.

Thankfully, vSphere logs guest customisation events which we can query through PowerCLI.

The following snippet selects a VM, then displays the customisation log every 30 seconds. Once a success/fail message is logged, we know customisation is done.

# We first define the VM(s) we care about
$VM = Get-VM -Name @("vm1","vm2")

# Then we loop through the logs for those VMs to determine at which stage customisations are at
While ($True) {
    Get-Date
    $VM | Get-VIEvent | where FullFormattedMessage -match "Customization log" | select CreatedTime, FullFormattedMessage | Sort-Object -Property createdtime | ft -auto
    Start-Sleep -Seconds 30
}

This will give us the following output every 30 seconds

Sunday, 4 November 2018 6:41:36 PM

CreatedTime           FullFormattedMessage
-----------           --------------------
23/10/2018 6:42:40 PM Started customization of VM vm1. Customization log located at C:/Windows/TEMP/vmware-imc/guestcust.log in the guest OS.
23/10/2018 6:44:18 PM Started customization of VM vm2. Customization log located at C:/Windows/TEMP/vmware-imc/guestcust.log in the guest OS.

Once guest customisation is complete, we’ll see the following

Sunday, 4 November 2018 6:49:39 PM

CreatedTime           FullFormattedMessage
-----------           --------------------
23/10/2018 6:42:40 PM Started customization of VM vm1. Customization log located at C:/Windows/TEMP/vmware-imc/guestcust.log in the guest OS.
23/10/2018 6:44:18 PM Started customization of VM vm2. Customization log located at C:/Windows/TEMP/vmware-imc/guestcust.log in the guest OS.
23/10/2018 6:46:54 PM Customization of VM vm1 succeeded. Customization log located at C:/Windows/TEMP/vmware-imc/guestcust.log in the guest OS.
23/10/2018 6:48:42 PM Customization of VM vm2 succeeded. Customization log located at C:/Windows/TEMP/vmware-imc/guestcust.log in the guest OS.

There we have it. It would only take minor alterations to incorporate this into a bigger deployment script, where the VM is spun up via a script, which then waits for customisations to complete before going into further deployment, for example, using Invoke-VMScript to deploy and configure services inside the guest.