Monday, April 4, 2016

PowerShell Script to Email List of VMware Snapshots

     During scheduled maintenance is a good time to delete any snapshots in vCenter.  VMware recommends only keeping snapshots for up to 72 hours. ("Use no single snapshot for more than 24-72 hours." From: https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1025279

     Here is a PowerShell script that will gather all snapshots in vCenter and email a list to recipients:

Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vcenter.server.name

$style = "<style>"
$style += "BODY{font-family: Arial; font-size: 10pt;}"
$style += "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style += "TH{border: 1px solid black; background: #dddddd; padding: 5px;}"
$style += "TD{border: 1px solid black; padding: 5px;}"
$style += "</style>"

$SendEmail = @{
SMTPServer = "IP or Name of SMTP Server"
From = Do_Not_Reply@domain.com
To = "user1@domain.com","user2@domain.com"
 Subject = "Current Snapshots"
}

$EmailBody = Get-VM | %{Get-Snapshot $_} | select @{N='Server Name';E={$_.VM}},@{N='Snapshot Name';E={$_.Name}},@{N='Snapshot Creation Date';E={$_.Created}} |
sort 'Server Name' | ConvertTo-Html -Head $style -Body "<H2>Testing script to email current list of snapshots that should be deleted.</H2>" | Out-String

Send-MailMessage @SendEmail -Body $EmailBody -BodyAsHtml

     The $style options included in the script make the email look better.  Here is a snippet of what an email will look like:


     As you can see there a couple snapshots hanging out longer than the 72 hour recommendation.  Those two machines are typically not running and only used for creating AppV packages.  The appropriate administrator will take care of those during the upcoming maintenance night...hopefully.

     This script could be set up to run as a scheduled task.  That way it will run without user intervention which will reduce steps required prior to maintenance night. 

-IT Bits




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.