When was this VM Shutdown

Continuing with a theme of simple scripts for the every day administrator, I was inspired again by the VMTN forums. Similar to several questions regarding the date a VM was deployed, there have been several question as to when a VM was shut down. The simple answer is to parse the events, but depending on how old the VM is, you could lose the events to the 30 day default window. Once again, a scheduled task can help with this.

Below you’ll find another script I wrote, very similar to my VM-CreationNotes script. In this case, we’re looking for shutdown events, checking if the VM in question is there AND powered off, then adding additional note details to indicate when and whom shut down the VM. The result produces a note like this:

Once again, I like notes as they stay with the VM and are easily accessible, I don’t need to go looking for them. With a little jury-rigging, you could easily use this script to feed a CMDB or custom field if you want. I run this on a daily basis using a Windows VM dedicated to task schedules and running scripts. You’ll note that I once again used Luc Dekens’ Get-VIEventPlus due to its performance.

Below is the code – scroll down if you want the play-by-play annotations of what’s going on. If you’d like to get the latest version, or contribute, you can join my Github repository at https://github.com/kdmhorn/powercli – it’s in the “adminscripts” folder. Thanks for reading and be sure to stay tuned for more to come!


Lines 1 – 35: Not much code to see here, I like to include fairly detailed notes on what the script is for and how it changes over time – you should too.

Lines 36-137: This is the Get-VIEventPlus function from LucD. You can read more about it here. Simply put, it’s fast, really fast!

Lines 138-152: Here is where the runtime is set up. In Line 141 I identify the number of days between each run of the script, by default I specify 1 day. $AdminName is pretty straight forward, change it to whatever account has the rights to read VIEvents and can change VM properties. In line 143, I’m using a password file to safely store the password. There’s a decent article here on how to do that. Line 144 I load a text file that simply has a list of vcenter servers FQDNs on each line, this is so I can reuse that list over again in other scripts, but also so I don’t need to initiate a change on all my scripts to add or remove vcenters from the list. In Lines 146-148, I set up an array of VM shutdown type events. Note the remark, that if there’s an event type you don’t particularly care about you can remark out or remove that line. I intentionally left VMPoweredOff events out of this list as these are not only often redundant, but in the case of a VmGuestShutdownEvent, they will not produce a responsible party. And finally I set up a constant for readability of  a carriage return and line feed to keep the note nicely formatted.

Lines 153-164: In this section, I convert some of the run-time parameters above into actionable variables. Most notable are creating the credential object that will be used to log into each vCenter, and converting and gathering the dates from which the viEvents will be retrieved.

Lines 165-190: This is where the bulk of the action happens. The script loops through the vCenters, logs on, finds any events related to the shutdown of a VM. If it doesn’t find any it moves on. If it does find some, then it loops through those events, runs a Get-VM for the target of the even. If the VM is still there and powered off, it adds additional information to the existing notes and applies it the the VM. Disconnect and move on or we’re done.