title: Returning errors from Python to PowerShell?
body: <p>I'm running multiple Python scripts in PowerShell, and I want at specific condition in Python script to notify PowerShell main script. Like throwing an error from Python and catching it with PowerShell.</p>
  
  <p>Python example:</p>
  
  <pre><code>import os
  print "Running Python script ..."
  raise Exception("This Error is coming from Python")
  </code></pre>
  
  <p>And PowerShell main script:</p>
  
  <pre><code>$ErrorActionPreference = 'Stop'
  try {
      python runThisScript.py -ErrorAction Stop
      # Write-Error -Message 'Oh no! An error!'
      Write-Host "What is that?"
  } catch {
      Write-Host $_.Exception.Message -ForegroundColor Green
      throw [System.Exception] "Python scrip XXX failed"
  } finally {
      Write-Host "This is the final block ..."
  }
  </code></pre>
  
  <p>But this does not work. It will show:</p>
  
  <pre><code>Running Python script ...
  Traceback (most recent call last):
    File "runThisScript.py", line 5, in &lt;module&gt;
      raise Exception("This Error is coming from Python")
  Exception: This Error is coming from Python
  What is that?
  This is the final block ...
  </code></pre>
  
  <p>If I un-comment the line <code>Write-Error -Message 'Oh no! An error!'</code> this is what I want to see (the error is catch and handle by PowerShell).</p>
  
  <pre><code>Oh no! An error!
  This is the final block ...
  Python scrip XXX failed
  At C:\Automation\general_stuff\tc\mainTestRunner.ps1:12 char:5
  +     throw [System.Exception] "Python scrip XXX failed"
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], Exception
      + FullyQualifiedErrorId : Python scrip XXX failed
  </code></pre>
  
  <p>Thank you in advance for your comments.</p>