2009-08-05 55 views
11

我從C#應用程序運行Powershell測試腳本。由於導致pipe.Invoke()拋出異常的錯誤cmdlet,該腳本可能會失敗。在Pipeline.Invoke後捕獲C#中的Powershell輸出

我能夠捕獲我需要的有關異常的所有信息,但我希望能夠顯示該腳本的輸出。我沒有任何運氣,因爲在引發異常時結果似乎爲空。

有什麼我失蹤了嗎?謝謝!

m_Runspace = RunspaceFactory.CreateRunspace(); 
m_Runspace.Open(); 
Pipeline pipe = m_Runspace.CreatePipeline(); 
pipe.Commands.AddScript(File.ReadAllText(ScriptFile)); 
pipe.Commands.Add("Out-String"); 
try { 
    results = pipe.Invoke(); 
} 
catch (System.Exception) 
{ 
    m_Runspace.Close(); 
    // How can I get to the Powershell output that comes before the exception? 
} 

回答

8

我結束了使用的解決方案是實現我們自己的PSHost處理PowerShell的輸出。這個初始信息來自http://community.bartdesmet.net/blogs/bart/archive/2008/07/06/windows-powershell-through-ironruby-writing-a-custom-pshost.aspx在「建立一個自定義PS主機」部分。

在我的情況下,它確實需要使用自定義PSHostRawUserInterface。

下面簡要概述了所做的工作。我只列出了實際實現的函數,但有很多隻包含throw new NotImplementedException();

private class myPSHost : PSHost 
{ 
    (Same as what the above link mentions) 
} 
private class myPSHostUI : PSHostUserInterface 
{ 
    private myPSHostRawUI rawui = new myPSHostRawUI(); 

    public override void Write // all variations 
    public override PSHostRawUserInterface RawUI { get { return rawui; } } 

} 
private class myPSHostRawUI : PSHostRawUserInterface 
{ 
    public override ConsoleColor ForegroundColor 
    public override ConsoleColor BackgroundColor 
    public override Size BufferSize 
} 
+0

不錯,我正在尋找。謝謝。 – 2012-07-23 08:34:34

14

不確定這是否有幫助。我猜你正在運行V1。這V2方法不拋出,並打印結果:

Hello World 
67 errors 

string script = @" 
    'Hello World' 
    ps | % { 
    $_.name | out-string1 
    } 
"; 

PowerShell powerShell = PowerShell.Create(); 

powerShell.AddScript(script); 
var results = powerShell.Invoke(); 

foreach (var item in results) 
{ 
    Console.WriteLine(item); 
} 

if (powerShell.Streams.Error.Count > 0) 
{ 
    Console.WriteLine("{0} errors", powerShell.Streams.Error.Count); 
} 
+0

你是正確的,我使用的電源外殼v1。 – 2009-08-07 13:45:58

0

我有同樣的問題。最簡單的方法來獲得輸出時pipe.Invoke()拋出一個異常是使用Invoke(IEnumerable input, IList output)

例子展示瞭如何讓所有的輸出,錯誤,減弱等以正確的順序

PowerShell腳本

Write-Output "Hello world" 
Write-Error "Some error" 
Write-Warning "Some warning" 
throw "Some exception" 

C#

List<string> RunLog = new List<string>(); 

using (System.Management.Automation.PowerShell psInstance = System.Management.Automation.PowerShell.Create()) 

{ 
    psInstance.AddScript(_Script); 

psInstance.Streams.Error.DataAdded += (sender, args) => 
{ 
    ErrorRecord err = ((PSDataCollection<ErrorRecord>)sender)[args.Index]; 
    RunLog.Add($"ERROR: {err}"); 
}; 

psInstance.Streams.Warning.DataAdded += (sender, args) => 
{ 
    WarningRecord warning = ((PSDataCollection<WarningRecord>)sender)[args.Index]; 
    RunLog.Add($"WARNING: {warning}"); 
}; 

... etc ... 

var result = new PSDataCollection<PSObject>(); 
result.DataAdded += (sender, args) => 
{ 
    PSObject output = ((PSDataCollection<PSObject>)sender)[args.Index]; 
    RunLog.Add($"OUTPUT: {output}"); 
}; 

try 
{ 
    psInstance.Invoke(null, result); 
} 
catch(Exception ex) 
{ 
    RunLog.Add($"EXCEPTION: {ex.Message}"); 
}             
}