Q: Passing variables from asp.net page to powershell script I'm currently working on a test asp.net page that will run a PowerShell script on the same machine that will open an application. I have made a variable called application in my PowerShell script. I wish to pass information to that variable from an ASP.net page.
I have managed to get the ASP.net page to run the PowerShell script which works fine but cannot pass variables over.
In theory I should be able to enter for example calc into the text box on the ASP.net page and it should update the application variable in the PowerShell script and thus open the calculator.
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Management.Automation;
using System.Text;
using System.IO;

namespace PowerShellExecution
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ExecuteCode_Click(object sender, EventArgs e)
        {
            // Clean the Result TextBox
            ResultBox.Text = string.Empty;

            // Initialize PowerShell engine
            var shell = PowerShell.Create();



            //Change application variable
            StreamReader objReader = new StreamReader("C:\\Users\\sysadmin\\Desktop\\Test.ps1");
            string strContent = objReader.ReadToEnd();
            strContent = strContent.Replace("*application*", Input.Text);





            // Add the script to the PowerShell object
           shell.Commands.AddScript("C:\\Users\\sysadmin\\Desktop\\Test.ps1");




            // Execute the script
            var results = shell.Invoke();

            // display results, with BaseObject converted to string
            // Note : use |out-string for console-like output
            if (results.Count > 0)
            {
                // We use a string builder ton create our result text
                var builder = new StringBuilder();

                foreach (var psObject in results)
                {
                    // Convert the Base Object to a string and append it to the string builder.
                    // Add \r\n for line breaks
                    builder.Append(psObject.BaseObject.ToString() + "\r\n");
                }

                // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
                ResultBox.Text = Server.HtmlEncode(builder.ToString());
            }
        }
    }
}

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PowerShellExecution.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <table>
            <tr><td>&nbsp;</td><td><h1 align="left">Powershell Test V2</h1></td></tr>
            <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
            <tr><td>&nbsp;</td><td>Enter Application Name</td></tr>
            <tr><td>
                <br />
                </td><td>
                <asp:TextBox ID="Input" runat="server" TextMode="SingleLine" Width="200px" Height="20px" ></asp:TextBox>
            </td></tr>
            <tr><td>
                &nbsp;</td><td>
                <asp:Button ID="ExecuteCode" runat="server" Text="Execute" Width="200" onclick="ExecuteCode_Click" />
            </td></tr>
                <tr><td>&nbsp;</td><td><h3>Result</h3></td></tr>
                <tr><td>
                    &nbsp;</td><td>
                    <asp:TextBox ID="ResultBox" TextMode="MultiLine" Width="700" Height="200" runat="server"></asp:TextBox>
                </td></tr>
        </table>
    </div>
</form>
</body>
</html>

Test.ps1 - PowerShell code
$application = "*application*"

Start $application 

Thanks in advance!

A: You don't save your modified script. Program runs the old version of ps1.
 //Change application variable
   string strContent = null;
   using (StreamReader objReader = new StreamReader("C:\\Users\\sysadmin\\Desktop\\Test.ps1")) 
   {
       strContent = objReader.ReadToEnd();
   }
   strContent = strContent.Replace("*application*", Input.Text);

   File.WriteAllText("C:\\Users\\sysadmin\\Desktop\\Test_modified.ps1",strContent );

But it is not a good way to go. You should add parameter to your script and invoke it with parameter and you shoud validate Input.Text, cause it's unsafe to pass user input directly to your script.
param([String]$application)

