Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 296 Vote(s) - 3.7 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pipe all output of .exe execution in Powershell?

#1
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`?
Reply

#2
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][1] 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

[1]:

[To see links please register here]

Reply

#3
Give this a try

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

There is a PowerShell [bug](

[To see links please register here]

) about `2>&1` making error records. The `[string]` cast works around it.
Reply

#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][1] such as `2>&1`**

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

[1]:

[To see links please register here]


sources:

[To see links please register here]


[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through