Creating a scheduled VMWare Snapshot Report (PowerShell)

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.

Import-Module -Name VMWare.PowerCLI



$PasswordFile = "<Path>\PasswordFile.txt"

$SecurePassword = Get-Content $PasswordFile | ConvertTo-SecureString

$Credentials = New-Object System.Management.Automation.PSCredential ("Username",$SecurePassword)



Connect-VIServer -Server myvc01 -Credential $Credentials

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.

$Snap = Get-VM | Get-Snapshot | Select-Object Description, Created, VM, SizeMB, SizeGB

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.

foreach ($snap in Get-VM | Get-Snapshot)

{

 $snapevent = Get-VIEvent -Entity $Snap.VM -Types Info -Finish $Snap.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'}

  if ($snapevent -ne $null)

   {

    $sVM = $Snap.VM

    $sDesc = $Snap.Description

    $sDate = $Snap.Created

    $sUser = $snapevent.UserName

   } else {

    $sVM = $Snap.VM

    $sDesc = $Snap.Description

    $sDate = $Snap.Created

    $sUser = "Unknown User"

   }

}

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.

$report = $report = "<!DOCTYPE html><html><head>$Styles<title>VMWare Snapshot Report</title></head><body>"



$report += "<h1>Snapshot Report for $date</h1><table><th>VM Name</th><th>Description</th><th>Date Created</th><th>Created By</th>"

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.

$report += "<tr><td>$sVM</td><td>$sDesc</td><td>$sDate</td><td>$sUser</td></tr>"

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.

$report += "</table>"

$report += "</body></html>"

Disconnect-VIServer -Server ServerName -Confirm:$false



$report | Out-File "C:\Reports\Snapshots\Snapshot Report.html"

The final line of this script then exports the report to an HTML file.

Final Script

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.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on LinkedIn
Share on email
Share through Email