How to pipe all output of .exe execution in Powershell?

前端 未结 2 1592
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 01:25

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         


        
相关标签:
2条回答
  • 2020-12-30 01:34

    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.

    Basically, call CMD with your executable as a parameter. Like this:

    UPDATE

    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
    
    0 讨论(0)
  • 2020-12-30 01:41

    Give this a try

    $output = [string] (& psftp.exe 'username@ssh@ftp.domain.com' -b psftp.txt 2>&1)
    

    There is a PowerShell bug about 2>&1 making error records. The [string] cast works around it.

    0 讨论(0)
提交回复
热议问题