Stopping Virtual Machines with VMM using PowerShell

You can start, stop and restore virtual machines easily using PowerShell. In the next couple of posts I will start, stop and restore virtual machines using PowerShell.

 

You can add the VMM snapin with ‘Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager’ if you dont want to use ‘Windows PowerShell – Virtual Machine Machiner’

 

Before you can utilize PowerShell with VMM you need to connect to the server. You connect to the server with ‘Get-VMMServer -ComputerName “<ComputerName> | out-null”’

image

You now made connection with the VMM server and you are ready to stop the virtual machine. Enter ‘Stop-VM –VM <ComputerName> | out-null’ to stop the virtual machine.

image

That’s all there is to stop your virtual machines. This script is quite simple and there are no special things in this post.

You can add additional features like an array with all your servers that you want to start, stop or restore.

 

foreach($Server in $serverarray)
{
$SelectedVM = get-VM -Name $Server</span>

<span style="color: #000000;">    #Stop VM's
Stop-VM -VM $SelectedVM | out-null</span>

<span style="color: #000000;">    do
{
start-sleep -seconds 3
Write-Host "$server is shutting down" -foregroundcolor cyan
}</span>

<span style="color: #000000;">    until($selectedVM.status -eq "PowerOff")</span>

Write-Host "$server turned off successfull" -foregroundcolor green

<span style="color: #000000;">}

 

You probably dont want to shut down multiple servers at the same time. With a simple do statement you can wait for every server to shut down properly.

do
{
Write-Host "$server is shutting down" -foregroundcolor cyan
start-sleep -seconds 3
}
until($selectedVM.status -eq "PowerOff")

One thought on “Stopping Virtual Machines with VMM using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *