Some updates to the PowerCLI Shutdown VMs Script – Now w/ a virtualized vCenter option

shutdown-button-mdOver that past few months I've been getting quite a few emails and comments as it pertains to the scripts I have shared to shutdown and power on VMs in the event of power loss.  I assume since the storm weather is kicking in people are realizing that they need to have some kind of protection in regards to gracefully getting the VMs down.  Now I've had the (privilege?) of watching my script run multiple times in a production environment and it has gotten to the point where it is working flawlessly.  One caveat to the script though – whichever Windows box is executing the PowerShell must stay powered on longer than your vCenter Server.

That being said the script would be pretty useless if you had a virtualized vCenter Server running on the same cluster as your production VMs that your are shutting down – this is what all of the emails and comments that I've been getting have been based around.  So, without further ado I've added a few updates to the script to better support those with a virtualized vCenter.  A few explanations below…

So first off (Line 50) I've added a variable called $vCenterVMName and assigned this a default value of NA.  This variable should hold the VM Name of your vCenter Server.  I've tried to keep just one version of the script supporting both virtualized and non-virtualized vCenter's thus is why the default to "NA".  So if you don't have a virtualized vCenter just leave the variable set to NA and the script will run as it always had, otherwise if you do, put its' VM name in here.

Secondly down on Lines 107 to 1118 I do a few things.  First, check to see if you have changed that default value and if you have we need to figure out which ESXi host vCenter is running on in order to shut it down last.  This is what's done here.

Then, in each of the shutdown loops (Line 125 and Line 162) I've added an if statement.  Basically it just checks to see if the VM it is processing is the vCenter Server.  If so, it doesn't process the shutdown.

Lastly I've done a couple more things.  On Line 186 I've added a check while shutting down the hosts.  If the host that is being processed is the host that vCenter is running on I want to leave it till the end, so it is skipped during this loop.  Then, the finally the block of code starting on Line 196, if needed, will send the shutdown command to the host that is running vCenter Server.

As you may of noticed I've added a some logging as well, nothing bit, just a record of what happened in a txt file rather than relying on Write-Host.  The script in its' entirety is below or you can download it here.  Watch for updates to the power on script to support the vritualized vCenter as well.  Just not sure how I'm going to tackle that one yet 🙂

So there you have it!  Hopefully this will help some of you out with your shutdown endeavors.  Feel free to make any changes you need in order to make it work for your environment and let me know how it goes – keep those comments, concerns, thoughts and emails coming in!

6 thoughts on “Some updates to the PowerCLI Shutdown VMs Script – Now w/ a virtualized vCenter option”

  1. Thanks for the updated script. I need to get this updated because I now have an SSO server, Inventory server, and vcenter server. Any ideas the easiest way to exclude all of my vcenter servers from shutting down until the end?

  2. Hi,

    i found your script to be really great, do you mind if i post it on my blog with some changes i made?

    I added the following:

    – support for vApps
    – removed annoying error messages when variables are = null
    – added support for encrypted password dumped on a file (don’t like password in clear text)
    – added date-time in the log file for each event
    – minor cosmetic in the output
    – tested with vsphere 5.5 U1

    1. You could technically pass your Datacenter object name into the cluster variable, then it would grab all the VMs located in that virtual datacenter! Probably the easiest way! You will have to ensure that you perform the HA/DRS enable/disable on both clusters though if you want to do that!

  3. Hi, this is really great! I wonder – is there a way to store the result? for example – if a VM can’t be shut down for whatever reason, is there some kind of output that can be stored in the log?

    1. I’m sure there is! I’ve been through a few iterations of this script and have a lot of notes and changes I’d like to make upon my next! I’ll be sure to keep that in mind! Thanks!

Leave a Comment

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

Scroll to Top