30

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!

1
  • +1 for the last code snippet, which overrides default Start-Process functionality of either eating up the called process output, or writing it to a file (but not to standard output) Commented Jul 18, 2011 at 12:57

5 Answers 5

37

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
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like the 2>$1 works!!!! I sincerely appreciate all you guys that took time to look at this!! I mean it!
Another good tip is that when using this, any output from stderr will be wrapped in ErrorRecord objects. So you can easily handle error output if you want.
10

You also can use Out-Host as shown below.

& $gpgLocation --import "key.txt" | Out-Host

Comments

6

Stobor's answer is great. I am adding to his answer because I needed to perform additional actions if the exe had an error.

You can also store the output of the exe into a variable like this. Then you can do error handling based on the result of the exe.

$out = $gpgLocation --import "key.txt" 2>&1
if($out -is [System.Management.Automation.ErrorRecord]) {
    # email or some other action here
    Send-MailMessage -to [email protected] -subject "Error in gpg " -body "Error:`n$out" -from [email protected] -smtpserver smtp.example.com
}
$out | out-file gpgout.txt

Comments

3

Also, PowerShell simply can't capture the output of some programs because they don't write to stdout. You can verify this by running the program in PowerShell ISE (it's in the version 2.0 CTP 3)

If PowerShell ISE can't show the output in the graphical console, then you can't capture it either and may need some other way of automating the program.

2 Comments

GPG certainly won't manipulate the console screen buffer directly. Or, at least I'd find it highly unlikely that a UNIX program which just needs some output would bother with curses.
Displaying the error in my situation was fortunately not a problem, just capturing it. I did however think about this same problem as I assumed not all apps are going to do the same thing when it comes to console output.
3

You need to use the --batch switch when automating GPG.EXE, as in:

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

Without that switch, GPG may be waiting for user input.

1 Comment

In my situation it does not, but that is a good tip though. I do have another spot I can make use of --batch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.