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