0

I created a website that does remotely execute powershell to specific server. I would like to know how it work to pass 2 user selected values to commandline from dropdownlist on asp.net website along with powershell script file? I already have working code with powershell script file but now adding 2 arguments in script file.The powershell arguments is,

    [string]$ContainerIn=$args[0]
[int]$ips2get=$args[1]

Here a C# working codes,

//These 3 input varaibles will pass to powershell script to get specific results
        string env = "";
        string container = "";
        string numIPs = "";

        //assign dropdown selected value to variable to pass to script
        container = DropDownListContainer.SelectedValue;
        numIPs = DropDownListIP.SelectedValue;

        if (container == "H02" || container == "H07" || container == "H08")
        {
            env = "Prod";
        }
        else
        {
            env = "NonProd";
        }

        // Create a Powershell
        Runspace runSpace = RunspaceFactory.CreateRunspace();
        runSpace.Open();
        Pipeline pipeline = runSpace.CreatePipeline();

        Command invokeScript = new Command("Invoke-Command");


        RunspaceInvoke invoke = new RunspaceInvoke();

        //Add powershell command/script functions into scriptblock
        //Somewhere on this codes that it need to add command line to go with Get-FreeAddress.ps1 file script

        ScriptBlock sb = invoke.Invoke(@"{D:\Scripts\Get-FreeAddress.ps1}")[0].BaseObject as ScriptBlock;
        //ScriptBlock sb = invoke.Invoke("{" + PowerShellCodeBox.Text + "}")[0].BaseObject as ScriptBlock;

        invokeScript.Parameters.Add("scriptBlock", sb);
        invokeScript.Parameters.Add("computername", TextBoxServer.Text);

        pipeline.Commands.Add(invokeScript);

        Collection<PSObject> output = pipeline.Invoke();

        //splitting results in new lines
        foreach (PSObject psObject in output)
        {

            str = str + psObject + "\r\n";
            //str = psObject + "\r\n";
            //str += "\n" + psObject;
            //str = str + Environment.NewLine + psObject;

        }

        if (str == "")
        {
            str = "Error";

            ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
        }

        //print out powershell output result
        ResultBox.Text = str;

    }
StudentIT
  • 481
  • 2
  • 19
  • 45
  • 1
    Is this not a duplicate of http://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments?rq=1 ? – Eris Nov 18 '13 at 21:36
  • This is useful but how does that work when I am using website remotely? This seems like it running local. Also I am using file path script file. – StudentIT Nov 19 '13 at 15:47
  • I updated my example which it more clear – StudentIT Nov 19 '13 at 17:13

1 Answers1

0

I finally made this work,

I just need to modify to

ScriptBlock sb = invoke.Invoke(@"{D:\Scripts\Get-FreeAddress.ps1 '"+container+"' "+numIPs+"}")[0].BaseObject as ScriptBlock;

The powershell script argument will get container and numIPs variables.

StudentIT
  • 481
  • 2
  • 19
  • 45