Home » Powershell » Restart Multiple VM’s

Restart Multiple VM’s

 Connecting to individual host

Open VMware vSphere PowerCLI console

#Connect to the host
Connect-VIServer -Server hostname.example.com

#Get all VMs from the host, filter those powered on and shut them down
Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false

Connecting to vCenter

If you connect to vCenter, you have more options. You can target a single host, a cluster, a datacenter or the whole vCenter.

If you need to shutdown all VMs in the vCenter, virtu-al.net has a nice script.

First connect to your vCenter

Connect-VIServer -Server vCenter.example.com
Shutdown all VMs in a host

#Get all VMs from the host, filter those powered on and shut them down
Get-VMHost “hostname.example.com” | Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false
Shutdown all VMs in a cluster

#Get all VMs from the cluster, filter those powered on and shut them down
Get-Cluster “Cluster1” | Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false
Shutdown all VMs in a datacenter

#Get all VMs from the datacenter, filter those powered on and shut them down
Get-DataCenter “DC1” | Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false
For one reason or the other, VMs refuse to shutdown gracefully. If you have a stuck VM, use Stop-VM to force poweroff.

Eg-

Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Stop-VM -Confirm:$false

#Restart VM from Text File

get-content c:\Temp\ServerList.txt | foreach-object { Restart-VM $_ -Confirm:$false}

#check poweron status

get-content c:\Temp\ServerList.txt | foreach-object { Where-Object {$_.powerstate -eq ‘PoweredOn’}}
get-content c:\Temp\serverlistvm.txt | foreach-Object {$_.powerstate -eq ‘PoweredOn’}|Shutdown-VMGuest $_ -Confirm:$false
get-content c:\Temp\serverlistvm.txt | foreach-Object {Stop-VM $_ -Confirm:$false}

Share This:
Facebooktwitter

Leave a Reply

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

Name *
Email *
Website