VMware vSphere vCenter and ESXi Home Lab shutdown script with PowerCLI
- Get link
- Other Apps
Some times you need to shut down your lab in as efficient way as possible.
I do this quite often as I don't like leaving my home lab running during various events (thunderstorms, weekends , vacation etc). And I really dont like having to click , wait, click, wait, click.... wait some more to get every thing down.
So I wrote this script to power down my lab, Ill walk you through what it does and the completed version will be at the end of this post. (I'm also including a start-up script I use as well)
First we need to import our power CLI modules and tell the script to ignore any self-signed or Invalid certificates ( If you want more info on setting up powerCli see the beginning of this post for more info)
-------------------------------------------------------------------
Import-Module VMware.PowerCLI
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
-------------------------------------------------------------------
Now we need to setup our credential variables, one will be for vCenter, and the other is the host that everything will be migrated to and then eventually shut down on. We are going to use a feature in power CLI that allows us to store our password so we don't have to have it in plain text in our script.
You should do this on the computer you will always run this script on.
For the ESXi host, we will use root.
From the command prompt run the following replacing the -Host with something you can identify as your host, and -Password with your password
New-VICredentialStoreItem -Host AlphaHost -User root -Password "SuperSecretP@$$w0rd"
Next lets store the password for your vcenter, I will be using administrator@vsphere.local as it does not require AD to be up and running.
New-VICredentialStoreItem -Host VCSA -User administrator@vsphere.local -Password "SuperSecretP@$$w0rd"
And now the lines we put in the script are:
-------------------------------------------------------------------
$VCSAPass = get-VICredentialStoreItem -Host VCSA -User administrator@vsphere.local
$rootPass = get-VICredentialStoreItem -Host AlphaHost -User root
-------------------------------------------------------------------
And now we connect to that vCenter
-------------------------------------------------------------------
connect-VIServer vcenter.home.lab -User 'administrator@vsphere.local' -Password $VCSAPass.Password
-------------------------------------------------------------------
The next step is we are going to create the vmservers variable that gets a list of all VMs that are powered on, except for our vcenter, domain controllers and the vCLS vms, and then shutdown the guest OS of the VM's. We do this as we need the DC for DNS resolution, and the vCLS vms will be powered off in a later step by vCenter (if they are powered off now, they just get auto started back up)
-------------------------------------------------------------------
$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’ -and $_.name -ne 'vcenter.home.lab' -and $_.name -ne 'dc1.home.lab' -and $_.name -ne 'dc2.home.lab' -and $_.name -cnotlike 'vCLS*' }
$vmservers| Shutdown-VMGuest -Confirm:$false
Now I have a 3 node cluster, so I will be putting 2/3 into maintenance mode before shutting them down. Since i have DRS enabled this will move the remaining VMs to a single host (makes startup script work as well)
-------------------------------------------------------------------
Get-VMHost -Name charlie.home.lab | set-vmhost -State Maintenance
Get-VMHost -Name bravo.home.lab | set-vmhost -State Maintenance
-------------------------------------------------------------------
Now that our hosts are in maintanence mode, we are going to set retreat mode for vCLS vms, this will cause vCenter to delete them(to find your domain-c<number> see the following VMware KB to get that info and replace it in the line below https://kb.vmware.com/s/article/80472):
-------------------------------------------------------------------
Get-AdvancedSetting -Entity vcenter.home.lab -Name 'config.vcls.clusters.domain-c26.enabled' | Set-AdvancedSetting -Value 'false' -Confirm:$false
start-sleep 60
-------------------------------------------------------------------
Now lets shut down those hosts
-------------------------------------------------------------------
Get-VMHost -Name charlie.home.lab | Stop-VMHost -Confirm:$false
Get-VMHost -Name bravo.home.lab | Stop-VMHost -Confirm:$false
-------------------------------------------------------------------
Our tasks for vCenter are done, we now disconnect from the vCenter , and connect directly to the last ESXi hosts that still has VMs running by IP:
-------------------------------------------------------------------
Disconnect-VIServer vcenter.home.lab -confirm:$false
connect-VIServer 192.168.0.252 -User 'root' -Password $rootPass.Password
-------------------------------------------------------------------
Now we power off the last set of VM's(dc's and vCenter should be all thats left):
-------------------------------------------------------------------
$morevmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}
$morevmservers| Shutdown-VMGuest -Confirm:$false
do { $vmstatus = (get-vm $morevmservers).PowerState ; Start-Sleep -Seconds 5} while ($vmstatus -eq "PoweredOn")
-------------------------------------------------------------------
And put this host into maintenance mode, then power it off and disconnect
-------------------------------------------------------------------
set-vmhost -State Maintenance
-------------------------------------------------------------------
Lastly I put in a final check to verify that all the ESXi hosts are no longer ping-able (powered off)
-------------------------------------------------------------------
#Check servers are down
do{}Until (!(Test-Connection 192.168.0.250 -Quiet -Count 1))
Write-Host -ForegroundColor Black -BackgroundColor Green "Host Charlie.Home.Lab Is Down"
do{}Until (!(Test-Connection 192.168.0.251 -Quiet -Count 1))
Write-Host -ForegroundColor Black -BackgroundColor Green "Host Bravo.Home.Lab Is Down"
do{}Until (!(Test-Connection 192.168.0.252 -Quiet -Count 1))
Write-Host -ForegroundColor Black -BackgroundColor Green "Host Alpha.Home.Lab Is Down"
-------------------------------------------------------------------
That's it, so as mentioned here is the full script:
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------vSphere Lap Power Off Script------
#--------------------------------------------------------------------
#------------By Jeramy Thompson------------------------------
#-----------https://jeramythompson.blogspot.com/----------
#-------------------------------------------------------------------
Import-Module VMware.PowerCLI
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$VCSAPass = get-VICredentialStoreItem -Host VCSA -User administrator@vsphere.local
$rootPass = get-VICredentialStoreItem -Host AlphaHost -User root
connect-VIServer vcenter.home.lab -User 'administrator@vsphere.local' -Password $VCSAPass.Password
$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’ -and $_.name -ne 'vcenter.home.lab' -and $_.name -ne 'dc1.home.lab' -and $_.name -ne 'dc2.home.lab' -and $_.name -cnotlike 'vCLS*' }
$vmservers| Shutdown-VMGuest -Confirm:$false
Get-VMHost -Name charlie.home.lab | set-vmhost -State Maintenance
Get-VMHost -Name bravo.home.lab | set-vmhost -State Maintenance
Get-AdvancedSetting -Entity vcenter.home.lab -Name 'config.vcls.clusters.domain-c26.enabled' | Set-AdvancedSetting -Value 'false' -Confirm:$false
start-sleep 60
Get-VMHost -Name charlie.home.lab | Stop-VMHost -Confirm:$false
Get-VMHost -Name bravo.home.lab | Stop-VMHost -Confirm:$false
Disconnect-VIServer vcenter.home.lab -confirm:$false
connect-VIServer 192.168.0.252 -User 'root' -Password $rootPass.Password
$morevmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}
$morevmservers| Shutdown-VMGuest -Confirm:$false
do { $vmstatus = (get-vm $morevmservers).PowerState ; Start-Sleep -Seconds 5} while ($vmstatus -eq "PoweredOn")
set-vmhost -State Maintenance
do{}Until (!(Test-Connection 192.168.0.250 -Quiet -Count 1))
Write-Host -ForegroundColor Black -BackgroundColor Green "Host Charlie.Home.Lab Is Down"
do{}Until (!(Test-Connection 192.168.0.251 -Quiet -Count 1))
Write-Host -ForegroundColor Black -BackgroundColor Green "Host Bravo.Home.Lab Is Down"
do{}Until (!(Test-Connection 192.168.0.252 -Quiet -Count 1))
Write-Host -ForegroundColor Black -BackgroundColor Green "Host Alpha.Home.Lab Is Down"
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
As Mentioned I also have a startup scrip. At this point its should be self explanitory.
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------vSphere Lap Power ON Script------
#--------------------------------------------------------------------
#------------By Jeramy Thompson------------------------------
#-----------https://jeramythompson.blogspot.com/----------
#-------------------------------------------------------------------
Import-Module VMware.PowerCLI
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$VCSAPass = get-VICredentialStoreItem -Host VCSA -User administrator@vsphere.local
$rootPass = get-VICredentialStoreItem -Host AlphaHost -User root
#Connect To Alpha
connect-VIServer 192.168.0.252 -User 'root' -Password $rootPass.Password
#If its in Maintence mode, exit it:
set-vmhost -State connected
$StartServers=Get-VM | Where-Object {$_.name -eq 'vcenter.home.lab' -or $_.name -eq 'dc1.home.lab' -or $_.name -eq 'dc2.home.lab'}
$StartServers| Start-VM -Confirm:$false
start-sleep 60
Disconnect-VIServer 192.168.0.252 -confirm:$false
Start-Sleep 600
connect-VIServer vcenter.home.lab -User 'administrator@vsphere.local' -Password $VCSAPass.Password
Get-AdvancedSetting -Entity vcenter.home.lab -Name 'config.vcls.clusters.domain-c26.enabled' | Set-AdvancedSetting -Value 'true' -Confirm:$false
Get-VMHost -Name charlie.home.lab | set-vmhost -State Connected
Get-VMHost -Name bravo.home.lab | set-vmhost -State Connected
#Start the vApp that contains other essentials
Start-VApp -VApp Infra -Confirm:$false
Disconnect-VIServer vcenter.home.lab -confirm:$false
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
Comments
Post a Comment