Here is a script that I wrote for powering on the virtual machines in a specific order. This script creates a function named PowerOnVM. You can then call PowerOnVM with the parameters $VM and $Wait. $VM should be equal to the name of the VM you want to turn on. $Wait will give the option to wait until the VM has fully booted before continuing, or to send the start command and continue on to the next VM. The values for $Wait should be set to “1” to wait, or “0” to continue.
The wait parameter works by using the ping-vm cmdlet. It will ping the VM every 10 seconds until it receives a response. Once it receives a response, it will continue on to the next VM. I also added a command to flush the client DNS resolver cache on every tenth failed ping. This will help to prevent DHCP based VM’s from causing the script to hang.
To run this script on your hyper-v server you need to save the code below as a PS1 and add the command “PowerOnVM –vm <VM name> –Wait 0 or 1” at the bottom. In the example below the VM’s DC1 and SQL1 will power on and wait for the response before continuing. The VM’s Stor1 and Exchange1 will power on and continue to the next VM.
Import-Module "C:\Program Files\modules\hyperV"
function PowerOnVM
{
param($vm, $Wait)
if ($Wait -eq 1) {
start-vm $vm
$i = ping-vm $vm
$x = $i.Status
$count = 0
do {
Write-Host $x
Write-Host $vm " is not running"
Write-Host "Count: " $count
$i = ping-vm $vm
$x = $i.Status
Start-Sleep -s 10
$count++
$DNS = $count.ToString()
$DNS = $DNS.substring($DNS.length-1,1)
if ($DNS -eq "0") {
Write-Host "Flushing DNS"
ipconfig /flushdns}
}
while ($x -ne "Success")
Write-Host $vm " is running"
Start-Sleep -s 30
}
else {
start-vm $vm
Start-Sleep -s 10
}
}
#Only make changes below this line
#Add a new line for each VM
#Paramters: -vm = the name of the VM to turn on
# -Wait = 1 to wait until VM is fully turned on
# 0 to not wait
PowerOnVM -vm DC1 -Wait 1
PowerOnVM -vm SQL1 -Wait 1
PowerOnVM -vm Stor1 -Wait 0
PowerOnVM -vm Exchange1 -Wait 0
This script requires that you have the PowerShell Management Library for Hyper-V install. It is available through CodePlex at http://pshyperv.codeplex.com/.
Here is a script that I wrote for powering on the virtual machines in a specific order. This script creates a function named PowerOnVM. You can then call PowerOnVM with the parameters $VM and $Wait. $VM should be equal to the name of the VM you want to turn on. $Wait will give the option to wait until the VM has fully booted before continuing, or to send the start command and continue on to the next VM. The values for $Wait should be set to “1” to wait, or “0” to continue. The wait parameter works by using the ping-vm cmdlet. It will ping the VM every 10 seconds until it receives a response. Once it receives a response, it will continue on to the next VM. I also added a command to flush the client DNS resolver cache on every tenth failed ping. This will help to prevent DHCP based VM’s from causing the script to hang. To run this script on your hyper-v server you need to save the code below as a PS1 and add the command “PowerOnVM -vm