unable to pass the parameter value from ASP page  [Answered]RSS

1 reply

Last post May 23, 2019 06:22 AM by Able

  • unable to pass the parameter value from ASP page

    May 22, 2019 03:30 PM|Manusan|LINK

    Hi , I have powershell script below to execute from ASP page . I just want to pass the input from textbox to ps script.

    i just want pass the value for $param1 from ASP page . do i need to try different method in PS script ?

    MY PS script

    [CmdletBinding()]
    param (
    [Parameter( Mandatory=$true)]
    [string]$param1
    )
    get-service $param1 | Out-File "D:\output.txt"

    aspx page

    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="tbx" Width="248px"></asp:TextBox>
    <asp:Button ID="cmdbutton" runat="server" Text="RUN BATCH"
    onclick="cmdbutton_Click" />

    aspx.cs page

    protected void cmdbutton_Click(object sender, EventArgs e)
    {
    string CN;
    CN = tbx.Text;
    string sourceFile = Server.MapPath("/test.ps1");
    var pshell = PowerShell.Create();
    pshell.AddScript(@"D:\Inetpub\PowershellScript\test.ps1").AddParameter(CN);
    var output = pshell.Invoke();
    }

  • Re: unable to pass the parameter value from ASP page

    May 23, 2019 06:22 AM|Able|LINK

    Hi Manusan,

    According to your description, in power shell if you want to pass parameters in powershell you should readtext firstly then you could pass your script into powershell script.You could write as below:

    Firstly, we  create a powershell script:

    param
    (
      $a
      )
    get-service "$a" |Out-File 'd://ps.txt'

    Then in c# code behind:

    public partial class AA : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                PowerShell powerShell = PowerShell.Create();
                powerShell.AddScript(File.ReadAllText(Server.MapPath("~/test.ps1")))
                    .AddParameter("a", Textbox1.text).Invoke();
    
            }
    
        }

    Best Regards

    Able

    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.