Lab Shutdown Script – Using PowerCLI – VirtuallyThatGuy

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

## 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

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

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