This is a handy script I was playing with to shutdown my lab remotely. I hope you find some of the commands useful
## Lab Shutdown script
$cred = Get-Credential
$vCenters = "lab-vcenter01.lab.local", "lab-vcenter02.lab.local"
Connect-VIServer $vCenters -Credential $cred
Get-Cluster "*VSAN*" | get-vm | Shutdown-VMGuest -Confirm:$false
Start-Sleep -Seconds 20
Get-Cluster "*VDI*" | get-vm | Shutdown-VMGuest -Confirm:$false
Start-Sleep -Seconds 20
Get-Cluster "*NESTED*" | get-vm | Shutdown-VMGuest -Confirm:$false
Start-Sleep -Seconds 20
Get-Cluster "*VSAN*" | get-vm | Stop-VM -Confirm:$false
Start-Sleep -Seconds 10
Get-Cluster "*VDI*" | get-vm | Stop-VM -Confirm:$false
Start-Sleep -Seconds 10
Get-Cluster "*NESTED*" | get-vm | Stop-VM -Confirm:$false
Start-Sleep -Seconds 10
$NestedHost = "LAB-ESXi10.lab.local","LAB-ESXi11.lab.local","LAB-ESXi12.lab.local","LAB-ESXi08.lab.local","LAB-ESXi09.lab.local","LAB-ESXi07.lab.local"
get-vmhost $NestedHost | Set-VMHost -state Maintenance -confirm:$false
get-vm | ?{$_.Name -notlike "*vcenter*" -and $_.Name -notlike "*esx*" -and $_.Name -notlike "*vstorage*"} | Shutdown-vmguest -Confirm:$false
Start-Sleep -Seconds 120
get-vm | ?{$_.Name -notlike "*vcenter*" -and $_.Name -notlike "*esx*" -and $_.Name -notlike "*vstorage*" -and $_.PowerState -like "*On*"} | Stop-vm -Confirm:$false
get-vmhost | ?{$_.Name -like "*lab-esx*"} | Set-VMHost -state Maintenance -confirm:$false
##shutdown hosts in MM
get-vmhost "*lab-esx*" |?{$_.ConnectionState -like "*Main*"} | Stop-VMHost -Confirm:$false
disConnect-VIServer $vCenters -Confirm:$false
## connect to main lab
Connect-Viserver MainHost01.lab.local -User root -Password StandUpIfYouHateTottenham
get-vm |?{$_.PowerState -like "*on*"} | ft -a
get-vm |?{$_.PowerState -like "*on*"} | Shutdown-VMGuest -Confirm:$false
Start-Sleep -Seconds 60
get-vm |?{$_.PowerState -like "*on*"} | Shutdown-VMGuest -Confirm:$false
get-vm |?{$_.PowerState -like "*on*"} | Stop-VM -Confirm:$false
get-vm |?{$_.PowerState -like "*on*"} | Shutdown-VMGuest -Confirm:$false
get-vm |?{$_.PowerState -like "*on*"} | Stop-VM -Confirm:$false
get-vmhost | Set-VMHost -state Maintenance -confirm:$false
Start-Sleep -Seconds 10
get-vmhost | Set-VMHost -state Maintenance -confirm:$false
Start-Sleep -Seconds 10
get-vmhost | Set-VMHost -state Maintenance -confirm:$false
get-vmhost | ?{$_.ConnectionState -like "*Main*"} | Stop-VMHost -Confirm:$false
## end of script
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | ## Lab Shutdown script #$cred = Get-Credential #Start-Sleep -Seconds 1200 $vCenters = "lab-vcenter01.lab.local" , "lab-vcenter02.lab.local" #Connect-VIServer $vCenters -Credential $cred Connect-VIServer $vCenters -User rboadi @lab .local -Password StandUpIfYouHateTottenham -ErrorAction SilentlyContinue $NestedHost = "LAB-vSAN-ESXi0*" Get-Cluster "*VSAN*" | get-vm | Shutdown-VMGuest -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 20 Get-Cluster "*VDI*" | get-vm | Shutdown-VMGuest -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 20 Get-Cluster "*NESTED*" | get-vm | Shutdown-VMGuest -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 20 Get-Cluster "*VSAN*" | get-vm | Stop-VM -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 10 Get-Cluster "*VDI*" | get-vm | Stop-VM -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 10 Get-Cluster "*NESTED*" | get-vm | Stop-VM -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 10 $NestedHost = "LAB-vSAN-ESXi01.lab.local" , "LAB-vSAN-ESXi02.lab.local" , "LAB-vSAN-ESXi03.lab.local" get-vmhost $NestedHost | Set-VMHost -state Maintenance -confirm : $false -ErrorAction SilentlyContinue get-vm | ?{ $_ .Name -notlike "*vcenter*" -and $_ .Name -notlike "*esx*" -and $_ .Name -notlike "*vstorage*" } | Shutdown-vmguest -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 120 get-vm | ?{ $_ .Name -notlike "*vcenter*" -and $_ .Name -notlike "*esx*" -and $_ .Name -notlike "*vstorage*" -and $_ .PowerState -like "*On*" } | Stop-vm -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 120 get-vmhost | ?{ $_ .Name -like "*lab-esx*" } | Set-VMHost -state Maintenance -confirm : $false -ErrorAction SilentlyContinue ##shutdown hosts in MM get-vmhost "*lab-esx*" |?{ $_ .ConnectionState -like "*Main*" } | Stop-VMHost -Confirm : $false -ErrorAction SilentlyContinue disConnect-VIServer $vCenters -Confirm : $false -ErrorAction SilentlyContinue ## connect to main lab Connect-Viserver MainHost01.lab.local -User root -Password StandUpIfYouHateTottenham get-vm |?{ $_ .PowerState -like "*on*" } | ft -a get-vm |?{ $_ .PowerState -like "*on*" } | Shutdown-VMGuest -Confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 120 get-vm |?{ $_ .PowerState -like "*on*" } | Shutdown-VMGuest -Confirm : $false -ErrorAction SilentlyContinue get-vm |?{ $_ .PowerState -like "*on*" } | Stop-VM -Confirm : $false -ErrorAction SilentlyContinue get-vm |?{ $_ .PowerState -like "*on*" } | Shutdown-VMGuest -Confirm : $false -ErrorAction SilentlyContinue get-vm |?{ $_ .PowerState -like "*on*" } | Stop-VM -Confirm : $false -ErrorAction SilentlyContinue get-vmhost | Set-VMHost -state Maintenance -confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 10 get-vmhost | Set-VMHost -state Maintenance -confirm : $false -ErrorAction SilentlyContinue Start-Sleep -Seconds 10 get-vmhost | Set-VMHost -state Maintenance -confirm : $false -ErrorAction SilentlyContinue get-vmhost | ?{ $_ .ConnectionState -like "*Main*" } | Stop-VMHost -Confirm : $false -ErrorAction SilentlyContinue ## end of script |