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

使用命令行参数从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脚本并确保空格没有问题?


holdtom
浏览 348回答 3
3回答

互换的青春

尝试将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 argumentsCommand myCommand = new Command(scriptfile);CommandParameter testParam = new CommandParameter("key","value");myCommand.Parameters.Add(testParam);pipeline.Commands.Add(myCommand);// Execute PowerShell scriptresults = pipeline.Invoke();

宝慕林4294392

有没有机会我可以更清楚地了解Commands.AddScript方法的传递参数?C:\ Foo1.PS1 Hello World Hunger C:\ Foo2.PS1 Hello WorldscriptFile =“C:\ Foo1.PS1”parameters =“parm1 parm2 parm3”...可变长度的参数解决了这个问题......将null作为名称,将param作为值传递给CommandParameters集合这是我的功能:private&nbsp;static&nbsp;void&nbsp;RunPowershellScript(string&nbsp;scriptFile,&nbsp;string&nbsp;scriptParameters){ &nbsp;&nbsp;&nbsp;&nbsp;RunspaceConfiguration&nbsp;runspaceConfiguration&nbsp;=&nbsp;RunspaceConfiguration.Create(); &nbsp;&nbsp;&nbsp;&nbsp;Runspace&nbsp;runspace&nbsp;=&nbsp;RunspaceFactory.CreateRunspace(runspaceConfiguration); &nbsp;&nbsp;&nbsp;&nbsp;runspace.Open(); &nbsp;&nbsp;&nbsp;&nbsp;RunspaceInvoke&nbsp;scriptInvoker&nbsp;=&nbsp;new&nbsp;RunspaceInvoke(runspace); &nbsp;&nbsp;&nbsp;&nbsp;Pipeline&nbsp;pipeline&nbsp;=&nbsp;runspace.CreatePipeline(); &nbsp;&nbsp;&nbsp;&nbsp;Command&nbsp;scriptCommand&nbsp;=&nbsp;new&nbsp;Command(scriptFile); &nbsp;&nbsp;&nbsp;&nbsp;Collection<CommandParameter>&nbsp;commandParameters&nbsp;=&nbsp;new&nbsp;Collection<CommandParameter>(); &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(string&nbsp;scriptParameter&nbsp;in&nbsp;scriptParameters.Split('&nbsp;')) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CommandParameter&nbsp;commandParm&nbsp;=&nbsp;new&nbsp;CommandParameter(null,&nbsp;scriptParameter); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commandParameters.Add(commandParm); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scriptCommand.Parameters.Add(commandParm); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;pipeline.Commands.Add(scriptCommand); &nbsp;&nbsp;&nbsp;&nbsp;Collection<PSObject>&nbsp;psObjects; &nbsp;&nbsp;&nbsp;&nbsp;psObjects&nbsp;=&nbsp;pipeline.Invoke();}
打开App,查看更多内容
随时随地看视频慕课网APP