重定向PowerShell脚本的stdout和stderr时出错

rip*_*234 11 powershell stdout stderr

当stdout/stderr未重定向时,脚本运行正常.

When I add both stderr and stdout redirection, I getg the following error: How can I avoid it?

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0 19.4M    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
 81 19.4M    0     0   81 15.9M      0  54.5M --:--:-- --:--:-- --:--:-- 55.8M
100 19.4M    0     0  100 19.4M      0  14.2M  0:00:01  0:00:01 --:--:-- 14.3M
100 19.4M    0     0  100 19.4M      0  8428k  0:00:02  0:00:02 --:--:-- 8454k
100 19.4M    0     0  100 19.4M      0  5924k  0:00:03  0:00:03 --:--:-- 5937k
100 19.4M    0     0  100 19.4M      0  4567k  0:00:04  0:00:04 --:--:-- 4575k
100 19.4M    0    50  100 19.4M     10  4291k  0:00:04  0:00:04 --:--:--  835k
out-lineoutput : The OS handle's position is not what FileStream expected. Do not use a handle simu
ltaneously in one FileStream and in Win32 code or another FileStream. This may cause data loss.
    + CategoryInfo          : NotSpecified: (:) [out-lineoutput], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.OutLineOutputCom 
   mand
Run Code Online (Sandbox Code Playgroud)

x0n*_*x0n 15

Lee Holmes(PowerShell团队的高级开发人员之一)在博客文章中介绍了这一点:

http://www.leeholmes.com/blog/WorkaroundTheOSHandlesPositionIsNotWhatFileStreamExpected.aspx

这是PowerShell v1.0中的错误,在以下情况下发生:

  • PowerShell命令生成常规输出和错误输出
  • 您已使用cmd.exe将输出重定向到文件
  • 您已使用cmd.exe合并输出和错误流

有一个解决方法.

-Oisin

  • @bernd_k根据引用帖子的更新,它是针对Windows 8进行排序的.我仍然无法相信我们将不得不在V2中再努力使用它10年.格儿. (2认同)