Recently I was given the task of creating and setting up an automated snapshot report for all our virtual machines hosted in our VMware environment which would run daily. I settled on using PowerShell and specifically, the VMware PowerCLI module to achieve this.
I needed to get the name of the VM, the snapshot description, snapshot size, date created and the username of whom created it.
Firstly, you’re going to want to make sure that you have downloaded and installed the PowerCLI module. This can be obtained from VMware here: https://code.vmware.com/web/tool/11.4.0/vmware-powercli
Now that we have this installed, we can begin writing our script.
Connect to vCenter
To start the script, we want to import the PowerCLI module and then setup our connection to vCenter.
Here we can see that we’re importing the module first. For this example, as we will be setting this up as an automated task, we need to store the password somewhere. I have opted to store the password in a text file using the ConvertFrom-SecureString to turn convert it to an encrypted format which cannot be easily read. Obviously, this isn’t ideal so I would avoid doing this if you don’t need to automate the script.
Once we have our set of credentials, we can go ahead and connect to our vCenter server. This can be done by using the Connect-VIServer cmdlet and the 2 switches which let us specify the server name and which credentials to use.
Get snapshot list
Getting the list of snapshots is simple and can be done with a single command.
Part of my requirements was to get the username of who created each snapshot. Unfortunately, the above command is unable to get this detail. We can get this though by creating a simple loop.
To get the username of who created each snapshot, we need to look at the events which have occurred on a vCenter server. We are then looking for events where the event description is “Task: created virtual machine snapshot”. Once found we can then get the username of the user who initiated this event.
Create the report
Now that we have all the data needed, we can create the report. As I have a background in web design, I’ll be creating the report as an HTML document. My solution may not be the most elegant but it works for what I needed.
I decided to put all my HTML code into a variable. Once completed this variable can then be outputted to a HTML file.
This sets up the start of the HTML document and the header. I have opted to use a second variable to store the CSS styling for the report. It then adds the start of the table for the report it’s headings.
I have next modified the loop from earlier with an additional line in both sections of the If statement.
This will create a new line for each snapshot and add in the values for the various pieces of data which I want to report on.
Finally, once we have all the data, we can close out the table, HTML and disconnect from the vCenter server.
The final line of this script then exports the report to an HTML file.
I made a couple of small tweaks to the completed script to add things such as styling for the report and to highlight some known snapshots which should not be touched and also to them email the report to myself so I don’t have to go and collect it.
You can view my final script at https://pastebin.com/PRQPQAHe.