Hyper-V WMI – Configuring Automatic Startup/Shutdown/Recovery Action’s For Virtual Machines

Hyper-V virtual machines have configuration options that control what happens when the parent/host server shuts down, start’s up, or if a virtual machine’s worker process terminates unexpectedly.  The first two conditions (parent/host shutdown, startup) are pretty self explanatory but what would cause a worker process to terminate?  There are a couple reasons, one would be if someone terminates the process, another would be if the process detects an security attack coming from the virtual machine, others reasons could be if there was bad hardware or a programming error (i.e bug) in either Hyper-V or another component in the system…

The default Automatic Startup Action is to start the virtual machine if it was running when the server was shutdown.  Another option is to always startup the virtual machine irrespective of the state of the virtual machine when the server was shutdown. The last option is none – this translates to leave the virtual machine off or saved when the server starts up.

The default Automatic Shutdown Action is to save any running virtual machines.  There are two other options, first is to turn off any running virtual machines and second is to shutdown any running virtual machines using the shutdown integration component.  The shutdown is forced – so any open/running programs will be terminated.  If the shutdown request fails the vm will be turned off – this can happen for many reasons primarily because the IC’s aren’t installed or if the guest is in safe mode/booting up.

The default Automatic Recovery Action is to restart the virtual machine, the other options are to revert to the last snapshot if one exists or none – which again equates to leaving the virtual machine off.

There is one additional option that I wanted to discuss briefly is Automatic Startup Action Delay, this is the amount of time to wait after the server reboot before taking the automatic startup action.  The default is zero – meaning take the action immediately.  This option could be very helpful for some servers – if you have lower and higher priority virtual machines you might want to delay the startup of your low priority virtual machines.

So here’s some code:

$VirtualSystemManagemntService = get-wmiobject -class “Msvm_VirtualSystemManagementService” -namespace “root\virtualization”

foreach ($Vm in Get-WmiObject -Namespace root\virtualization -Query “Select * from Msvm_ComputerSystem Where Description=’Microsoft Virtual Machine'”)
$SettingData = Get-WmiObject -Namespace root\virtualization -Query “Associators of {$Vm} Where ResultClass=Msvm_VirtualSystemGlobalSettingData AssocClass=Msvm_ElementSettingData”
$SettingData.AutomaticStartupAction = 0 #None=0, Restart if Previously Running=1, Always Startup=2
$SettingData.AutomaticShutdownAction = 0 #Turn Off=0, Save State=1, ShutDown=2
$SettingData.AutomaticRecoveryAction = 0 #None=0, Restart=1, Revert to Snapshot=2
$SettingData.AutomaticStartupActionDelay = [System.Management.ManagementDateTimeconverter]::ToDmtfTimeInterval((New-TimeSpan -Minutes 10))

$VirtualSystemManagemntService.ModifyVirtualSystem($Vm, $SettingData.PSBase.GetText(1))


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s