I am trying to write a PowerCLI script to generate a report of all snapshots that are older than 7 days and email it to me every morning. I have this part working which actually generates the info I need:
Get-VM | Get-Snapshot | Where { $_.Name.Length -gt 0 } | Where {$_.Created -lt ((Get-Date).AddDays(-7))} | Select VM,Name,Description,Created | Sort Created
I am a complete newbie to scripting. I found an almost similar script here http://www.virtu-al.net/2009/06/22/powercli-snapreminder which I began modifying to suit my needs. But I am stuck now because I get an email but it does not contain the data that was generated, just the static text. Here is the script below. Can anyone point me in the right direction on where I'm going?
Thanks
-
$smtpServer = "smtpserver"
$MailFrom = "me@domain.com"
$MailTo = "me@domain.com"
$VISRV = "virtualcenterserver"
Function SnapMail ($Mailto, $SnapshotInfo)
{
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $MailFrom
$msg.To.Add($Mailto)
$msg.Subject = "Snapshot Reminder"
$MailText = @"
This is a reminder email of all the open snaphots greater than 7 days old: $SnapshotInfo
"@
$msg.Body = $MailText
$smtp.Send($msg)
}
Connect-VIServer $VISRV
$SnapshotInfo = Get-VM | Get-Snapshot | Where { $_.Name.Length -gt 0 } | Where {$_.Created -lt ((Get-Date).AddDays(-7))} | Select VM,Name,Description,Created | Sort Created
SnapMail $mailto $SnapshotInfo
I'm not sure why you use the
But if you pipe the array to a string, you can just append it to your body text.
$smtpServer = "smtpserver" $MailFrom = "me@domain.com" $MailTo = "me@domain.com" $VISRV = "virtualcenterserver" function SnapMail ($Mailto, $SnapshotInfo) { $msg = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient($smtpServer) $msg.From = $MailFrom $msg.To.Add($Mailto) $msg.Subject = "Snapshot Reminder" $MailText = "This is a reminder email of all the open snaphots greater than 7 days old:" $MailText += ($SnapshotInfo | Out-String) $msg.Body = $MailText $smtp.Send($msg) } Connect-VIServer $VISRV $SnapshotInfo = Get-VM | Get-Snapshot | where { $_.Name.Length -gt 0 -and $_.Created -lt ((Get-Date).AddDays(-7))} | Select VM,Name,Description,Created | Sort Created SnapMail $mailto $SnapshotInfo
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference