将输出流以外的两个或更多Powershell流重定向到同一文件 [英] Redirect two or more Powershell streams other than output stream to the same file

查看:144
本文介绍了将输出流以外的两个或更多Powershell流重定向到同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Powershell中,我们可以将标准输出流与任何其他流组合在一起,然后将结果重定向(写入)到同一文件.

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

示例:

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 语句,并且我只想将错误和警告写到同一文件中.

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

注意:如果我没记错的话,1是输出流,2是错误流,3是警告流.

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

然后我的第一个逻辑尝试是使用 3>& 2> -如果将1和2结合使用,为什么3和2不会呢?见下文:

My first logical try was then to use 3>&2> - if combining 1 and 2 works, why would not 3 and 2 ? See below:

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

但是, 3>& 2> 不能作为有效的重定向运算符.

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

我可以尝试:

Powershell -File "C:\myscript.ps1" 3>"C:\warninglog.txt" 2>"C:\errorlog.txt"
但是我真的很想写相同的文件.

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

如果我尝试运行:

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

似乎错误流(2)从未写入log.txt,因为文件已被警告流锁定.

it seems the error stream (2) never gets written to log.txt because file is locked by the warning stream.

是否可以将两个(或多个)输出流合并为一个流并将结果重定向到同一文件?

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

推荐答案

当您通过以下方式运行PowerShell脚本时,详细,警告和调试流将合并到STDOUT中:

The verbose, warning, and debug streams are merged into STDOUT when you run PowerShell scripts via

powershell -File "C:\myscript.ps1"

,因此您无法再分别重定向它们.只有错误流是不同的,因为它似乎同时到达STDOUT和STDERR,可以在其中通过1>2>重定向.

so you can't redirect them separately anymore. Only the error stream is different, since it seems to go to both STDOUT and STDERR, where it can be redirected by 1> as well as 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:\>_

如果要分别重定向详细,警告或调试流,则必须使用-Command而不是-File并在PowerShell中进行重定向:

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

但是,在 CMD ,您可以将任何句柄重定向到任何其他句柄(3>&21>&5,...),

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"

或(重定向所有流),如下所示:

or (redirecting all streams) like this:

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

否则,我看到的唯一选择是重定向到其他文件:

Otherwise the only option I see is to redirect to different files:

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

这篇关于将输出流以外的两个或更多Powershell流重定向到同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆