2012-10-15 30 views
12

Tôi đang sử dụng System.Management.Automation.Runspaces để thực thi các tập lệnh PowerShell. có tùy chọn nào tôi có thể đọc mã thoát của một tập lệnh nhất định không?Cách đọc mã thoát PowerShell qua C#

using System.IO; 
using System.Management.Automation.Runspaces; 
using System.Collections.ObjectModel; 
using System.Management.Automation; 

namespace PowerShell 
{ 
    public class PowerShellExecuter 
    { 
     public Collection<PSObject> RunPsScript(string psScriptFile) 
     { 
      string psScript; 
      if (File.Exists(psScriptFile)) 
      { 
       psScript = File.ReadAllText(psScriptFile); 
      } 
      else 
      { 
       throw new FileNotFoundException("Wrong path for the script file"); 
      } 
      Runspace runSpace = RunspaceFactory.CreateRunspace(); 
      runSpace.Open(); 

      RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace); 
      runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 

      Pipeline pipeLine = runSpace.CreatePipeline(); 
      pipeLine.Commands.AddScript(psScript); 
      pipeLine.Commands.Add("Out-String"); 

      Collection<PSObject> returnObjects = pipeLine.Invoke(); 
      runSpace.Close(); 

      return returnObjects; 
     } 
    } 
} 

Trả lời

5

Lệnh PowerShell có cơ chế lỗi phong phú hơn mã thoát số nguyên. Có một luồng lỗi mà các lỗi không chấm dứt xuất hiện trên đó. Chấm dứt lỗi dẫn đến các ngoại lệ được ném để bạn cần xử lý các lỗi đó. Các mã sau đây cho thấy làm thế nào để sử dụng hai cơ chế:

using System; 
using System.Collections.ObjectModel; 
using System.Management.Automation; 

namespace PowerShellRunspaceErrors 
{ 
    class Program 
    { 
     private static PowerShell s_ps; 

     static void Main(string[] args) 
     { 
      s_ps = PowerShell.Create(); 
      ExecuteScript(@"Get-ChildItem c:\xyzzy"); 
      ExecuteScript("throw 'Oops, I did it again.'"); 
     } 

     static void ExecuteScript(string script) 
     { 
      try 
      { 
       s_ps.AddScript(script); 
       Collection<PSObject> results = s_ps.Invoke(); 
       Console.WriteLine("Output:"); 
       foreach (var psObject in results) 
       { 
        Console.WriteLine(psObject); 
       } 
       Console.WriteLine("Non-terminating errors:"); 
       foreach (ErrorRecord err in s_ps.Streams.Error) 
       { 
        Console.WriteLine(err.ToString()); 
       } 
      } 
      catch (RuntimeException ex) 
      { 
       Console.WriteLine("Terminating error:"); 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 
} 

Nếu bạn chạy chương trình này nó ra:

Output: 
Non-terminating errors: 
Cannot find path 'C:\xyzzy' because it does not exist. 
Terminating error: 
Oops, I did it again. 
Press any key to continue . . . 
1

Quy trình trả lại mã thoát, chứ không phải PowerShell Runspaces.

Theo như tôi biết, cách tốt nhất để nắm bắt trạng thái của Runspace sẽ là đăng ký sự kiện Runspace.StateChanged, điều này sẽ cung cấp cho bạn RunspaceStateInfo để giải quyết. Có hai thuộc tính hữu ích trên RunspaceStateInfo: ReasonState.

http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspacestateinfo_members(v=vs.85).aspx

0

Nó đơn giản như yêu cầu giá trị cho runspace bạn vừa tạo:

s_ps.AddScript("$LASTEXITCODE"); 
results = s_ps.Invoke(); 
int.TryParse(results[0].ToString(),out valorSortida); 
Các vấn đề liên quan