Line em up! Starting Hyper-V VMs in Specific Order

At my recent IT Pro talks, a frequent question has been raised in regards to options for starting virtual machines running on Windows Server 2012 Hyper-V or our FREE Hyper-V Server 2012 in a particular order.  In many environments, some virtual machines (VMs) may have dependencies on other VMs being up and running first – for example, a Domain Controller VM may need to be started prior to application VMs that authenticate to that Domain Controller. 

In this article, I’ll explore three options that exist as out-of-the-box solutions for starting VMs in a specific order based on how Hyper-V is deployed within an environment and the requirements for starting VMs.

Let’s Start Simple …

Hyper-V includes per-VM “Automatic Start Actions” that can be used to start VMs in a particular order when a Hyper-V host is booted by setting a Startup delay in seconds for each VM.  Although the Startup delay was originally intended to reduce resource content between virtual machines during the initial boot of a Hyper-V host, it can also be used to specify an order to start each VM.  For example, to start three different sets of VMs in a particular order, you could vary the Startup delay value across the VMs as follows:

  • First set of VM’s to start: set Startup delay to 0 seconds in the settings of each VM
  • Second set of VM’s to start: set Startup delay to 60 seconds in the settings of each VM
  • Third set of VM’s to start: set Startup delay to 120 seconds in the settings of each VM

HyperVMgrAutoStartAction

Starting VM’s across several Hyper-V hosts in order …

If you have several Hyper-V hosts running Windows Server 2012 or Hyper-V Server 2012, you can cluster them together.  Once defined in a cluster, you can then use Failover Cluster Manager to set the Startup Priority setting for each clustered VM to determine the order in which VM’s should start when the cluster comes online.  For example, to start three different sets of VMs in a particular order, you could vary the Priority value across the VMs as follows:

  • First set of VM’s to start: set Startup Priority to High on each clustered VM
  • Second set of VM’s to start: set Startup Priority to Medium on each clustered VM
  • Third set of VM’s to start: set Startup Priority to Low on each clustered VM

ClusterPriorities01 

Once the Startup Priority is defined for clustered VMs , this startup priority determines the order in which the VMs start when the cluster comes online as well as the order in which these VMs will failover between cluster nodes.  Very useful in clustered VM environments!

What if I have more complex VM startup needs?

If your needs are more complex, you can always leverage PowerShell 3.0 to script the startup of VM’s in a particular order.  In addition, PowerShell also offers the flexibility to optionally include script code that performs customized connectivity testing for each VM before moving on to start the next VM in sequence.

Below, I’ve provided a sample PowerShell script with comments to demonstrate a basic approach for using PowerShell to manage the startup of VMs.  You can customize the variables at the beginning of the script to match your environment as needed. 

# Enter list of VMs to start in order below ...

$VMList = ( "KEMLABDC01", "KEMLABFS01", "KEMLABFS02" )

# Enter name of Hyper-V host or cluster below ...

$VMHost = "KEMLABHV01"

# Enter maximum number of retries to test connectivity to VM ...

$MaxRetries = 6

# Enter number of seconds to wait between retries for testing VM connectivity ...

$SleepTime = 20

For ( $VMCount = 0; $VMCount -lt $VMList.Count; $VMCount++)
{
$VM = $VMList[$VMCount]

    $VMProgress = 100 / $VMList.Count * ($VMCount + 1)

    Write-Progress -Activity "Starting VMs" -CurrentOperation "Starting.." -Status $VM -PercentComplete $VMProgress

   # Start the Virtual Machine - if using a cluster, use Start-ClusterResource Cmdlet instead

    Start-VM -Name $VM -ComputerName $VMHost

$VMup = $False

    $Retries = 0

    Write-Progress -Activity "Starting VMs" -CurrentOperation "Waiting.." -Status $VM -PercentComplete $VMProgress

    do
{
Start-Sleep -Seconds $SleepTime
$VMup = Test-Connection -ComputerName $VM -Quiet
$Retries++
}
until ( $VMup -or ($Retries -ge $MaxRetries) )

    if ( !$VMup )
{
Write-Host "${VM}: not responding. Automated startup aborted."
Break
}

}

In this example, I’m using the Write-Progress Cmdlet to provide a basic visual progress bar and the Test-Connection Cmdlet to perform a basic ICMP ping test of each VM, but you can easily add more functionality or testing logic if needed.  For instance, in some cases you may want to also include Get-Service as part of the testing process if you want to ensure that particular services are started on a dependent VM before attempting to start the next VM in order.

What’s Next?

Your turn! Build your lab environment with Windows Server 2012 and configure your own VM startup approach using the suggestions above.

Do You Have Other Scenarios?

Do you have additional VM startup scenarios that you are using in your shop?  Feel free to post your feedback below in the comments to share your ideas!

HTH,

Keith