如何使用参数调用c#中的powershell脚本

时间:2013-04-01 21:57:36

标签: c# powershell

我正在尝试使用c#中的参数调用powershell脚本。是否有任何选项只提供powershell脚本文件和参数,而不是将整个powershell命令作为c#代码中的字符串。

2 个答案:

答案 0 :(得分:7)

快速谷歌搜索确实为您提供所需的一切。这个来自http://www.devx.com/tips/Tip/42716

您需要参考System.Management.Automation 然后使用

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

创建一个运行空间来托管PowerScript环境:

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

使用runspace,为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 :(得分: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);

感谢Ale Tiro的想法