Effortlessly Shut Down Virtual Machines with PowerCLI

VMware PowerCLI allows us to shut down a guest operating system on a virtual machine (assuming VMTools are installed), but the command to do this – Shutdown-VMGuest – returns straight away. In a script, we may wish to wait for the VM to power down before continuing, which can be done as follows.

To shut down a guest operating system using PowerCLI, we can use the Shutdown-VMGuest command with the Force parameter set to true. This will initiate the shutdown process and wait for it to complete before returning. However, if we want to wait for the VM to power down before continuing with our script, we need to use the Wait-For-Multiple command instead.

The Wait-For-Multiple command takes a list of PowerCLI commands as input, and waits for all of them to complete before returning. We can use this command to wait for the Shutdown-VMGuest command to complete before continuing with our script. Here’s an example of how we can modify our script to wait for the VM to power down:

“`

$vmName = “MyVirtualMachine”

$force = $true

# Shut down the guest operating system

Shutdown-VMGuest -VM $vmName -Force $force

# Wait for the VM to power down before continuing

Wait-For-Multiple -Cmd “Get-VM $vmName” -Timeout 60

“`

In this example, we first set the name of the virtual machine we want to shut down ($vmName) and the force parameter ($force) to true. We then use the Shutdown-VMGuest command to initiate the shutdown process.

Next, we use the Wait-For-Multiple command to wait for the Get-VM command to complete before continuing with our script. The Get-VM command is used to check if the virtual machine has powered down, and the timeout parameter is set to 60 seconds. This means that the script will wait up to 60 seconds for the VM to power down before continuing.

If you want to wait for the VM to power down before continuing with your script, it’s important to note that the Wait-For-Multiple command can be a bit unpredictable. Sometimes, the command may return before the VM has had a chance to power down, which can cause issues with your script. To avoid this, you can use the Wait-For-Multiple command in combination with the Get-VM command to check if the VM has powered down before continuing.

Here’s an example of how you can modify your script to wait for the VM to power down using the Wait-For-Multiple and Get-VM commands:

“`

$vmName = “MyVirtualMachine”

$force = $true

# Shut down the guest operating system

Shutdown-VMGuest -VM $vmName -Force $force

# Wait for the VM to power down before continuing

Wait-For-Multiple -Cmd “Get-VM $vmName” -Timeout 60

# Check if the VM has powered down before continuing

if (Get-VM $vmName).PowerState -eq “off” {

# The VM has powered down, continue with your script

} else {

# The VM has not powered down yet, wait a bit longer and try again

Write-Host “Waiting for the VM to power down…”

sleep -Milliseconds 1000

Wait-For-Multiple -Cmd “Get-VM $vmName” -Timeout 60

}

“`

In this example, we first shut down the guest operating system using the Shutdown-VMGuest command. We then use the Wait-For-Multiple command to wait for the Get-VM command to complete before continuing with our script.

Next, we check if the VM has powered down using the Get-VM command. If the PowerState property of the VM is set to “off”, we know that the VM has powered down and we can continue with our script. If the PowerState property is not set to “off”, we know that the VM has not powered down yet, so we wait a bit longer and try again using the Wait-For-Multiple command.

Overall, using the Wait-For-Multiple command in combination with the Get-VM command can help you avoid issues with your script when shutting down a guest operating system using PowerCLI. By waiting for the VM to power down before continuing, you can ensure that your script runs smoothly and without any errors.