我想用c#中的參數調用powershell腳本。有沒有任何選項只給出PowerShell腳本文件以及參數,而不是將整個PowerShell命令作爲c#代碼中的字符串。如何在C#中調用powershell腳本的參數
2
A
回答
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);
由於啤酒提羅爲主意
相關問題
- 1. 如何調用powershell腳本與C#中的參數configfile#
- 2. 用C#中的參數調用PowerShell腳本#
- 3. PowerShell,調用無參數參數的腳本,如帶選項的腳本
- 4. 我的本地PowerShell腳本如何調用遠程PowerShell腳本?
- 5. 如何將[switch]參數傳遞給C#中的PowerShell腳本?
- 6. 如何在使用'使用PowerShell運行'執行PowerShell腳本時在另一個PowerShell腳本中調用函數
- 7. Powershell腳本調用函數
- 8. 如何使用c#獲取powershell腳本參數列表?
- 9. 如何使用PowerShell腳本到C#中?
- 10. 如何在C++中執行powershell腳本?
- 11. 在C#應用程序腳本中調用PowerShell函數
- 12. 使用PowerShell的調用(&)運算符來調用PowerShell腳本與開關參數
- 13. Powershell調用Powershell腳本
- 14. 如何調用腳本中的腳本,並解析參數
- 15. 如何使用powershell腳本中的參數調用可執行文件
- 16. 如何將參數傳遞給C#程序的powershell腳本?
- 17. 調用-WebRequest的通網址爲腳本參數(PowerShell中)
- 18. 使用批處理腳本中的參數運行PowerShell腳本
- 19. 解析PowerShell腳本參數
- 20. 從非託管C++調用PowerShell腳本
- 21. 如何調用我的dll並在PowerShell腳本中使用它
- 22. 如何獲取PowerShell腳本中命名參數的數量?
- 23. 如何在Powershell中爲腳本提供參數屬性?
- 24. 如何在PowerShell腳本中指定非位置參數?
- 25. 如何使用Invoke-Sqlcmd從Powershell腳本內的表值參數(TVP)調用sp_executeSql?
- 26. C#powershell腳本
- 27. 從cmd調用具有許多參數的powershell腳本
- 28. 調用PowerShell腳本與「指令」的參數,而不是「-file」
- 29. 帶參數*和*函數的Powershell腳本
- 30. PowerShell腳本參數傳遞的數組
也許[此](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
我試圖爲.net框架獲取一些常用方法,而不是針對c# –