简体   繁体   中英

Passing parameters into raw powershell script from c# app

I am having issues passing parameters/arguments into my powershell script from ac# application. The issue is that I have a powershell script stored in my database to be executed. 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? 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. 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. In my code below, say that personsName = "Joe" and personsEmail = "Joe@yahoo.com". I'm assuming that taskRun.Task.Script is a full path to a PS script.

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.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() works in this case: if you wrote var myCommand = new Command("gwmi Win32_Bios"); and then put myCommand.Parameters.Add("computerName","myComputer"); and myCommand.Parameters.Add("credential", "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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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