如何在Powershell中通过管道传输.exe执行的所有输出? [英] How to pipe all output of .exe execution in Powershell?
问题描述
在Powershell中,我正在运行psftp.exe
,这是PuTTy的主页.我正在这样做:
In Powershell I am running psftp.exe
which is PuTTy's homepage. I am doing this:
$cmd = "psftp.exe"
$args = '"username@ssh"@ftp.domain.com -b psftp.txt';
$output = & $cmd $args
这有效;我正在打印$output
.但是它仅捕获该变量中的某些输出(例如远程工作目录为[...]"),并将其他输出抛出为这样的错误类型:
This works; and I am printing out $output
. But it only catches some output in that variable (like "Remote working directory is [...]") and is throwing other output to an error type like this:
psftp.exe : Using username "username@ssh".
At C:\full_script.ps1:37 char:20
+ $output = & <<<< $cmd $args
+ CategoryInfo : NotSpecified: (Using username "username@ssh".:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
此正在使用用户名..."等看起来像普通的FTP消息.如何确保所有输出都放入$output
?
This "Using username ..." etc looks like a normal FTP message. How can I make sure all output gets put into $output
?
推荐答案
问题是某些输出被发送到STDERR,并且在PowerShell中重定向的方式与在CMD.EXE中不同.
The problem is some output is being sent to STDERR and redirection works differently in PowerShell than in CMD.EXE.
How to redirect output of console program to a file in PowerShell has a good description of the problem and a clever workaround.
基本上,以可执行文件作为参数调用CMD
.像这样:
Basically, call CMD
with your executable as a parameter. Like this:
我修复了我的代码,使其可以正常工作. :)
I fixed my code so it would actually work. :)
$args = '"username@ssh"@ftp.domain.com -b psftp.txt';
$output = cmd /c psftp.exe $args 2`>`&1
这篇关于如何在Powershell中通过管道传输.exe执行的所有输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!