当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《使用命令行参数从C#执行PowerShell脚本》经验,为你挑选了4个好方法。

我需要在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脚本并确保空格没有问题?



1> Kosi2801..:

尝试将scriptfile创建为单独的命令:

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();



2> Jowen..:

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

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.Arguments = @"& 'c:\Scripts\test.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



3> 小智..:

有没有机会我可以更清楚地了解Commands.AddScript方法的传递参数?

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

scriptFile ="C:\ Foo1.PS1"

parameters ="parm1 parm2 parm3"...可变长度的参数

解决了这个问题......将null作为名称,将param作为值传递给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 commandParameters = new Collection();
    foreach (string scriptParameter in scriptParameters.Split(' '))
    {
        CommandParameter commandParm = new CommandParameter(null, scriptParameter);
        commandParameters.Add(commandParm);
        scriptCommand.Parameters.Add(commandParm);
    }
    pipeline.Commands.Add(scriptCommand);
    Collection psObjects;
    psObjects = pipeline.Invoke();
}



4> James Pogran..:

您也可以将管道与AddScript方法一起使用:

string cmdArg = ".\script.ps1 -foo bar"            
Collection psresults;
using (Pipeline pipeline = _runspace.CreatePipeline())
            {
                pipeline.Commands.AddScript(cmdArg);
                pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
                psresults = pipeline.Invoke();
            }
return psresults;

它需要一个字符串,以及您传递的任何参数。

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有