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

3

30

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

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

This "Using username ..." etc looks like a normal FTP message. How can I make sure all output gets put into $output?

Disciplinant answered 15/3, 2013 at 16:16 Comment(1)
If you have to use the call operator (&), (which you don't if directly running psftp), the args have to be an array, like 'user@domain','-b','file.txt'Kislev
O
24

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
Offwhite answered 15/3, 2013 at 17:4 Comment(7)
No quotes around the $cmd details? I tried this, but the Powershell just seems to hang now.Disciplinant
Thanks, that somewhat worked as it didn't parse any output into an STDERR. But instead of the "Using username ..." etc now being in $output, it seems nowhere to be found. With your code, did you intend for that?Disciplinant
I didn't have PSFTP.EXE to test with so I was using PSEXEC.EXE and $args = '-d' to test. I'll see if I can try it with PSFTP.Offwhite
I should add that I was using PSEXEC because I know it writes some of it's output to STDERR.Offwhite
Actually I may be mistaken; I think the STDERR is there. But appears at the end of the $output string. Intended?Disciplinant
It should capture it in the order it is output. Where does it appear if you just run your command from the prompt without redirecting or capturing the output to a variable?Offwhite
If you use cmd /c, it all goes to standard output anyway.Kislev
C
13

Give this a try

$output = [string] (& psftp.exe 'username@[email protected]' -b psftp.txt 2>&1)

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

Caduceus answered 15/3, 2013 at 22:15 Comment(2)
Can't get this to work in PowerShell 2.0. It redirects to a file named 1.Jordain
The link is dead.Shortcircuit
S
4
& "my.exe" | Out-Null    #go nowhere    
& "my.exe" | Out-Default # go to default destination  (e.g. console)
& "my.exe" | Out-String  # return a string

the piping will return it in real-time

& "my.exe" | %{    
   if ($_ -match 'OK')    
   { Write-Host $_ -f Green }    
   else if ($_ -match 'FAIL|ERROR')   
   { Write-Host $_ -f Red }   
   else    
   { Write-Host $_ }    
}

Note: If the executed program returns anything other than a 0 exitcode, the piping will not work. You can force it to pipe with redirection operators such as 2>&1

& "my.exe" 2>&1 | Out-String

sources:

https://mcmap.net/q/108076/-how-to-tell-powershell-to-wait-for-each-command-to-end-before-starting-the-next

https://social.technet.microsoft.com/forums/windowsserver/en-US/b6691fba-0e92-4e9d-aec2-47f3d5a17419/start-process-and-redirect-output-to-powershell-window

Semi answered 4/10, 2022 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.