In Hyper-V, you can configure automatic startup and shutdown options for your virtual machines when you boot or restart your host OS. In this article, we’ll show how to configure the actions a Hyper-V host should take on virtual machines when it is powered on or shut down gracefully, and how to set the boot order of the virtual machines.
Configure Automatic Startup and Shutdown Action for Hyper-V Virtual Machines
By default, a Hyper-V host saves the state of the registered virtual machines when restarted. It means that if a VM was running before a restart, Hyper-V will start it automatically. Automatic startup settings are configured for each VM individually.
Run the Hyper-V console, open the properties of any VM, and go to Settings -> Automatic Start Action. Three options to manage the automatic startup of a virtual machine are available:
- Nothing – when a host is started, a virtual machine doesn’t start automatically (regardless of its state before the host restart)
- Automatically start if it was running when the service stopped – a VM will automatically start only if it has been running before the host shutdown/restart
- Always start this virtual machine automatically – always start this virtual machine when the Hyper-V host boots.
One more parameter is available for the last option – Startup Delay. Here you can specify the startup delay time for the virtual machine (in seconds). Using the delay, you can manage the boot order of your virtual machines (for example, to boot a domain controller before starting a VM with SQL Server), and reduce the load on the disk storage due to starting multiple VMs in turn.
Also, in the Automatic Stop Action section, you can set what to do with your virtual machines if the host is shutdown or restarted.
- Save the virtual machine state – a full state of a virtual machine is saved (including its memory). At the next startup, the virtual machine will resume from this point. Note that you must have additional free space on your disk to keep your VM memory (*.BIN files). The guest OS is not restarted;
- Turn off the virtual machine – when a Hyper-V host is shutdown, a virtual machine will also be stopped (in the same way as a physical computer is shutdown). The VM state is not saved, a guest OS will be started with a full boot cycle. In this mode, there is some risk of getting inconsistent data in the apps running in the VM.
- Shutdown the guest operating system – a guest OS is shutdown using Hyper-V integration service (graceful shutdown). All apps running in the VM are stopped and the risk of getting inconsistent data is very low.
You can view and change the automatic startup and shutdown settings of your Hyper-V virtual machines using PowerShell.
Display the current startup and shutdown settings of all VMs:
Get-VM –VMname * | Select-Object VMname,AutomaticStartAction,AutomaticStartDelay,AutomaticStopAction
You can change the automatic startup settings of a VM using the AutomaticStartAction option. Its possible values are Nothing
, StartIfRunning
, Start
.
Get-VM –VMname lon-win10| Set-VM –AutomaticStartAction Start
To set up startup delay for all VMs except for one (for example, a domain controller with FSMO roles):
Get-VM –VMname * | Where-object –FilterScript {$_.vmname –notlike “lon-dc*”} | Set-VM –AutomaticStartDelay 90
Using the –AutomaticStopAction option, you can set the VM shutdown settings (Save
, TurnOff
, ShutDown
).
Boot (Startup) Order of Hyper-V Virtual Machines
When starting a standalone Hyper-V host, an administrator must manage the startup order of virtual machines on it. For example, you need the Exchange VM to boot only after the domain controller is available, and an app server to start after a database server. Hyper-V doesn’t have any built-in tools to manage startup order of the virtual machines, except for the start delay option (AutomaticStartDelay
).
In the simplest cases, you can configure the order of VM startup by setting different startup delays for them:
Get-VM –VMname lon-dc01| Set-VM –AutomaticStartDelay 0
Get-VM –VMname lon-exch1,lon-db01 | Set-VM –AutomaticStartDelay 90
Get-VM –VMname lon-rds01,lon-app01 | Set-VM –AutomaticStartDelay 180
Another way is to start VMs in turn using a PowerShell startup script. In the script, you can set a delay before starting the next VM and perform additional checks for the availability of an application or service in the VM (to make sure that the app or service has been started). To make it more convenient, you can join several VMs into a group using tags. For example, I have set the following tags for the VMs:
set-vm lon-dc01,lon-dc02 -Notes "Boot order 1"
set-vm lon-exch1, lon-db01 -Notes "Boot order 2"
set-vm lon-rds01,lon-app01 -Notes "Boot order 3"
The following PowerShell script starts virtual machines in a specific order and runs extra availability checks for some services (TCP ports) in the VM using the PowerShell cmdlet Test-NetConnection:
$VMtoStart = Get-VM | where notes -contains 'Boot order 1'
foreach ($cn in $VMtoStart)
{Start-VM $cn.name -asjob}
While (!(Test-NetConnection lon-dc01 -Port 445 -WarningAction SilentlyContinue).tcpTestSucceeded ){
Start-Sleep 30
}
$VMtoStart = Get-VM | where notes -contains 'Boot order 2'
foreach ($cn in $VMtoStart)
{Start-VM $cn.name -asjob}
While ((Test-NetConnection lon-exch1 -Port 25 -WarningAction SilentlyContinue).tcpTestSucceeded ){
Start-Sleep 30
}
$VMtoStart = Get-VM | where notes -contains 'Boot order 3'
foreach ($cn in $VMtoStart)
{Start-VM $cn.name -asjob}
Then add the PowerShell script to autostart or run it using the Task Scheduler (do not forget to disable automatic startup for all VMs that are started using this script). Remember that running PowerShell scripts is restricted in Windows by default. If needed, sign the PS1 script or change the PowerShell script execution policy.