Windows powershell 2.0命令行重定向
我正在寻找以下差异的解释: 给定以下powershell脚本foo.ps1:Windows powershell 2.0命令行重定向,windows,powershell,command-line,file-io,powershell-2.0,Windows,Powershell,Command Line,File Io,Powershell 2.0,我正在寻找以下差异的解释: 给定以下powershell脚本foo.ps1: write-host "normal" write-error "error" write-host "yay" 使用 C:\>powershell。\foo.ps1>out.txt 2>&1 产生: normal C:\foo.ps1 : error At line:1 char:10 + .\foo.ps1 <<<< + CategoryInfo : NotSp
write-host "normal"
write-error "error"
write-host "yay"
使用
C:\>powershell。\foo.ps1>out.txt 2>&1
产生:
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
我几乎下定决心认为重定向的顺序在Windows中很重要,但是,本文中的所有示例都显示了stderr重定向之前的文件重定向
有人能给我解释一下吗?
作为参考,这是在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
使用写输出产生相同的结果
(这个问题与我解决问题的工作有关。)这看起来像是PowerShell 2.0中的一个bug。我尝试使用预览进行复制,现在它可以正常工作。我不知道答案,但我知道Write Host的处理方式与其他所有Write cmdlet不同。我现在像躲避瘟疫一样躲避它。尝试使用写输出测试。谢谢提示!不幸的是,出现了相同的结果。写入输出和写入主机的用途完全不同。写入主机将信息(文本)写入PowerShell主机。写入输出将对象写入PowerShell管道,然后可由接受管道输入的命令拦截和处理该管道。如果未使用此类命令,则对象输出也会呈现给主机。显然,在这两种不同的情况下,cmd.exe的行为有所不同。然而,由于这种行为没有记录在案,依靠它来解决你的问题可能不是一个好主意。。。更快乐的回答?升级到CTP,向Msft报告问题
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