Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture EXE output in PowerShell

Tags:

powershell

A little background first.

I've been tasked with encrypting files with a Powershell script using GPG (gnupg.org). The specific exe I'm calling is simply gpg.exe. I'd like to capture the output whenever I execute a command.

For instance, I import a public key in powershell as follows:

& $gpgLocation --import "key.txt" 

$gpgLocation is simply the file location of gpg.exe (default being "C:\Program Files\GNU\GnuPG\gpg.exe"

My entire issue here is that if I try:

& $gpgLocation --import "key.txt" | out-file gpgout.txt 

All I get is a 1kb file, named appropriately, but it is COMPLETELY blank. I've tried several flags for out-file just to see if I was running into a quirk.

I've also tried sending the command to this code (and capturing the output with the usual out-file etc):

param (     [string] $processname,      [string] $arguments )  $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo; $processStartInfo.FileName = $processname; $processStartInfo.WorkingDirectory = (Get-Location).Path; if($arguments) { $processStartInfo.Arguments = $arguments } $processStartInfo.UseShellExecute = $false; $processStartInfo.RedirectStandardOutput = $true;  $process = [System.Diagnostics.Process]::Start($processStartInfo); $process.WaitForExit(); $process.StandardOutput.ReadToEnd(); 

Any ideas? I'm desperate!

like image 581
CLR Avatar asked May 28 '09 04:05

CLR


People also ask

How do I copy the output of a command in PowerShell?

Use QuickEdit to copy text—Although it's not obvious, the PowerShell command shell lets you select and quickly copy any text displayed in the command shell. Use the mouse to select the text to be copied, then press Enter or right-click on the selected text to copy it to the clipboard.


1 Answers

Does the output you're expecting go to standard-error or standard-out?

does this work?

& $gpgLocation --import "key.txt" 2>&1 | out-file gpgout.txt 
like image 159
Stobor Avatar answered Sep 22 '22 23:09

Stobor