首先,要麼A)我沒有調查到這足夠硬或B)我發現一個問題,需要一些時髦的黑客。順便說一句,這是豪華v1.0。輸出重定向/捕獲問題與Powershell和Try-Catch和外部EXE
這裏有雲:
一個星期左右前,我問了一個問題關於在PowerShell中的EXE這是沒有,否則被抓住的exection輸出重定向。我很快提出了「2> & 1」,它解決了這個問題。
現在我又碰到了另一個障礙,希望看到你們中的一些人可以扔到它。
我在我的代碼中使用try-catch塊作爲一名優秀的程序員應該。當我去的地方,GPG(gnupg.org)的調用,傳遞給它一些命令如下:
try `
{
& $gpgExeLocation --import $keyFileName 2>&1 | out-file "theOutput.txt";
} `
-Catch `
{
write-host "$_";
}
得到了一個空白的文本文件(theOutput.txt)。
但是,如果我做同樣的呼叫外try-catch塊的,文本文件獲取預期寫入一些文本。
我想知道的是,如果輸出重定向到標準輸出和PowerShell陷阱異常的方式有問題 - 或者它是我的嘗試catch代碼開始?
,這裏是我的try-catch實施
function global:try
{
param
(
[ScriptBlock]$Command = $(Throw "The parameter -Command is required."),
[ScriptBlock]$Catch = { Throw $_ },
[ScriptBlock]$Finally = {}
)
& {
$local:ErrorActionPreference = "SilentlyContinue"
trap
{
trap
{
& {
trap { Throw $_ }
&$Finally
}
Throw $_
}
$_ | & { &$Catch }
}
&$Command
}
& {
trap { Throw $_ }
&$Finally
}
};
我得到了快樂觸發,並沒有閱讀你的代碼。我會試試看看會發生什麼。我很感激。 – CLR 2009-06-08 17:45:55