0

I'm running a Python script in PowerShell, and I want to send success or failure output to email.

Python example:

import os
print "Running Python script ..."
raise Exception("This Error is coming from Python")

And PowerShell main script:

---some line of code--
    $output = python runThisScript.py    
    Write-Host $output
--- write email---

This works for success case. I get output as success and email is sent. But in case of failure, I just get blank email.

Output in case of failure is something like below:

Running Python script ...
Traceback (most recent call last):
  File "runThisScript.py", line 5, in <module>
    raise Exception("This Error is coming from Python")
Exception: This Error is coming from Python

I would like to get above trace back statements in email. I am not sure how to capture. Thank you in advance for your comments.

  • Since you know Python, why are you not just sending the mail via Python? Anyway, sending mail in [PS has the mail cmdlets](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.2) or [.Net libraries](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.send?view=net-6.0). Just capture your results to a variable or hash table or PSCustom Object, so you and use those strings/properties in the cmdlets. You just add whatever you want to the email body. – postanote Oct 15 '22 at 22:51
  • Python prints error message to stderr, which you can capture in PowerShell via a `2>` redirection - see the linked duplicate for details. – mklement0 Oct 16 '22 at 02:41

0 Answers0