I need to get a report in email when the size of the snapshot of a particular VM is > 3TB and if it is less than the 3TB size then the script has to simply exit and send no email. I have a script below but need to modify it according to the requirement. Can some one please advise and help.
I can task schedule this on daily basis to run and send the report. Thanks
The post where you got this script also has an answer on how to use the Where-clause.
Instead of the creation date you would have to test on the SizeGB property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried but I am not able to figure out the logic or math to get the 3TB size specification. Can some one please help on the logic. Thanks
What did you place in the Where clause?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
here is the logic, not sure if this is correct?
Where {$_.SizeGB -lt (Get-Disk).size > 3000GB/1GB}
Can you try with
Where {$_.SizeGB -gt (3 * 1024)}
You were looking for snapshots greater than 3TB, correct?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes correct LucD. I am looking for snapshots greater than 3TB. I will try this and will update you the status. Thanks
what if you wanted to include the contents of that attachment that was created in the body of the email itself?
Read the content of that file, organise it (don't forget newlines) and assign that to the Body property
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tweaked it slightly to send the contents in the email body rather than an attachment.
# PowerShell script to check for VM snapshots older than 1w and send Email report
Connect-VIServer -Server vcsa.example.com -User "exampleuser" -Password "examplepassword"
$SMTPServer = "smtp.example.com"
$SMTPPort = 25
$SMTPFrom = "vcenter@example.com"
$SMTPTo = "distro@example.com"
# HTML FORMATTING
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"
$a = $a + "</style>"
# GET LIST OF VMs WITH SNAPSHOTS
Write-Host "Checking VMs for for snapshots"
$date = get-date
$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
#$ss = Get-vm | Get-Snapshot
$ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-7)}
Write-Host " Complete " -ForegroundColor Green
Write-Host "Generating VM snapshot report"
$ssreport = $ss | Select-Object vm, name, Created, SizeGB, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report </H2> <p>Snapshots over 1w old</p>"
Write-Host " Complete " -ForegroundColor Green
# CREATE THE EMAIL
$subject = "VM Snapshot Report " + $datefile
$body = $ssreport
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.IsBodyHTML = $true
$message.body = $body
$message.to.add($SMTPTo)
$message.from = $SMTPFrom
# SEND THE EMAIL
Write-Host "Sending Email"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.send($message)
Write-Host " Complete " -ForegroundColor Green