Today I wondered that with Hyper-V there is no option to „reboot“ a VM with right clicking on it. With ESX I have the option to reboot the guest and so I created a script to schedule the reboot with the help of VMWare Powershell and VMWare tools.
The Hyper-V modules on SCVMM gives you the option to shut down and start a VM. Therefore I wrote a simple script that shutdown all VM machines in a specified list (you can use an argument if you want to work with multiple lists).
The shutdown is initiated with the “-RunAsynchronously“ switch so that the Powershell command does not wait until the vm is powered off. Immediately after sending the shutdown command a “do while” loop begins to work and waits until one of the machines is powered off. If a machine is switched off the loop powers the VM machine on. The loop ends when all machines are started up.
As I have also a VMWare vCenter connected in SCVMM I only touch VM machines with a filter: VirtualizationPlatform = “HyperV”
# Import Module
Import-Module -Name "virtualmachinemanager"
# Clear Variables
[String[]] $VMsToStart = $Null
# Get List of VM's to reboot
$VMsToReboot = Get-Content "C:\Program Files\Scripts\ServerList.txt"
# Shutdown each VM
Foreach($VMToShutdown in $VMsToReboot){
$VMToShutdownObject = Get-SCVirtualMachine $VMToShutdown
If ($VMToShutdownObject -ne $Null){
If($VMToShutdownObject.VirtualizationPlatform -eq "HyperV" -and $VMToShutdownObject.VirtualMachineState -eq "Running"){
Shutdown-VM -VM $VMToShutdownObject -RunAsynchronously
$VMsToStart += $VMToShutdown
}
}Else{
Write-Verbose ("Machine " + $VMToShutdown + " not found")
}
}
# Start each VM after graceful shutdown
Do{
ForEach($VMToStart in $VMsToStart){
$VMToStartObject = Get-SCVirtualMachine $VMToStart
If($VMToStartObject.VirtualMachineState -eq "PowerOff"){
Start-VM -VM $VMToStartObject -RunAsynchronously
$VMsToStart = $VMsToStart | ? {$_ -ne $VMToStart}
}
}
}while($VMsToStart.Count -ne 0)