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
?
There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.
Most PowerShell cmdlets are designed to support pipelines. In most cases, you can pipe the results of a Get cmdlet to another cmdlet of the same noun. For example, you can pipe the output of the Get-Service cmdlet to the Start-Service or Stop-Service cmdlets.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With