VMware Cloud Community
lukeglazebrook
Enthusiast
Enthusiast

PowerCLI newbie, how can I get any sort of error/failure in my scripts automatically emailed to me?

So I have stolen a few scripts and tested them all in a lab and everything is great.  However if as an example the script below to there was a problem/error of any kind how could I get the red text that typically appears automatically emailed to me?  Is there a simply bit of script I someone has I could steal Smiley Happy

0 Kudos
1 Reply
linotelera
Hot Shot
Hot Shot

As seen in powershell technet, try to use:

$output = <your script>.ps1 | out-string

Send-MailMessage -body $output

And here in the library see help for this Send-MailMessage command: https://technet.microsoft.com/en-us/library/hh849925.aspx

This command will send all the output... Another way is to improve the code, catching the exception and sending e-mail messages with your useful content (like stacktrace...)

Hope this could find useful

Regards

0 Kudos