简体   繁体   English

powershell 2.0命令行重定向

[英]powershell 2.0 command line redirection

I'm looking for an explanation of the following discrepancy: 我正在寻找以下差异的解释:

Given the following powershell script foo.ps1 : 给出以下powershell脚本foo.ps1

write-host "normal"
write-error "error"
write-host "yay"

Running it with 运行它

C:\\>powershell .\\foo.ps1 > out.txt 2>&1

Produces: 生产:

normal
C:\foo.ps1 : error
At line:1 char:10
+ .\foo.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1

Write-Host : The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 co
de or another FileStream. This may cause data loss.
At C:\foo.ps1:3 char:11
+ write-host <<<<  "yay"
    + CategoryInfo          : NotSpecified: (:) [Write-Host], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.WriteHostCommand

But running with: 但运行:

C:\\>powershell .\\foo.ps1 2>&1 > out.txt

Produces (correctly): 产生(正确):

normal
C:\foo.ps1 : error
At line:1 char:10
+ .\foo.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1

yay

I almost resolved myself into thinking that the order of the redirection mattered in Windows, however all of the examples in the TechNet usage page for command redirection show the file redirection preceding the stderr redirection. 我几乎认为自己认为重定向的顺序在Windows中很重要,但是TechNet用法页面中用于命令重定向的所有示例都显示了stderr重定向之前的文件重定向。

Can someone please explain this to me? 有人可以向我解释一下吗?

For reference, this is being done on Server 2003 x64 SP2 with: 作为参考,这是在Server 2003 x64 SP2上完成的:

C:\>powershell get-host


Name             : ConsoleHost
Version          : 2.0
InstanceId       : 53c90e87-ded1-44f9-8e8d-6baaa1335420
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

and using write-output produces the same result. 并使用write-output产生相同的结果。

(This question is related to my work in solving this .) (这个问题与我解决这个问题的工作有关。)

That looks like a bug in PowerShell 2.0. 这看起来像PowerShell 2.0中的一个错误。 I tried reproducing with the PowerShell 3.0 preview and it now works as expected. 我尝试使用PowerShell 3.0预览进行复制,现在它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM