Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;

    }
share|improve this question
1  
Is this not a duplicate of stackoverflow.com/questions/527513/… ? –  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 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.