Maybe you want to shutdown all the Virtual Machines on the HyperV server with just a click.
Well here are some tools that will you do your life easier.
Create a new folder under the C:\ drive called HyperV and create these 3 files (you can use notepad, just copy and paste)
BAT FileĀ (shutdown_vm.bat)
1 2 3 4 5 6 7 8 |
@echo off REM Hyper-V Virtual Machine Shutdown REM Shutting down virtual machines %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\HyperV\shutdown_vm.ps1 C:\HyperV\vms.txt 1 REM Finished! |
PowerShell script (shutdown_vm.ps1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# ---------- SCRIPT STARTS HERE-------------- $waitshutdown = 120 if ($args[1] -match "1") { $inputfile=get-content $args[0] foreach ($guest in $inputfile) { write-host "Shutting down $guest" $vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'" $vmname = $vm.name $vmshut = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'" $result = $vmshut.InitiateShutdown("$true","no comment") if ($result.returnvalue -match "0") { start-sleep -s $waitshutdown write-host "" write-host "No error was detected while Shutting down $guest" write-host "Shutdown of $guest has been Completed" -foregroundcolor green write-host "" } else { write-host "" write-host "Unable to Shutdown $guest" -foregroundcolor red write-host "" } } } else { write-host "USAGE: to shutdown VMs," -nonewline; write-host ".\shutdown_vm.ps1 C:\HyperV\vms.txt 1" -foregroundcolor yellow write-host "USAGE: to start VMs," -nonewline; write-host ".\shutdown_vm.ps1 c:\HyperV\vms.txt 0" -foregroundcolor yellow } # ---------- SCRIPT ENDS HERE-------------- |
Virtual Machine List (vms.txt)
In this file, you have to put the name of the Virtual Machines you want to shutdown (one per line).
1 2 3 4 |
VM1 VM2 VM3 ... |