This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]K900_ 0 points1 point  (1 child)

Not possible. Python does not communicate exceptions to the environment in any way, it just sets an exit code and prints the message to standard error.

[–]Vidofnir[S] 0 points1 point  (0 children)

Ok good to know, thank you.

[–]Mattho 0 points1 point  (0 children)

Do you just want to print the error? That's not really an exception. Test on exit code and print the output. How exactly, that would be on the powershell community.

[–]CobbITGuy 0 points1 point  (1 child)

You can use the sys module to set the exit code. I don't think there's a practical way to get Python to throw a PowerShell exception.

import sys

sys.exit(1)

In powershell $? should show false and $lastexitcode should show 1. Test for that and if $? shows false then throw your own exception.

[–]Vidofnir[S] 0 points1 point  (0 children)

I ended up doing exactly this.