Tuesday, March 11, 2014

VMware Snapshot Report / Email

Oh VMware, why do you have to break things and not fix them - like Snapshot Alarms in v5.x and up....?

If you want to get a daily email of snapshots, use this code.  I stole most of it from here, but made it better.

This report will send you an HTML-formatted email, with a table that looks like this:

Virtual Machine
Snapshot
Size in GB
Creation Time
Description
SERVER1
Snapshot Name #1
1.337
03/01/2014 13:37:00
I'm a n00b and don't delete snaps
SERVER2
Snapshot Name #2
13.37
09/31/2017 21:33:44
Oops I forgot to delete this one too

Make a new text file:

C:\Scripts\getsnaps.ps1

Put this in it.  Obviously change the first six variables to match your environment.


#Add VMware 
add-pssnapin VMware.VimAutomation.Core

#Set Variables
$vcenterserver = 'vcenter.domain.tld'
$user = 'serviceaccount@domain.tld'
$password = 'SuperSecretP@ssw0rd'
$toAddr = 'youradmindistro@domain.com'
$fromAddr = 'vCenterSnaps@domain.tld'
$smtpsrv = 'smtp.domain.tld'
$date = Get-Date

#Connect to vCenter
Connect-VIServer -server $vcenterserver -user $user -password $password

$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb)

# Make sure the body isn't empty. If it is, skip this part and exit the script.
if($VMsWithSnaps -ne $null){
    $body = @("
        <center><table border=1 width=50 % cellspacing=0 cellpadding=8 bgcolor=Black cols=3>
        <tr bgcolor=White><td>Virtual Machine</td><td>Snapshot</td><td>Size in MB</td><td>Creation Time</td></tr>")


    $i = 0
    do {
        if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++}
        else {$body += "<tr bgcolor=#EFEFEF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++}
    }
    while ($VMsWithSnaps[$i] -ne $null)

    $body += "</table></center>"
    # Send email alerting recipients about snapshots.
    if($attachmentPref){
        $VMsWithSnaps | Export-CSV "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
        Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on $vcenterserver"  -Body "$body" -Attachments "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv" -SmtpServer "$smtpsrv" -BodyAsHtml
        Remove-Item "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
    }
    Else{
        Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on $vcenterserver" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
    }
}
else{
    Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject " No VMware Snapshots found on $vcenterserver" -Body "" -SmtpServer "$smtpsrv"
}

#Disconnect from vCenter
Disconnect-VIServer -Server $vcenterserver -Confirm:$false

Now, add a scheduled task on a server that has powerCLI installed.

Run this program
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

This will be your argument.

c:\scripts\getsnaps.ps1

Set it to run as often as you like, it takes a few minutes to run depending on the size of your environment.

Don't forget to set credentials in the task scheduler :)



No comments:

Post a Comment

Featured Post

Remove 3D Objects and other annoying folders on Windows 10

 Microsoft just keeps adding more crap to clutter up the navigation in Windows 10.  Seriously, who needs a 3D Objects folder?  The tiny perc...