在Powershell中,我正在运行psftp.exe,这是PuTTy的主页。我正在这样做:

$cmd = "psftp.exe"
$args = '"username@ssh"@ftp.domain.com -b psftp.txt';
$output = & $cmd $args

这有效;我正在打印$output。但是它仅捕获该变量中的某些输出(例如“远程工作目录为[...]”),并将其他输出抛出为如下错误类型:
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中?

最佳答案

问题是某些输出被发送到STDERR,并且在PowerShell中重定向的工作方式与在CMD.EXE中不同。

How to redirect output of console program to a file in PowerShell很好地描述了问题,并且提供了一个聪明的解决方法。

基本上,使用可执行文件作为参数调用CMD。像这样:

更新

我修复了我的代码,使其可以正常工作。 :)

$args = '"username@ssh"@ftp.domain.com -b psftp.txt';
$output = cmd /c psftp.exe $args 2`>`&1

10-07 18:57
查看更多