vsphere powershell automated shutdown script

vmware

If you need like I have in the past an automated method to shutdown your vsphere environment but do not have a bucket of cash to throw at the issue then you need to turn to scripting.

Firstly I’d like to point out a couple of things.

I’m no powershell guru I cobbled this powershell script together a couple of years ago to get a belt and braces approach to powercuts in an environment I was working in where the the length of powercuts were either seconds or hours. Secondly there are now UPS devices that are vcenter aware so if you do have a bit of money to spare its a good idea to talk to your preferred reseller/consultant regarding current hardware offerings.

 

I’ve got a sneaky suspicion that this blog post might turn out to be rather long, however along with the script I’ll write a little about pre-req’s and gotchas that you might want to be aware of before implementing the script (By the way if you do use my script please TEST it first before moving it to your live environment as I cannot be held responsible for what it does in your environment).

This script was written to safely shutdown an entire VMware environment with a few user specified variables whilst logging its progress within a text file.

 

Things you need to know:

I use this script along with a UPS that has a network adapter in it and is SNMP compatible. I also use it in conjunction with the trap receiver application that is available from HERE. Although with a bit of tweaking and reading up on your particular UPS you could use the script with any number of different connection methods.

If you just want the script then CLICK HERE but please do read on as I shall describe what each section does, although powershell in itself is fairly simple to pick up and run with. I’ll break the script down into sections to make it easier to figure out.

 

add-pssnapin vmware.vimautomation.core

connect-viserver -server 192.168.1.1 -user root -password trickypassword

#Time to wait before assuming vm’s are stuck

$waittime = 90 #Seconds
$forcetime = 10 #Second

The first line of the script loads all the VMware based powershell snapins, the second line then connects to your vcenter server so please change these details to ones relevant to your environment.

The wait time and forcetime variables can be changed to your preference. After the initial shutdown of vm’s using the vmtools is initialized the script then waits the allotted time before presuming that the vm’s are stuck or are having issues, where upon it will poweroff the vms later on in the script. Its quite important to change this waittime line to something relevant to your environment, too short a time will result in vm’s being powered off forcibly whilst they are shutting down, too longer time may mean your battery will dischange and everything will come crashing down around your ears.

 

#script that outputs currently powered on vm’s to a file for later use

Write-output “powered on vm’s before graceful shutdown” | out-file c:\shutdown\shtdwnreport.txt
write-host “powered on vm’s before graceful shutdown”
get-vm | Where-Object {$_.Powerstate -eq “Poweredon”} | out-file c:\shutdown\shtdwnreport.txt -append

#Graceful shutdown of powered on vm’s that have vmtools installed

write-host “Attempting Graceful shutdown” -foregroundcolor red -backgroundcolor yellow
Get-VM | Where-Object {$_.PowerState -eq “PoweredOn”} | Where {$_.Name -ne “VCENTER”} | Shutdown-VMGuest -Confirm:$false
sleep $waittime

The next section of the script writes the hostnames of the vm’s that are powered on to a text file, again the out-file location is a variable so feel free to put that where you want it. The second part of this section begins the shutdown of the vm’s except the vcenter vm. The script must live on the vcenter vm for the script to complete correctly therefore the script must exclude (at the moment at least) the shutdown of the vcenter vm (again change this to the VMware vm name for the vcenter instance).

The last line of of this section puts the script to sleep for the amount of time as specified in the waittime variable above.

 

#script that outputs currently powered on vm’s after graceful shutdown attempt to the same file

write-output “powered on vm’s after graceful shutdown, will now be forced to shutdown” | out-file c:\shutdown\shtdwnreport.txt -append
write-host “powered on vm’s after graceful shutdown, will now be forced to shutdown”
get-vm | where-object {$_.powerstate -eq “poweredon”} | out-file c:\shutdown\shtdwnreport.txt –append

#Force vm shutdown

Write-Host “Forced shutdown of vm’s without tools or stuck on power down” -foregroundcolor red -backgroundcolor yellow
Get-vm | Where-Object {$_.PowerState -eq “PoweredOn”} | Where {$_.Name -ne “VCENTER”} | stop-vm -Confirm:$false
sleep $forcetime

 

This section of the script will list the remaining powered on vm’s after the waittime has expired and then forcibly shut them down. The script will again not shutdown the vcenter vm as the process has not quite finished yet so we need the vcenter vm for a little while longer. The forcetime is the amount of time the script waits before presuming that all the vm’s have been forcibly shutdown.

#shutdown esxi hosts

write-output “shutting down esxi hosts” | out-file c:\shutdown\shtdwnreport.txt -append
get-vmhost | out-file c:\shutdown\shtdwnreport.txt -append
get-vmhost | stop-vmhost -confirm:$false -force
exit

The last part of the script then shuts down the esxi hosts and again adds a text output to the file specified.

Now in my implementation I have installed the trap receiver application within the vcenter server and then creating a DR community string within trap receiver and on the UPS device. Because SNMP can now effectively shutdown your entire infrastructure then you must be sure that the traps and community strings you create cannot be accidentally triggered or “tested” . I’ve then configured the UPS to send an SNMP message to the receiver after 20 minutes of no power. Also I set the vcenter vm to automatically shutdown with the host by setting the Virtual Machine Startup/Shutdown options within the configuration tab of the VI client.

 

Full script:

add-pssnapin vmware.vimautomation.core

connect-viserver -server 192.168.11.241 -user root -password trickypassword

#Time to wait before assuming vm’s are stuck

$waittime = 90 #Seconds
$forcetime = 10 #Seconds

#script that outputs currently powered on vm’s to a file for later use

Write-output “powered on vm’s before graceful shutdown” | out-file c:\shutdown\shtdwnreport.txt
write-host “powered on vm’s before graceful shutdown”
get-vm | Where-Object {$_.Powerstate -eq “Poweredon”} | out-file c:\shutdown\shtdwnreport.txt -append

#Graceful shutdown of powered on vm’s that have vmtools installed

write-host “Attempting Graceful shutdown” -foregroundcolor red -backgroundcolor yellow
Get-VM | Where-Object {$_.PowerState -eq “PoweredOn”} | Where {$_.Name -ne “VCENTER”} | Shutdown-VMGuest -Confirm:$false
sleep $waittime

#script that outputs currently powered on vm’s after graceful shutdown attempt to the same file

write-output “powered on vm’s after graceful shutdown, will now be forced to shutdown” | out-file c:\shutdown\shtdwnreport.txt -append
write-host “powered on vm’s after graceful shutdown, will now be forced to shutdown”
get-vm | where-object {$_.powerstate -eq “poweredon”} | out-file c:\shutdown\shtdwnreport.txt -append

#Force vm shutdown

Write-Host “Forced shutdown of vm’s without tools or stuck on power down” -foregroundcolor red -backgroundcolor yellow
Get-vm | Where-Object {$_.PowerState -eq “PoweredOn”} | Where {$_.Name -ne “VCENTER”} | stop-vm -Confirm:$false
sleep $forcetime

#shutdown esxi hosts

write-output “shutting down esxi hosts” | out-file c:\shutdown\shtdwnreport.txt -append
get-vmhost | out-file c:\shutdown\shtdwnreport.txt -append
get-vmhost | stop-vmhost -confirm:$false -force
exit

 

Author: Dale Scriven

4 thoughts on “vsphere powershell automated shutdown script

  1. Hi…We have ESX5.5 host. But its not write licensed. So when i try to shutdown its giving me error msg like “Cuurent license or ESXi version prohibits execution of the requested operation.prohibits execution of the requested operation. By any means can we make it work?

    1. Hi Karthika,

      Are you by any chance trying to connect with powershell directly to an ESXi server and not a vCentre server. I’m assuming this is where the error is occurring, if you can give me some more info on when the error is occurring and what you are doing when it occurs I might be able to help. As you can probably see by the date of the post I wrote this some time ago so some powershell cmdlets may have changed.

      Regards
      Dale

  2. The “CLICK HERE” link in the second paragraph of the “Things you need to know” section isn’t working. Do you have another location where the file is?

    1. Thanks for letting me know, its nice to know that this post is still being used 5 years after I wrote it. I’ve corrected the link but obviously a lot of things has changed since I wrote it so you’ll need to test it first before deploying it in production.

Leave a Reply to Alonso CalixCancel reply