Thursday, January 21, 2016

Generate snapshots report of all VMWare virtual machines using PowerCLI

Ok, so I heard that you're tired of going to every virtual machines in your VMWare vCenter and checking for snapshots that need to be removed. VMWare has provided a solution that can ease your pain, It's PowerCLI.

In short, PowerCLI is a Windows PowerShell interface for managing VMware vSphere. It comes with a lot of useful cmdlets that will help you manipulate your VMWare infrastructure smoothly and painlessly (sort of) including automating tasks.

Here is how I can generate a snapshots report of all the virtual machines in my VMWare 6 environment:

1. In a Windows Server 2008 R2 SP1 (or Windows 7 and above), install these dependencies:
  • vSphere client v6 for windows: download here.
  • Microsoft Management Framework 3.0: download here (for Windows Server 2008 R2 SP1, I will download Windows6.1-KB2506143-x64.msu package)
2. Download and install PowerCLI:

Download the latest PowerCLI here.
It's PowerCLI 6 R3 at the time I write this article.

3. Open PowerCLI and set the ExecutionPolicy to RemoteSigned:

Set-ExecutionPolicy RemoteSigned

For more information about ExecutionPolicy, please read this article: here.

4. Download this script and put it into C:\report_snapshots.ps1

5. Modify the following information in the script:

$Key = [byte]57,86,31,23,73,75,85,52,77,33,0,21,56,76,99,14
# these following information you can pass to the running script but I refer put them directly into the file
    [string]$VIServer = "Your vCenter Server",
    [string]$Admin = "youradminaccount",
    [string]$PathToCredentials  = "/path/to/your/credential/file",
    [string]$PathToReport = "/path/to/report/file.html",

    [string]$From = "Notification Email <>",
    [string]$SMTPServer = "",
    [string]$SMTPPort = "587",
    [string]$SMTPPasswd = "yourGMailPass",
    [string]$SMTPUser = "yourGMailUser"
$TO_ADDR = "", "", ""

6. In PowerCLI unblock the downloaded script with the following command:

Unblock-File C:\report_snapshots.ps1

7. Run the script the first time in PowerCLI to create the credential file:


It will open a dialog and ask for your admin credential.

8. In Task Scheduler, create a task to run this command at every Thursday at 1AM (or anytime you want):

powershell -file "C:\report_snapshots.ps1"

9. Enjoy