简体   繁体   English

将参数从c#app传递到原始PowerShell脚本

[英]Passing parameters into raw powershell script from c# app

I am having issues passing parameters/arguments into my powershell script from ac# application. 我有问题从ac#application传递参数/参数到我的powershell脚本。 The issue is that I have a powershell script stored in my database to be executed. 问题是我有一个存储在我的数据库中的powershell脚本要执行。 This script takes in parameters and its this that doesn't seem to take in my passed in arguments. 这个脚本接受参数及其在传入的参数中似乎没有的参数。 An example of what I am doing is below. 我正在做的一个例子如下。 The tasjRun.Task.Script is the actual script NOT the path to a ps1 file and I think this may be the issue? tasjRun.Task.Script是实际脚本而不是ps1文件的路径,我认为这可能是问题所在? Can I pass in parameters to a raw script? 我可以将参数传递给原始脚本吗?

Many thanks 非常感谢

Richard 理查德

var runspaceConfiguration = RunspaceConfiguration.Create();

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

var pipeline = runspace.CreatePipeline();

var myCommand = new Command(taskRun.Task.Script, true);

foreach (var kvp in taskRun.GetScriptParameters())
{
    myCommand.Parameters.Add(new CommandParameter(kvp.Key, kvp.Value));
}

pipeline.Commands.Add(myCommand);

var result = pipeline.Invoke().FirstOrDefault();

I can tell you your approach works in principle eg: 我可以告诉你,你的方法原则上是有效的,例如:

var runspaceConfiguration = RunspaceConfiguration.Create();
var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

var pipeline = runspace.CreatePipeline();

var script = "param($p1,$p2) \"p1 is $p1, p2 is $p2\"";
var myCommand = new Command(script, true);

myCommand.Parameters.Add(new CommandParameter("p1", DateTime.Now));
myCommand.Parameters.Add(new CommandParameter("p2", Math.PI));

pipeline.Commands.Add(myCommand);

var result = pipeline.Invoke().FirstOrDefault();
Console.WriteLine(result);

Outputs: 输出:

p1 is 12/16/2011 10:24:48, p2 is 3.14159265358979

So we would need to see your script in order to better determine why this isn't working for you. 因此,我们需要查看您的脚本,以便更好地确定为什么这对您不起作用。

I'm new to stackoverflow so forgive any weird formatting. 我是stackoverflow的新手,所以请原谅任何奇怪的格式。 I know this thread is old, but I had this problem myself today and couldn't find an explicit solution online so I figured I'd share the solution of one of my co-workers and me. 我知道这个帖子已经老了,但我今天自己遇到了这个问题,并且无法在网上找到明确的解决方案,所以我想我会分享一个同事和我的解决方案。

The trick is if you're calling an actual script (eg "C:\\scripts\\sendEmail.ps1") then you need to pass in the parameters the same way you would do if you were running the script from command line. 诀窍在于,如果您正在调用实际脚本(例如“C:\\ scripts \\ sendEmail.ps1”),那么您需要以与从命令行运行脚本时相同的方式传递参数。 In my code below, say that personsName = "Joe" and personsEmail = "Joe@yahoo.com". 在下面的代码中,说personName =“Joe”和personsEmail =“Joe@yahoo.com”。 I'm assuming that taskRun.Task.Script is a full path to a PS script. 我假设taskRun.Task.Script是PS脚本的完整路径。

If you print myCommand it should read "C:\\scripts\\sendEmail.ps1 -Name Joe -Email Joe@yahoo.com" where -Name and -Email would correspond to $Name and $Email parameters in sendEmail.ps1 (make sure you set up the parameters correctly in your PS script!) 如果你打印myCommand,它应该是“C:\\ scripts \\ sendEmail.ps1 -Name Joe -Email Joe@yahoo.com”,其中-Name和-Email对应于sendEmail.ps1中的$ Name和$ Email参数(请确保你在PS脚本中正确设置参数!)

myCommand.Parameters.Add() WON'T WORK in this case because it assumes that your command is an actual command, not a full script. myCommand.Parameters.Add()在这种情况下不起作用,因为它假定您的命令是实际命令,而不是完整脚本。 myCommand.Parameters.Add() works in this case: if you wrote var myCommand = new Command("gwmi Win32_Bios"); myCommand.Parameters.Add()适用于这种情况:如果你写了var myCommand = new Command(“gwmi Win32_Bios”); and then put myCommand.Parameters.Add("computerName","myComputer"); 然后把myCommand.Parameters.Add(“computerName”,“myComputer”); and myCommand.Parameters.Add("credential", "myPassword"); 和myCommand.Parameters.Add(“凭证”,“myPassword”); where "myComputer" and "myPassword" are hard-coded strings, you would get the command, "gwmi win32_Bios -computerName myComputer -credential myPassword" which should execute fine (it won't return data as I've written it, but it will pass in the parameters correctly). 其中“myComputer”和“myPassword”是硬编码的字符串,你会得到命令“gwmi win32_Bios -computerName myComputer -credential myPassword”哪个应该执行正常(它不会像我写的那样返回数据,但它将正确传递参数)。

Sorry this is so long, but I wanted it to be clear because I struggled with this problem for quite some time. 对不起,这已经很久了,但我希望它清楚,因为我在这个问题上挣扎了很长一段时间。 The full code is below. 完整代码如下。 FYI, you don't need runspaceConfiguration to create a Runspace object. 仅供参考,您不需要runspaceConfiguration来创建Runspace对象。

var runspaceConfiguration = RunspaceConfiguration.Create();

var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);

var pipeline = runspace.CreatePipeline();    

var myCommand = new Command(String.Format("{0} -Name {1} -Email {2}", taskRun.Task.Script, personsName, personsEmail), true);

pipeline.Commands.Add(myCommand);

var result = pipeline.Invoke().FirstOrDefault(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM