Friday, February 3, 2017

PowerCLI script to get list of Snapshots older then x days as a mail atachment

This script would sent you a list of Snapshots older than x days(replace x with the the number of days as required) in html table format as a mail attachment.

To run this script, copy the content of it to notepad file and save it as file_name.ps1 now you can run it from any system which have Windows Powershell and VMware PowerCLI installed.

<# ==================================================================
Title: Get_Snapshot_list.ps1
Description: List Snapshots older than x days and then email it as an attachment
Requirements: Windows Powershell with PowerCLI installed
Usage: .\Get_Snapshot_list.ps1
==================================================================== #>
Add-PSSnapin VMware.VimAutomation.Core

$viserver = Connect-VIServer vCenter_Server_Name_or_IP -user "userName" -password "your vCenter Password"

$SnapshotReport = Get-VM  | Get-Snapshot | where { $_.Created -lt (Get-Date).AddDays(-15)} | select VM, Name, Powerstate, SizeMB, Created | ConvertTo-Html | Set-Content SnapshotReport.html

$EmailFrom = "from_mail_id"

$EmailTo = "to_mail_id"

$subject = "Snapshots older then x days"

$body = "Please find the enclosed snpshot report: SnapshotReport.html"

$smtp = "smtp_server_address"

$viserver; $SnapshotReport; Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $subject -Body $body -SmtpServer $SMTP -Attachments "snapshotreport.html" -Priority "High"

Remove-Item SnapshotReport.html

Disconnect-VIServer "vCenter_server_name_or_IP" -Confirm:$false
By scheduling this script using task scheduler we can get the snapshot report on weekly basis. To schedule this script, please check the following post, How to run Powershell script as a scheduled task.

As here the login credentials are written in plain text so if are planing to schedule this script then instead of using login credentials in script, it would be better to schedule it using credentials which already have login rights on vCenter and as PowerShell supports passthrough authentication so that will also work.

That's it... :)


No comments:

Post a Comment