Hi,
I would like to know, how to send a email if successful or unsuccessful after the script execution.
Please help
$dest = "\\backup\DR\$date"
$vmhosts = get-vmhost
Foreach ($vmhost in $vmhosts) {
Get-VMHostFirmware -VMHost $vmhost -BackupConfiguration -Destination $dest
}
That's due to the single quotes instead of double quotes.
I also corrected the splatting variable in the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This will catch exceptions and send an email with the result afterwards.
You can of course put more information in the Subject or the Body of the email.
$vmhosts = Get-VMHost
Foreach ($vmhost in $vmhosts) {
try {
Get-VMHostFirmware -VMHost $vmhost -BackupConfiguration -Destination $dest
$subject = 'Success'
}
catch {
$subject = 'Unsuccessful'
}
$sMail = @{}
Send-MailMessage -To user@dmain -From user@domain -Subject $subject -SmtpServer smtp.domain
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
The script works but I am getting multiple successful emails, is there a way to limit to one at the end of the script.
Sure, try like this
$vmhosts = Get-VMHost
$body = @()
Foreach ($vmhost in $vmhosts) {
try {
Get-VMHostFirmware -VMHost $vmhost -BackupConfiguration -Destination $dest
$body += "Success on $($vmHost.Name)"
}
catch {
$body += "Unsuccessful on $($vmHost.Name)"
}
}
$sMail = @{
To = 'user@dmain'
From = 'user@domain'
Subject = $subject
Body = $body | Out-String
SmtpServer = 'smtp.domain'
}
Send-MailMessage @sMail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I am getting the email after I made little change
$sMail = @{}
Send-Mailmessage -From $FromAddress -To $ToAddress -Subject $Subject -Body $($body | Out-String) -SmtpServer $SMTPServer
but, in the email output, I am not able to get the hostname, I am just getting
Success on $($vmHost.Name)
Success on $($vmHost.Name)
Success on $($vmHost.Name)
Success on $($vmHost.Name)
That's due to the single quotes instead of double quotes.
I also corrected the splatting variable in the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
One last question, Hostname are coming in same line in the email, how can I get in the next line for new host
Thanks a lot, that worked
Do you separate lines when you do
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference