PowerCLI Script – Report Virtual Machines with 3 days older VM Snapshots

This could be one among the very few PowerCLI scripts in my blog. I am in learning phase to create a PowerCLI script to automate and get the report of virtual infrastructure within short span of time. This script also created with the help of one of my friend. In this post, I am going to discuss about the PowerCLI script which helps us to report the Virtual Machines with 3 days older VM snapshots and export the output in Excel (.CSV) file. It is always recommended not to keep VM snapshots more than 24- 72 hours. The VM snapshot file continues to grow in size when it is retained for a longer period. This can cause the snapshot storage location to run out of space and impact the system performance. This PowerCLI script could be really useful to identify the Virtual machines with 3 days older snapshot and delete them immediately or raise a concern to the respective team for the snapshot deletion before VM snapshots causes outage to your production infrastructure.

PowerCLI Script – Report Virtual Machines with 3 days older VM Snapshots

#*************************************************************************************************************
# Script Name : VMSnapShot3DaysOld.ps1
# Purpose : Get the report of VMS Snapshot 3 Days Old and save it in CSV file
# Date : 27-10-2016 # - Initial version
# :
# Author : www.VMwareArena.com
#
#*************************************************************************************************************
#
If(!(Get-PSSnapin | Where {$_.Name -Eq "VMware.VimAutomation.Core"}))
{
Add-PSSnapin VMware.VimAutomation.Core
}
$VCServer = Read-Host 'Enter VC Server name'
$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)

$LogFile = "VMSnapShot3DaysOld_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"

Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
$Result = @()

If($? -Eq $True)

{
Write-Host "Connected" -Foregroundcolor "Green"
$Results = @()
$datetime = Get-Date

$threeDaysAgo = (Get-Date).AddDays(-3)
Get-VM | Foreach-Object {
Get-Snapshot -VM $_ | Foreach-Object {
If($_.Created -lt $threeDaysAgo) {
$Result += $_ | Select VM,@{Name="Snapshot Name";E={$_.Name}},@{Name="Date Created";E={$_.Created}}
}
}
}

$Result | Export-Csv -NoTypeInformation $LogFile
}
Else
{
Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red"
}

Disconnect-VIServer * -Confirm:$false
#
#-------------------------------------------------------------------------------------------------------------

How to Execute the Script?

Copy and Paste the above Script and save it in the Notepad. Name the script as VMSnapShot3DaysOld.ps1

  1. Execute the Script in PowerCLI “.\VMSnapShot3DaysOld.ps1″
  2. Input  vCenter Server Name to execute the script to get the report of VM Snapshots older then 3 days
  3. Enter the Username with administrative credentials on vCenter Server
  4. Enter the password for the above entered Username
  5. Hit Enter to execute the script and pull the report.

VM Snapshots older than 3 days

Script Output

Virtual Machines with 3 days older VM Snapshots will be exported and saved in the Microsoft Excel output file with the filename “VMSnapshot3DaysOld_Today_Day.CSV”  under the same directory where the PowerCLI Script “VMSnapshot3Daysold.PS1”  is located.
vm-snapshots-older-than-3-days-1

When you open the output Excel (.CSV) file “VMSnapshot3DaysOld_Today_Day.CSV”, You will be able to get the details of Virtual Machine which has 3 days older VM Snapshots.  This output file contains Virtual Machine Name, Snapshot Name and Date Created.

vm-snapshots-older-than-3-days-2

That’s it. We are done with the PowerCLI script and got the awesome output with the list of Virtual machines which contains 3 Days older VM Snapshot. I hope this script will be informative for you.  Thanks for Reading!!!. Be social and share it in social media, if you feel worth sharing it.

VM Snapshot Related Posts:

VM Snapshot – How to Change default VMware Snapshot location in ESXi 6