# PowerShell script to check for VM snapshots and send Email report add-pssnapin VMware.VimAutomation.Core Connect-VIServer -Server 'vcenter' -User 'domain\user' -Password 'password' # HTML formatting $a = "" $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 + "" # Main section of check Write-Host "Checking VMs for for snapshots" $date = get-date $datefile = get-date -uformat '%m-%d-%Y-%H%M%S' $filename = "x:\dir\Snaps_" + $datefile + ".htm" # Get list of VMs with snapshots # Note: It may take some time for the Get-VM cmdlet to enumerate VMs in larger environments $ss = Get-vm | Get-Snapshot Write-Host " Complete" -ForegroundColor Green Write-Host "Generating VM snapshot report" #$ss | Select-Object vm, name, description, powerstate | ConvertTo-HTML -head $a -body "VM Snapshot Report"| Out-File $filename $ss | Select-Object vm, name, description | ConvertTo-HTML -head $a -body "VM Snapshot Report"| Out-File $filename Write-Host " Complete" -ForegroundColor Green Write-Host "Your snapshot report has been saved to:" $filename # Create mail message $server = "smtp.domain.com" $port = 25 $to = "user@domain.com" $from = "user@domain.com" $subject = "VM Snapshot Report" $body = Get-Content $filename $message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body # Create SMTP client $client = New-Object system.Net.Mail.SmtpClient $server, $port # Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client's behalf. $client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials # Try to send the message try { # Convert body to HTML $message.IsBodyHTML = $true # Uncomment these lines if you want to attach the html file to the email message # $attachment = new-object Net.Mail.Attachment($filename) # $message.attachments.add($attachment) # Send message $client.Send($message) "Message sent successfully" } #Catch error catch { "Exception caught in CreateTestMessage1(): " }
Hi Team,
I need to edit above script to highlight older the 90Days snapshots.
can you help me to add the code in above script.
Thanks in Advance
Keshava Prasad
I think you only need to change line 22 from
$ss = Get-vm | Get-Snapshot
to
$ss = Get-vm | Get-Snapshot | Where-Object { $_.Created -lt (Get-Date).AddDays(-90) }
for the expected result.
I hope above works for you because I can’t test it due to a lack of access to a VMware environment at the moment.
Cheers
Daniel
Thank You Krebs, I will check and update you.
No problem. Please call me Daniel instead
Daniel is correct. That should be all that needs to be modified.
Here is the script refactored to a bit more simple:
Add-PSSnapin VMware.VimAutomation.Core Connect-VIServer -Server 'vcenter' -User 'domain\user' -Password 'password' $css = @" BODY { background-color:white; } TABLE { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse; } TH { border-width: 1px; padding: 5px; border-style: solid; border-color: black; foreground-color: black; background-color: LightBlue; } TD { border-width: 1px; padding: 5px; border-style: solid; border-color: black; foreground-color: black; background-color: white; } "@ $htmlBody = Get-VM | Get-Snapshot | Where-Object { $_.Created -lt (Get-Date).AddDays(-90) } | Select-Object vm, name, description | ConvertTo-HTML -head $css -body "VM Snapshot Report" $mailParams = @{ To = "user@domain.com" From = "user@domain.com" Subject = "VM Snapshot Report" Body = $htmlBody BodyAsHtml = $true SmtpServer = "smtp.domain.com" Port = 25 } Send-MailMessage @mailParams
Edit: Forum is eating some of the HTML. There needs to be STYLE tags in the CSS and H1 tags around the -Body statement
Thank You Daniel, Edmond, Rob.
one more, I need to receive html report as attachment in mail.
Got It All Working Fine.
Thank You For Your Help.