稍微背景優先。在PowerShell中捕獲EXE輸出
我的任務是使用GPG(gnupg.org)通過Powershell腳本加密文件。我打電話的具體exe只是gpg.exe。我想在執行命令時捕獲輸出。
舉例來說,我導入公鑰在PowerShell中,如下所示:
& $gpgLocation --import "key.txt"
$ gpgLocation簡直是gpg.exe的文件位置(默認爲「C:\ Program Files文件\ GNU \ GnuPG的\ GPG .EXE」
我在這裏的整個問題是,如果我嘗試:
& $gpgLocation --import "key.txt" | out-file gpgout.txt
我得到的是一個1KB的文件,適當命名的,但它是完全空白的我試過幾個標誌對於出。文件只是爲了看如果我遇到了一個怪癖。
我也試着發送命令到該代碼(與通常的出文件等捕獲它的輸出):
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();
任何想法?我很絕望!
1的最後的代碼段,它覆蓋的缺省開始處理功能或者吃起來稱爲過程輸出,或將其寫入到文件中(但不向標準輸出) – 2011-07-18 12:57:37