Create VMware snapshots report

PowerShellVMware

Written by:

Konichiwa Yujin,
Sorry for not posting for so long, I been to Japan for a work trip. While Japan is amazing, the state of the ESXi in the office wasn’t so great. The ESXi crushed, and after investigation, we notice that he had no free space on HDD’s, which was weird. we have 100TB, and only 4 VM’s. the first suspect? Snapshots!

Taking a break in Tokyo Parks

Beautiful building in Tokyo Park

Snapshots are great, but they can be your worst enemy if you don’t use them correctly. The “issue” with snapshots is that they save your delta’s. This means that every change happening on the VM is “recorded” on the snapshot, “another change to revert”. as the time pass, the snapshot will grow and grow with the limit set to your free space on HDD’s.

It easy to set up a snapshot, but it hard to track them, especially when you have a big environment and many “hands” on the system. but we have an easy solution, Powershell. We can create a report, that give us all the information needed to manage the snapshots, so let get started!

Prerequisites

Before we can start, please make sure you have the following:

  • Access to the VCenter or ESXi (I encourage to create a service account with relevant permissions)
  • A server or PC with PowerCLI installed.

Step 1 – install PowerCLI

Installing PowerCLI is easy, we can get it directly from the Powershell Gallery:

Powershell Window with the cmdlet

Easy as that

Step 2 – Connect to the VCenter or ESXi

Before we can run any command in PowerCLI, we need to connect the VCenter or ESXi. To do so we need to use the Connect-VIServer cmdlet:

If you have any issue with the certificate, you will get a notice now. it will be in yellow which state warning and not an error. I advise you to fix it, but it’s not necessary for our cause.

A little tip, keeping the password in plain text in scripts is not so safe, so it’s better to secure it a little bit. I don’t like this method either, but this is much safer. First, we will convert the password to secure string, and save it to a file (I recommend changing the ACL for the file also).

Now we can connect to the VCenter or ESXi more secure:

Please, do yourself a favor and use this method in you script.

Step 3 – Get all the Snapshots

Snapshots are unique to a specific VM, so first we need to get all the VM’s on the Vcenter or ESXi, to do it we will use the Get-VM cmdlet, then for each VM we will get the snapshots, we will use the Get-Snapshot cmdlet.

I also took only the properties that I needed.

Step 4 – Process the data

Now, I’ll be honest with you, you can pipe this to Format-List and you will have a great report to start with. But, I always love to make my reports better looking. So, there are a few things we need to process first.

Snapshot Size:

I will create a simple function that will process the snapshot size (this why I took both SizeMB and SizeGB) and return a nice value.

Snapshot Created Date

I also want to add some colors to my reports, Convertto-HTML cmdlet doesn’t support conditional formation, so I will process the Created date to style it manually, I decided that snapshots not older than 1 week will be green, snapshots between 1 to 2 weeks will be yellow, and snapshots older than 2 weeks will be red.

then, I created another function that will get the HTML data and change the style

This is ungentle solution, If you have a better idea, I would love to here!

Step 3 – Create the report

Now that we have all the data we need, we can create the report. We will create an HTML report, so it will be easy to use it in different scenarios.

Header and style:

first we will handle the HTML header and set basic style to our page

Create title to our table:

To add a title before our table:

Convert to HTML

We did everything needed to create our report, now let’s wrap up everything

Format the HTML with conditional formatting:

Step 4 – Publish the report

now that we have our report ready, we need to decide how to publish it. either by mail or as an HTML page, you choose.

Report of the snapshots we built

Our Report

Create HTML file:

If you want to create an HTML file, just pipe your report to out-file

Send report over mail:

To send the report over mail, we can use the Send-MailMessage cmdlet

Step 5 – Disconnect from server

It’s very important to disconnect from the server when we finish. Two main reasons, one for security reasons, and two for session limit on VMWare, the limit are set to 100 concurrent sessions, both Idle and active sessions count. we can disconnect from the server with the Disconnect-VIServer

Conclusion

Snapshots are great! but they can do harm! We learned how to generate snapshot reports, to see how old the snapshots are and how much storage they take. Decide the frequency you want to run this report, and set a scheduled task, but remember to be secure! I will also include the full script if you want, you can download it here

See you next time!

Comments are closed.