VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Send Email After successful

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

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

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.

$dest = "\\backup\DR\$date"

$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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

$dest = "\\backup\DR\$date"

$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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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 Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you separate lines when you do

  Body = $body -join "`r`n"


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

0 Kudos