Redirect two or more Powershell streams other than the output stream to the same file

In Powershell, we can combine a standard output stream with any other stream, and then redirect (write) the result to the same file.

Examples:

Powershell -File "C:\myscript.ps1" 2>&1> "C:\log.txt" Powershell -File "C:\myscript.ps1" 3>&2>&1> "C:\log.txt" 

Suppose I use Write-Error and Write-Warning entries in myscript.ps1 and I want to write only errors and warnings to the same file.

Note: if I'm not mistaken, 1 is the output stream, 2 is the error stream, and 3 is the warning stream.

My first logical attempt was to use 3> & 2> - if a combination of 1 and 2 works, why not 3 and 2? See below:

 Powershell -File "C:\myscript.ps1" 3>&2> "C:\log.txt" 

However, 3> & 2> does not work as a valid redirection operator.

I could try:

Powershell -File "C:\myscript.ps1" 3>"C:\warninglog.txt" 2>"C:\errorlog.txt"
but I really want to write to the same file.

If I try to run:

 Powershell -File "C:\myscript.ps1" 3>"C:\log.txt" 2>"C:\log.txt" 

it seems that the error stream (2) is never written to log.txt because the file is blocked by the signal stream.

Is there a way to combine two (or more) output streams into one stream and redirect the result to the same file?

+6
source share
2 answers

Multi-page, warning, and debugging streams merge into STDOUT when running PowerShell scripts through

 powershell -File "C:\myscript.ps1" 

so that you cannot redirect them separately. Only the error stream is different, because it seems to be suitable for both STDOUT and STDERR, where it can be redirected to 1> , as well as 2> .

Demonstration:

  C: \> type test.ps1
 $ DebugPreference = "Continue"
 $ VerbosePreference = "Continue"
 Write-Output "Output message"
 Write-Error "Error message"
 Write-Verbose "Verbose message"
 Write-Warning "Warning message"
 Write-Debug "Debug message"

 C: \> powershell -File. \ Test.ps1
 Output message
 C: \ test.ps1: Error message
     + CategoryInfo: NotSpecified: (:) [Write-Error], WriteErrorException
     + FullyQualifiedErrorId: Microsoft.PowerShell.Commands.WriteErrorException, test.ps1

 VERBOSE: Verbose message
 WARNING: Warning message
 DEBUG: Debug message

 C: \> powershell -File. \ Test.ps1 2> nul 3> nul 4> nul 5> nul
 Output message
 VERBOSE: Verbose message
 WARNING: Warning message
 DEBUG: Debug message

 C: \> powershell -File. \ Test.ps1 1> nul

 C: \> _

If you want to redirect the flow of details, warnings, or debugging separately, you must use -Command instead of -Command and do the redirection in PowerShell:

  C: \> powershell -Command ". \ Test.ps1 2> $ null 3> $ null 5> $ null"
 Output message
 VERBOSE: Verbose message

However, while in CMD , you can redirect any descriptor to any other descriptor ( 3>&2 , 1>&5 , ...), PowerShell redirection only supports redirecting a file ( 3>C:\out.txt ) or the success output stream ( 3>&1 ). Attempting to redirect to any other thread will throw an error:

  C: \> powershell -Command ". \ Test.ps1 2> out.txt 3> & 2"
 At line: 1 char: 22
 +. \ test.ps1 2> out.txt 3> & 2
 + ~~~~
 The '3> & 2' operator is reserved for future use.
     + CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
     + FullyQualifiedErrorId: RedirectionNotSupported

since it redirects different streams to the same file:

  C: \> powershell -Command ". \ Test.ps1 2> out.txt 3 >> out.txt"
 out-file: The process cannot access the file 'C: \ out.txt' because it is being 
  used by another process.
 At line: 1 char: 1
 +. \ test.ps1 2> out.txt 3 >> out.txt
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo: OpenError: (:) [Out-File], IOException
     + FullyQualifiedErrorId: FileOpenFailure, Microsoft.PowerShell.Commands.OutFileCommand

If you want to merge warning and success, you can do something like this:

 powershell -Command ".\test.ps1 >out.txt 3>&1 2>error.log" 

or like this:

 powershell -Command ".\test.ps1 >out.txt 3>&1 2>&1" 

or (redirect all threads) as follows:

 powershell -Command ".\test.ps1 *>out.txt" 

Otherwise, the only option I see is redirection to different files:

 powershell -Command ".\test.ps1 3>warning.log 2>error.log" 
+4
source

You can redirect the error and warning stream to the output stream separately, instead

  3>&2>&1 

you have to do it

  3>&1 2>&1 

Adapted from Understanding Streams

 function Write-Messages { Write-Host "Host message" Write-Output "Output message" Write-Verbose "Verbose message" Write-Warning "Warning message" Write-Error "Error message" Write-Debug "Debug message" } $DebugPreference = "Continue" $VerbosePreference = "Continue" Write-Messages 3>&1 2>&1 > $env:TEMP\test.txt 

For your example, it will become

 Powershell -File "C:\myscript.ps1" 3>&1 2>&1> "C:\log.txt" 

or you can redirect all streams to your file

 Powershell -File "C:\myscript.ps1" *> "C:\log.txt" 
+1
source

Source: https://habr.com/ru/post/989316/


All Articles