ai didi

powershell - 如何在Powershell中通过管道传输.exe执行的所有输出?

转载 作者:行者123 更新时间:2023-12-03 13:28:26 24 4
gpt4 key购买 nike

在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

关于powershell - 如何在Powershell中通过管道传输.exe执行的所有输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15437244/

24 4 0
文章推荐: r - 代码中不一致的数组错误
文章推荐: internet-explorer - 在PowerShell 3.0中使用Invoke-Webrequest会产生Windows安全警告
文章推荐: r - 预测新数据的 LDA 主题
文章推荐: r - 使用maptools readShapePoly在R中打开SHP文件时出错
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com