VMware Cloud Community
Squigglymonkey
Enthusiast
Enthusiast
Jump to solution

email snapshots

I based a little script to email snapshot information on an older thread. It gets the snapshot information just fine, I can see that there are in fact snapshots, but the email does not actually contain the information. Can I get some help with the $body section as I guess that is where the issue is.
As I am writing this I tried something, I put quotes around the body, and found it starts to "work", a little bit anyway.
With no quotes, nothing is sent, but with quotes like this:

$body = "$snaps | Select VM, Name, Description, Created, SizeGB | Sort SizeGB"
I get a a line in the email of 2/5 snapshots, and then the pipe and select... like this:
VM Snapshot 11%2f30%2f2022, 8:44:21 AM name VM Snapshot 8%252f11%252f2022, 4:21:30 PM | Select VM, Name, Description, Created, SizeGB | Sort SizeGB


#############Securely connect to Vcenters #######################################################
Set-powercliconfiguration -invalidcertificateaction ignore -confirm:$false
$encryptedPassword = Import-Clixml -Path '.\EP.xml'
$decryptedPassword = $encryptedPassword.GetNetworkCredential().Password
Connect-VIServer vcenter1 -User domain\user1 -Password "$decryptedPassword"
Connect-VIServer vcenter2 -User domain\user1 -Password "$decryptedPassword"
############Get snapshots###################################################################
$snaps = Get-VM|Get-Snapshot
############ see snapshots visually#############################################################
$snaps | Select VM, Name, Description, Created, SizeGB | Sort SizeGB|format-table -autosize
########### I see snapshots from above!##########################################################
if ($snaps) {
$smtpSrv = SMTP.server"
$from = ReportCreation@domain.com
$To = 'user1@domain.com'
$Subject = "Snapshot Report $(get-date -f MM-dd-yyyy)"
$body = $snaps | Select VM, Name, Description, Created, SizeGB | Sort SizeGB
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$smtp = new-object Net.Mail.SMTPclient($smtpSrv)
$smtp.send($msg)
} else {
$smtpSrv = "SMTP.server"
$from = ReportCreation@domain.com
$To = 'user1@domain.com'
$Subject = "Snapshot Report $(get-date -f MM-dd-yyyy)"
$body = "There are no Snapshots present."
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$smtp = new-object Net.Mail.SMTPclient($smtpSrv)
$smtp.send($msg)
}
Disconnect-VIServer * -Confirm:$false 
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suggest using the Send-MailMessage cmdlet and place the parameter in splatting format.
Something like this for example

$sMail = @{
    From       = 'ReportCreation@domain.com'
    To         = 'user1@domain.com'
    Subject    = "Snapshot Report $(Get-Date -Format 'MM-dd-yyyy')"
    Body       = ''
    SMTPServer = 'SMTP.server'
    BodyAsHtml = $false
}
if ($snaps) {
    $sMail.Body = $snaps | ConvertTo-Html | Out-String
    $sMail.BodyAsHtml = $true
}
else{
    $sMail.Body = 'There are no snapshots present'
}
Send-MailMessage 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

I suggest using the Send-MailMessage cmdlet and place the parameter in splatting format.
Something like this for example

$sMail = @{
    From       = 'ReportCreation@domain.com'
    To         = 'user1@domain.com'
    Subject    = "Snapshot Report $(Get-Date -Format 'MM-dd-yyyy')"
    Body       = ''
    SMTPServer = 'SMTP.server'
    BodyAsHtml = $false
}
if ($snaps) {
    $sMail.Body = $snaps | ConvertTo-Html | Out-String
    $sMail.BodyAsHtml = $true
}
else{
    $sMail.Body = 'There are no snapshots present'
}
Send-MailMessage 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Squigglymonkey
Enthusiast
Enthusiast
Jump to solution

Getting closer.... This is what is in the email now.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>HTML TABLE</title>

</head><body>

<table>

<colgroup><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/></colgroup>

<tr><th>Description</th><th>Created</th><th>Quiesced</th><th>PowerState</th><th>VM</th><th>VMId</th><th>Parent</th><th>ParentSnapshotId</th><th>ParentSnapshot</th><th>Children</th><th>SizeMB</th><th>SizeGB</th><th>IsCurrent</th><th>IsReplaySupported</th><th>ExtensionData</th><th>Id</th><th>Name</th><th>Uid</th></tr>

<tr><td>name234</td><td>11/30/2022 8:45:04 AM</td><td>False</td><td>PoweredOn</td><td>server5</td><td>VirtualMachine-vm-15326</td><td></td><td></td><td></td><td></td><td>16458.22269916534423828125</td><td>16.072483104653656482696533203</td><td>True</td><td>False</td><td>VMware.Vim.VirtualMachineSnapshotTree</td><td>VirtualMachineSnapshot-snapshot-40527</td><td>VM Snapshot 11%2f30%2f2022, 8:44:21 AM</td><td>/VIServer=user\domain:443/Snapshot=VirtualMachineSnapshot-snapshot-40527/</td></tr>

<tr><td>20221129-dls</td><td>11/29/2022 4:58:08 PM</td><td>False</td><td>PoweredOn</td><td>server4</td><td>VirtualMachine-vm-7587</td><td></td><td></td><td></td><td></td><td>1433.54517650604248046875</td><td>1.3999464614316821098327636719</td><td>True</td><td>False</td><td>VMware.Vim.VirtualMachineSnapshotTree</td><td>VirtualMachineSnapshot-snapshot-40484</td><td>pre-CIS Set 1 GPO</td><td>/VIServer=tss\domain\user:443/Snapshot=VirtualMachineSnapshot-snapshot-40484/</td></tr>

<tr><td>20221123-1332 - dls</td><td>11/23/2022 1:32:24 PM</td><td>False</td><td>PoweredOn</td><td>server2</td><td>VirtualMachine-vm-5632</td><td></td><td></td><td></td><td></td><td>8554.99980831146240234375</td><td>8.354492000304162502288818359</td><td>True</td><td>False</td><td>VMware.Vim.VirtualMachineSnapshotTree</td><td>VirtualMachineSnapshot-snapshot-40211</td><td>name3</td><td>/VIServer=domain\user:443/Snapshot=VirtualMachineSnapshot-snapshot-40211/</td></tr>

<tr><td></td><td>8/11/2022 4:21:37 PM</td><td>False</td><td>PoweredOn</td><td>server11</td><td>VirtualMachine-vm-408</td><td></td><td></td><td></td><td></td><td>13104.53231143951416015625</td><td>12.797394835390150547027587891</td><td>True</td><td>False</td><td>VMware.Vim.VirtualMachineSnapshotTree</td><td>VirtualMachineSnapshot-snapshot-33408</td><td>VM Snapshot 8%252f11%252f2022, 4:21:30 PM</td><td>/VIServer=domain\user@l:443/Snapshot=VirtualMachineSnapshot-snapshot-33408/</td></tr>

</table>

</body></html>
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I forgot to add the BodyAsHtml switch.
I corrected the code above


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Squigglymonkey
Enthusiast
Enthusiast
Jump to solution

I'll try that, I changed the convert to CSV, and it was not as bad....

0 Kudos