2013-04-01 27 views
2

我想用c#中的參數調用powershell腳本。有沒有任何選項只給出PowerShell腳本文件以及參數,而不是將整個PowerShell命令作爲c#代碼中的字符串。如何在C#中調用powershell腳本的參數

+0

也許[此](http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C)製品和/或[這](http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments)以前的SO問題可以幫助你? (** PS。**,因爲你在你的問題中指定了C#我已經刪除了VB.NET標記) – nkvu

+0

我試圖爲.net框架獲取一些常用方法,而不是針對c# –

回答

7

快速谷歌搜索真的給你所有你需要的。這是一個從http://www.devx.com/tips/Tip/42716

你需要參考​​ 然後用

using System.Management.Automation; 
using System.Management.Automation.Runspaces; 

創建一個運行空間舉辦的PowerScript環境:

Runspace runSpace = RunspaceFactory.CreateRunspace(); 
runSpace.Open(); 

使用運行空間,創建一個新的管道對於您的cmdlet:

Pipeline pipeline = runSpace.CreatePipeline(); 

創建Command對象以表示要執行的cmdlet並將其添加到管道中。本示例檢索所有進程,然後按內存使用情況對它們進行排序。

Command getProcess = new Command("Get-Process"); 
Command sort = new Command("Sort-Object"); 
sort.Parameters.Add("Property", "VM"); 
pipeline.Commands.Add(getProcess); 
pipeline.Commands.Add(sort); 

前面的代碼的功能與以下PowerShell命令行:

PS > Get-Process | Sort-Object -Property VM

最後,在管道中執行的命令,並做一些與輸出:

Collection output = pipeline.Invoke(); 
foreach (PSObject psObject in output) 
{ 
    Process process = (Process)psObject.BaseObject; 
    Console.WriteLine("Process name: " + process.ProcessName); 
} 
1

我做了這樣的最後一件事

public static string RunScript(string scriptText) 
    { 
     Runspace runspace = RunspaceFactory.CreateRunspace(); 
     runspace.Open(); 
     Pipeline pipeline = runspace.CreatePipeline(); 
     pipeline.Commands.AddScript(scriptText); 
     pipeline.Commands.Add("Out-String"); 
     try 
     { 
      Collection<PSObject> results = pipeline.Invoke(); 
      runspace.Close(); 
      StringBuilder stringBuilder = new StringBuilder(); 
      if (pipeline.Error.Count > 0) 
      { 
       //iterate over Error PipeLine until end 
       while (!pipeline.Error.EndOfPipeline) 
       { 
        //read one PSObject off the pipeline 
        var value = pipeline.Error.Read() as PSObject; 
        if (value != null) 
        { 
         //get the ErrorRecord 
         var r = value.BaseObject as ErrorRecord; 
         if (r != null) 
         { 
          //build whatever kind of message your want 
          stringBuilder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message); 
          stringBuilder.AppendLine(r.InvocationInfo.PositionMessage); 
          stringBuilder.AppendLine(string.Format("+ CategoryInfo: {0}", r.CategoryInfo)); 
          stringBuilder.AppendLine(
          string.Format("+ FullyQualifiedErrorId: {0}", r.FullyQualifiedErrorId)); 
         } 
        } 
       } 
      } 
      else 
       stringBuilder.AppendLine(string.Format("Build is Success")); 
      return stringBuilder.ToString(); 
     } 
     catch (Exception ex) 
     { 
      string err = ex.ToString(); 
      err = "Build Failed!!!" + "\r\n" + "Check the Setup File Available or Path error"; 
      return err; 
     } 
    } 


SCRIPT = "buildmsi.ps1 " + ssource + " " + sdestination; 
      projectbuildstatus.Text = RunScript(SCRIPT); 

由於啤酒提羅爲主意

相關問題