Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

Lab Shutdown Script – Using PowerCLI – VirtuallyThatGuy

Posted on 14 March 20206 December 2022 by 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

Leave a Reply Cancel reply

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

When autocomplete results are available use up and down arrows to review and enter to go to the desired page. Touch device users, explore by touch or with swipe gestures.

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • JB on Script: How to get VM with Tag Assignment and export results to csv using PowerCLI or Powershell
  • DL on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • 360coolp on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • Yogesh on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2025 VirtuallyThatGuy | Powered by Superbs Personal Blog theme