2

I'am tring to call a powershell script and pass through a parameter. I would all so like to pass through a more than one parameter eventually

        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

        Pipeline pipeline = runspace.CreatePipeline();

        String scriptfile = "..\\..\\Resources\\new group.ps1";

        Command myCommand = new Command(scriptfile, false);

        CommandParameter testParam = new CommandParameter("test3");

        myCommand.Parameters.Add(testParam);


        pipeline.Commands.Add(myCommand);
        Collection<PSObject> psObjects;
        psObjects = pipeline.Invoke();
        runspace.Close();

The problem seems to be ... well nothing happens. Is this how to correctly assign the varibles? Giving test powershell script

# creates group
net localgroup $username /Add
# makes folder
#mkdir $path
2
  • 1
    Not an answer to your question so much as a quick comment - have you tried interacting with the local ADSI store through .NET instead? stackoverflow.com/questions/1640022/… Commented Aug 7, 2012 at 16:13
  • No never through of it, but thanks Commented Aug 7, 2012 at 16:18

2 Answers 2

4

This line of code:

CommandParameter testParam = new CommandParameter("test3");

Creates a parameter named test3 that has a value of null. I suspect you want to create a named parameter e.g.:

CommandParameter testParam = new CommandParameter("username", "test3");

And you're script needs to be configured to accept parameters e.g.:

--- Contents of 'new group.ps1 ---
param([string]$Username)
...
Sign up to request clarification or add additional context in comments.

Comments

1

The PowerShell script needs to be setup to accept parameters:

Try adding the following to the top of that script and re-testing:

param([string]$username)

Alternatively, you could add the folowing line to the top of your script:

$username = $args[0]

Good luck.

3 Comments

this helps but is assigning System.Management.Automation.Runspaces.CommandParameter as groupname
If you open up PowerShell and execute the script with that parameter, what happens?
Take a look at Keith's answer. Looks like an issue with your C# and how you're setting the parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.