Sunday, July 17, 2022

[FIXED] How to separate warning message from stdout/result in powershell redirection?

Issue

I'm trying to write a simple test script in PowerShell to separate warning message from stdout/result in order to get two separate text files, one for warnings and one for standard results in PowerShell Redirection.

Here is my simple output.ps1 script:

Write-Output "This is result"
Write-Warning "This is warning"

I'm trying to run output.ps1 using a .bat file using below command:

PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt
pause

But my problem is warning.txt is completely empty, while both – warning and result – are written to results.txt.

This is result
WARNING: This is warning

What I am missing in PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt to separate warnings and errors into two files?


Edit: Modified to see standard error, 2> is working.

Even if I redirect standard error to separate file using 2>error.txt by using

PowerShell.exe -Command "& '%~dp0output.ps1'" 2>error.txt 3>warning.txt >results.txt

and if I modify output.ps1 to generate an error in the following manner

Write-Output "This is result"
Write-Warning "This is warning""
This will generate error because this is not a ps command.

it will generate the error as expected to a file error.txt with results.txt where results.txt is the combination of warning and stdout/result. warning.txt is still empty. I still can't figure out why PowerShell warnings are not filtered and redirected properly to a separate file.

error.txt:

this : The term 'this' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\Users\Dell\Desktop\output.ps1:3 char:1
+ This will generate error because this is not a ps command.
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (this:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

results.txt:

This is result
WARNING: This is warning

I'm using Windows 10 and PowerShell Version 5.1.14393.1358.


Solution

Real problem, in the below example, is, When you use powershell.exe to execute a ps1 file verbose, warning, and debug streams are merged into STDOUT) -Source. (---resulting a blank warning.txt)

    PowerShell.exe -Command "& '%~dp0output.ps1'" 3>warning.txt >results.txt  

To circumvent this limitation in powershell.exe, there is a workaround for above example using extra intermediate ps1 file. You can use two powershell files and a bat file and get three separate files for error/warning/results. (2>error.txt 3>warning.txt >results.txt)

Have the same content for output.ps1 with below content.

Write-Output "This is result"
Write-Warning "This is warning""
This will generate error because this is not a ps command.

Then create another powershell file. (I will call it newout.ps1) and newout.ps1 can run output.ps1 with below content.

.\output.ps1 2>error.txt 3>warning.txt >results.txt

Finally your bat file can run newout.ps11 instead of output.ps1 in this manner.

PowerShell.exe -Command "& '%~dp0newout.ps1'"

Which gives you three separate files with desired results.



Answered By - Aravinda
Answer Checked By - Marie Seifert (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.