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
# PowerShell script to check for VM snapshot size more than 3TB and send Email report
add-pssnapin VMware.VimAutomation.Core
Connect-VIServer -Server <ip-or-host> -User <username> -Password <password>
# 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>"
# Main section of check
Write-Host "Checking VM for snapshots"
$date = get-date
$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
$filename = "c:\dir\Snapshots_" + $datefile + ".htm"
# Get VM with snapshots
# Note: It may take some time for the Get-VM cmdlet to enumerate VMs in larger environments
$ss = Get-VM Vm Name | Get-Snapshot
Write-Host " Complete " -ForegroundColor Green
Write-Host "Generating VM snapshot Size report"
#$ss | Select-Object VM, Name, Created, SizeGB | ConvertTo-HTML -head $a -body "<H2>VM Snapshot size Report</H2>"| Out-File $filename
$ss | Select-Object VM, Name, Created, SizeGB | ConvertTo-HTML -head $a -body "<H2>VM Snapshot size Report</H2>"| Out-File $filename
Write-Host " Complete " -ForegroundColor Green
Write-Host "Your snapshot report has been saved to:" $filename
$SMTPServer = <smtp_server>
$SMTPPort = 587
$Username = "email@email.com"
#Define the receiver of the report
$to = "recipient.email@email.com"
$subject = "VM Snapshot Size Report"
$body = " VM Snapshot Size Report"
$attachment = new-object Net.Mail.Attachment($filename)
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.from = $username
$message.attachments.add($attachment)
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
write-host "Mail Sent"