powershell 2.0命令行重定向 [英] powershell 2.0 command line redirection

查看:379
本文介绍了powershell 2.0命令行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找以下差异的解释:



给出以下PowerShell脚本 foo.ps1

 写主机正常
写错误错误
写主机yay

使用

运行

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



产生:

  normal 
C:\foo.ps1:error
在行:1个字符:10
+。\foo.ps1<<
+ CategoryInfo:NotSpecified:(:) [Write-Error],WriteErrorException
+ FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1

Write-Host: OS句柄的位置不是FileStream期望的。不要在一个FileStream和Win32 co
de或另一个FileStream中同时使用句柄。这可能会导致数据丢失。
在C:\foo.ps1:3 char:11
+ write-host<<< yay
+ CategoryInfo:NotSpecified:(:) [Write-Host],IOException
+ FullyQualifiedErrorId:System.IO.IOException,Microsoft.PowerShell.Commands.WriteHostCommand

但是运行时:



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



产生(正确):

  normal 
C:\foo.ps1:error
在行:1个字符:10
+。\foo.ps1<<
+ CategoryInfo:NotSpecified:(:) [Write-Error],WriteErrorException
+ FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1

yay

我几乎解决了自己认为重定向的顺序在Windows中是重要的, href =http://technet.microsoft.com/en-us/library/bb490982.aspx =nofollow>用于命令重定向的TechNet用法页面显示stderr重定向之前的文件重定向。 p>

有人可以向我解释这个问题吗?



在Server 2003 x64 SP2上:

  C:\> powershell get-host 

b $ b名称:ConsoleHost
版本: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

并使用write-output产生相同的结果。



(此问题与我在解决相关的工作有关。 )

解决方案

这看起来像是PowerShell 2.0中的一个错误。我尝试使用 PowerShell 3.0 预览进行再现,现在可以使用预期。


I'm looking for an explanation of the following discrepancy:

Given the following powershell script 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.

Can someone please explain this to me?

For reference, this is being done on Server 2003 x64 SP2 with:

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.

(This question is related to my work in solving this.)

解决方案

That looks like a bug in PowerShell 2.0. I tried reproducing with the PowerShell 3.0 preview and it now works as expected.

这篇关于powershell 2.0命令行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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