使用命令行参数从C#执行PowerShell脚本

我需要在C#中执行PowerShell脚本。 该脚本需要命令行参数。

这是我迄今为止所做的:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(scriptFile);

// Execute PowerShell script
results = pipeline.Invoke();

scriptFile包含诸如“C: Program Files MyProgram Whatever.ps1”之类的东西。

该脚本使用命令行参数(如“-key Value”),而Value可以是可能包含空格的路径。

我没有得到这个工作。 有谁知道如何从C#中将命令行参数传递给PowerShell脚本,并确保空格没有问题?


尝试将脚本文件创建为单独的命令:

Command myCommand = new Command(scriptfile);

那么你可以添加参数

CommandParameter testParam = new CommandParameter("key","value");
myCommand.Parameters.Add(testParam);

最后

pipeline.Commands.Add(myCommand);

以下是完整的编辑代码:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();

//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile);
CommandParameter testParam = new CommandParameter("key","value");
myCommand.Parameters.Add(testParam);

pipeline.Commands.Add(myCommand);

// Execute PowerShell script
results = pipeline.Invoke();

我有另一个解决方案。 我只想测试执行PowerShell脚本是否成功,因为也许有人可能会更改策略。 作为参数,我只是指定要执行的脚本的路径。

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.Arguments = @"& 'c:Scriptstest.ps1'";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();

string output = process.StandardOutput.ReadToEnd();
Assert.IsTrue(output.Contains("StringToBeVerifiedInAUnitTest"));

string errors = process.StandardError.ReadToEnd();
Assert.IsTrue(string.IsNullOrEmpty(errors));

脚本的内容是:

$someVariable = "StringToBeVerifiedInAUnitTest"
$someVariable

任何机会,我可以更清晰地通过Commands.AddScript方法的传递参数?

C: Foo1.PS1 Hello World Hunger C: Foo2.PS1 Hello World

scriptFile =“C: Foo1.PS1”

参数=“parm1 parm2 parm3”...可变长度的参数

解决了这个问题...将null作为名称并将参数作为值传递给CommandParameters集合

这是我的功能:

private static void RunPowershellScript(string scriptFile, string scriptParameters)
{
    RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
    Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
    runspace.Open();
    RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
    Pipeline pipeline = runspace.CreatePipeline();
    Command scriptCommand = new Command(scriptFile);
    Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
    foreach (string scriptParameter in scriptParameters.Split(' '))
    {
        CommandParameter commandParm = new CommandParameter(null, scriptParameter);
        commandParameters.Add(commandParm);
        scriptCommand.Parameters.Add(commandParm);
    }
    pipeline.Commands.Add(scriptCommand);
    Collection<PSObject> psObjects;
    psObjects = pipeline.Invoke();
}
链接地址: http://www.djcxy.com/p/37875.html

上一篇: Execute PowerShell Script from C# with Commandline Arguments

下一篇: Is a value on its own a valid JSON string?