12

从命令行我可以做到。

.\test.ps1 1

从 C# 执行此操作时如何传递参数?我试过了

   .AddArgument(1)
   .AddParameter("p", 1)

我已经尝试在 .Invoke() 中将值作为 IEnumerable<object> 传递,但 $p 没有得到该值。

namespace ConsoleApplication1
{
    using System;
    using System.Linq;
    using System.Management.Automation;

    class Program
    {
        static void Main()
        {
            // Contents of ps1 file
            //  param($p)
            //  "Hello World ${p}"

            var script = @".\test.ps1";

            PowerShell
                .Create()
                .AddScript(script)
                .Invoke().ToList()
                .ForEach(Console.WriteLine);
        }
    }
}
4

4 回答 4

10

这个怎么样?

static void Main()
{
    string script = @"C:\test.ps1 -arg 'hello world!'";
    StringBuilder sb = new StringBuilder();

    PowerShell psExec = PowerShell.Create();
    psExec.AddScript(script);
    psExec.AddCommand("out-string");

    Collection<PSObject> results;
    Collection<ErrorRecord> errors;
    results = psExec.Invoke();
    errors = psExec.Streams.Error.ReadAll();

    if (errors.Count > 0)
    {
        foreach (ErrorRecord error in errors)
        {
            sb.AppendLine(error.ToString());
        }
    }
    else
    {
        foreach (PSObject result in results)
        {
            sb.AppendLine(result.ToString());
        }
    }

    Console.WriteLine(sb.ToString());
}

这是一个类似的版本,它传递了一个 DateTime 的实例

static void Main()
{
    StringBuilder sb = new StringBuilder();

    PowerShell psExec = PowerShell.Create();
    psExec.AddCommand(@"C:\Users\d92495j\Desktop\test.ps1");
    psExec.AddArgument(DateTime.Now);

    Collection<PSObject> results;
    Collection<ErrorRecord> errors;
    results = psExec.Invoke();
    errors = psExec.Streams.Error.ReadAll();

    if (errors.Count > 0)
    {
        foreach (ErrorRecord error in errors)
        {
            sb.AppendLine(error.ToString());
        }
    }
    else
    {
        foreach (PSObject result in results)
        {
            sb.AppendLine(result.ToString());
        }
    }

    Console.WriteLine(sb.ToString());
}
于 2012-07-10T01:44:18.913 回答
3

因此,为了简短的回答:使用 AddCommand 而不是 AddScript

于 2015-03-11T14:34:08.493 回答
2

另一种方法是用变量填充运行空间。

    public static string RunPs1File(string filePath, Dictionary<string, object> arguments)
    {
        var result = new StringBuilder();

        using (Runspace space = RunspaceFactory.CreateRunspace())
        {
            space.Open();

            foreach (KeyValuePair<string, object> variable in arguments)
            {
                var key = new string(variable.Key.Where(char.IsLetterOrDigit).ToArray());
                space.SessionStateProxy.SetVariable(key, variable.Value);
            }

            string script = System.IO.File.ReadAllText(filePath);
            using (PowerShell ps1 = PowerShell.Create())
            {
                ps1.Runspace = space;
                ps1.AddScript(script);

                var psOutput = ps1.Invoke();

                var errors = ps1.Streams.Error;
                if (errors.Count > 0)
                {
                    var e = errors[0].Exception;
                    ps1.Streams.ClearStreams();
                    throw e;
                }

                foreach (var line in psOutput)
                {
                    if (line != null)
                    {
                        result.AppendLine(line.ToString());
                    }
                }
            }
        }

        return result.ToString();
    }
于 2012-10-17T19:24:03.720 回答
-1

目前更简单的解决方案是首先将脚本作为字符串读取。

PowerShell
    .Create()
    .AddScript(File.ReadAllText(script)).AddParameter("p", 1)
    .Invoke().ToList()
    .ForEach(Console.WriteLine);
于 2020-01-14T11:23:07.120 回答