使用 IDE 时从 Powershell 调用 3rd 方可执行文件时出错 [英] Error when calling 3rd party executable from Powershell when using an IDE

查看:21
本文介绍了使用 IDE 时从 Powershell 调用 3rd 方可执行文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 du.exe 的 PowerShell 脚本(磁盘使用情况 最初来自 Sysinternals)来计算目录的大小.

I have a PowerShell script that uses du.exe (Disk Usage originally from Sysinternals) to calculate the size of directories.

如果我在控制台中运行 du c:Backup,它会按预期工作,但是在 ISE 或 PowerGui 中运行的同一行代码给出了预期的结果加上错误

If I run du c:Backup in the console, it works as expected, but the same line of code run in ISE or PowerGui gives the expected result plus the error

+ du <<<<  c:ackup
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

这是为什么?我如何避免这个错误?我尝试使用 & 调用表达式,但没有成功.

Why is that? How do I avoid this error? I tried invoke-expression, using &, but no go.

感谢您的帮助.

推荐答案

为了避免这种情况,您可以将 stderr 重定向到 null 例如:

To avoid this you can redirect stderr to null e.g.:

du 2> $null

本质上,控制台主机和 ISE(以及远程处理)对 stderr 流的处理方式不同.在控制台主机上,PowerShell 支持诸如 edit.com 之类的应用程序与其他将彩色输出和错误写入屏幕的应用程序一起工作非常重要.如果 I/O 流未在控制台主机上重定向,PowerShell 会为本机 EXE 提供一个控制台句柄以直接写入.这会绕过 PowerShell,因此 PowerShell 看不到写入的错误,因此无法通过 $error 或写入 PowerShell 的 stderr 流来报告错误.

Essentially the console host and ISE (as well as remoting) treat the stderr stream differently. On the console host it was important for PowerShell to support applications like edit.com to work along with other applications that write colored output and errors to the screen. If the I/O stream is not redirected on console host, PowerShell gives the native EXE a console handle to write to directly. This bypasses PowerShell so PowerShell can't see that there are errors written so it can't report the error via $error or by writing to PowerShell's stderr stream.

ISE 和远程处理不需要支持这种情况,因此它们确实会看到 stderr 上的错误,然后写入错误并更新 $error.

ISE and remoting don't need to support this scenario so they do see the errors on stderr and subsequently write the error and update $error.

这篇关于使用 IDE 时从 Powershell 调用 3rd 方可执行文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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