locked
Passing parameters to powershell script in C# RRS feed

  • Question

  • Hi, i have read a lot of posts regarding this issue but i still can't make it to work

    http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments

    http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

    http://social.msdn.microsoft.com/Forums/en-SG/csharpgeneral/thread/faa70c95-6191-4f64-bb5a-5b67b8453237

    I have a Windows Form with 2 Textboxes

    TB_user

    TB_office

    I also have a powershell script on c:\scripts\script.ps1  that i need to run with 2 parameters

    script.ps1 -user TB_user.Tostring() -office TB_office.ToString()

    So getting the textbox content as parameters to call the script on a button click, my code is:

     private void button1_Click(object sender, EventArgs e)
            {
                string user = TB_user.ToString();
                string office = TB_office.ToString();
                var scriptfile = @"c:\scripts\script.ps1";
    
    
                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 userParam = new CommandParameter("user", user);
                myCommand.Parameters.Add(userParam);
                CommandParameter officeParam = new CommandParameter("office", office);
                myCommand.Parameters.Add(officeParam);
    
                pipeline.Commands.Add(myCommand);
    
                // Execute PowerShell script
                results = pipeline.Invoke();
    
            }
        }
    }

    I got the name results doesn't exist in the current context, and also, nothing runs.. :(

    Can anybody help?

    Thanks

    Friday, March 1, 2013 2:04 PM

Answers

All replies

  • what if you try to use Process.Start() :

    string user = TB_user.ToString();
    string office = TB_office.ToString();
    var scriptfile = @"c:\scripts\script.ps1";
    Process _Proc = Process.Start("Powershell.exe", @"" + scriptfile + " '" + user + "' " + " '" + office + "'");

    regards

    joon


    • Edited by Joon84 Friday, March 1, 2013 2:18 PM
    Friday, March 1, 2013 2:17 PM
  • Hi, what assembly reference you use for process?

    Thanks

    Friday, March 1, 2013 2:23 PM
  • sorry, forgot to mention. It is "using System.Diagnostics;"

    regards

    joon

    Friday, March 1, 2013 2:26 PM
  • Hi, Thanks.

    2 questions, when i run it it starts powershell in a window, can i hide it?

    Also it seems powershell returns an error but i can't see where because the window closes...

    Also the process shouldn't be like this:?

       Process _Proc = Process.Start("Powershell.exe", @"" + scriptfile + "-user" + user + "' " + "-office" + office);

    thanks

    Friday, March 1, 2013 2:32 PM
  • you may need to run the powershell with an additional parameter, check this link for this : http://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window

    string user = TB_user.ToString();
    string office = TB_office.ToString();
    var scriptfile = @"c:\scripts\script.ps1";
    Process _Proc = new Process();
    _Proc.StartInfo = "Powershell.exe";
    _Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    _Proc.StartInfo.Arguments = "'" + user + "' " + " '" + office + "'";
    _Proc.Start();

    there is one API available for this work, check this : http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28VS.85%29.aspx

    regards

    joon


    • Edited by Joon84 Friday, March 1, 2013 2:48 PM
    Friday, March 1, 2013 2:40 PM
  • Hello,

    Not related to your problem directly but it might help you to track down the problem, you can use EachoArgs.ps1 to check what are the actual arguments you send and how you sent it by executing the script.

    Small script but handy.


    Regards,

    Eyal Shilony

    Friday, March 1, 2013 3:01 PM
  • I have a Windows Form with 2 Textboxes

    TB_user

    TB_office


     private void button1_Click(object sender, EventArgs e)
            {
                string user = TB_user.ToString();
                string office = TB_office.ToString();
    

    Your now calling ToString on the TextBox. I would think you want to use TB_user.Text; to get the user input from the TextBox.
    Friday, March 1, 2013 3:05 PM
  • hi where do you call scriptfile from there?

    Also the script neds to be called like:

    script.ps1 -user user -office office

    And right now it is not working, can you please help?

    thanks

    Friday, March 1, 2013 3:24 PM
  • I think you  ight be right with that so i changed thm to:

    string user = TB_user.Text;
    string office = TB_office.Text;

    But i still don't know how to call the process with the arguments

    Thansk

    Friday, March 1, 2013 3:32 PM
  • Hi,

    Which method do you want to use ? If RunSpace IMO you should use the AddScript method at some point. Currently your code does as if the script file name was a real PS command...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, March 1, 2013 3:43 PM
  • Hi Patrice, at the moment i want to pass 2 parameters on 

    startInfo.Arguments = @"c:\script.ps1";

    So i get script1.ps1 + -user "user" + -office "office"

    Not sure which method is best.

    Thanks

    Friday, March 1, 2013 3:58 PM
  • refer this link

    http://www.codeproject.com/Questions/213870/Runnung-Powershell-Scripts-with-aruguments-using-C

    http://social.msdn.microsoft.com/Forums/en-NZ/csharplanguage/thread/5de2f424-3d2e-478f-b1ab-01faa8fb763e

    and please refer this

    http://blogs.msdn.com/b/zainnab/archive/2008/07/26/calling-a-powershell-script-from-your-net-code.aspx //this link give you in detail in step by step


    Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights


    • Edited by Tarun00007 Friday, March 1, 2013 5:32 PM
    • Marked as answer by Bob Shen Monday, March 11, 2013 10:55 AM
    Friday, March 1, 2013 5:31 PM
  • Um..   I don't see results

    declared anywhere.  Hence the error results doesn't exist in the current context.

    i.e.:

    Collection<PSObject> results = ....

    (I can't believe no one else caught this??)



    Now whether this is a solution to your problem, I don't know.  But if it is, please mark it.
    • Edited by jamesiw Friday, March 1, 2013 9:53 PM
    • Proposed as answer by jamesiw Friday, March 1, 2013 9:53 PM
    • Marked as answer by Bob Shen Monday, March 11, 2013 10:55 AM
    Friday, March 1, 2013 6:47 PM