在 Powershell 中,我们可以将标准输出流与任何其他流结合起来,然后将结果重定向(写入)到同一个文件中。
例子:
Powershell -File "C:\myscript.ps1" 2>&1> "C:\log.txt"
Powershell -File "C:\myscript.ps1" 3>&2>&1> "C:\log.txt"
假设我在 myscript.ps1 中使用 Write-Error 和 Write-Warning 语句,并且我只想将错误和警告写入同一个文件。
注意:如果我没记错的话,1 是输出流,2 是错误流,3 是警告流。
我的第一个合乎逻辑的尝试是使用 3>&2> - 如果组合 1 和 2 有效,为什么 3 和 2 不行?见下文:
Powershell -File "C:\myscript.ps1" 3>&2> "C:\log.txt"
但是, 3>&2> 不能用作有效的重定向运算符。
我可以绕过尝试:
Powershell -File "C:\myscript.ps1" 3>"C:\warninglog.txt" 2>"C:\errorlog.txt"
但我真的想写入同一个文件。如果我尝试运行:
Powershell -File "C:\myscript.ps1" 3>"C:\log.txt" 2>"C:\log.txt"
似乎错误流 (2) 永远不会写入 log.txt,因为文件被警告流锁定。
有没有办法将两个(或更多)输出流组合成一个流并将结果重定向到同一个文件?
最佳答案
当您通过以下方式运行 PowerShell 脚本时,详细、警告和调试流将合并到 STDOUT 中
powershell -File "C:\myscript.ps1"
所以你不能再单独重定向它们了。只有错误流不同,因为它似乎同时进入 STDOUT 和 STDERR,在那里它可以通过
1>
重定向。以及 2>
.示范:
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 verbose, warning, or debug stream separately you must use -Command
instead of -File
and do the redirection within 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 handle to any other handle (3>&2
, 1>&5
, ...), PowerShell redirection only supports redirection to either a file (3>C:\out.txt
) or the success output stream (3>&1
). Trying to redirect to any other stream 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
as will redirecting 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 merging warning and success output is an option for you, you could do something like this:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>error.log"
或者像这样:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>&1"
或(重定向所有流)像这样:
powershell -Command ".\test.ps1 *>out.txt"
否则我看到的唯一选择是重定向到不同的文件:
powershell -Command ".\test.ps1 3>warning.log 2>error.log"
关于powershell - 将输出流以外的两个或多个 Powershell 流重定向到同一文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30906329/