exception - PowerShell: fatal errors don't get sent to STDERR, how to log them? -


i have powershell script on desktop:

write-output "some output" write-error "non-fatal error" $erroractionpreference = "stop" # further errors should treated fatal write-output "some more output" write-error "fatal error" 

i call way powershell.exe:

ps c:\users\me\desktop> .\test.ps1 2> err.txt 1> out.txt 

output:

c:\users\vmadmin\desktop\test.ps1 : fatal error @ line:1 char:1 + .\test.ps1 2> err.txt 1> out.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : notspecified: (:) [write-error], writeerrorexception     + fullyqualifiederrorid : microsoft.powershell.commands.writeerrorexception,test.ps1 

out.txt:

some output more output 

err.txt:

c:\users\vmadmin\desktop\test.ps1 : non-fatal error @ line:1 char:1 + .\test.ps1 2> err.txt 1> out.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : notspecified: (:) [write-error], writeerrorexception     + fullyqualifiederrorid : microsoft.powershell.commands.writeerrorexception,test.ps1 

i fatal error logged err.txt. how can accomplish that?

powershell has 2 different channels error-handling: first stderr, non-fatal errors written. second exception mechanism, similar see in programming languages c# or java.

here's trick: error in either 1 channel or other, never both. means fatal errors not go through stderr!

here's workaround: wrap powershell calls in cmd call. since cmd language not have exceptions, powershell exceptions go through piped stderr.

example usage:

ps c:\users\me\desktop> cmd.exe /c "powershell.exe .\test.ps1" 2> err.txt 1> out.txt 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -