title: Passing variables from asp.net page to powershell script
body: <p>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.</p>
  
  <p>I have managed to get the ASP.net page to run the PowerShell script which works fine but cannot pass variables over.</p>
  
  <p>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.</p>
  
  <p>Default.aspx.cs</p>
  
  <pre><code>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 &gt; 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 &lt; &gt;
                  ResultBox.Text = Server.HtmlEncode(builder.ToString());
              }
          }
      }
  }
  </code></pre>
  
  <p>Default.aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PowerShellExecution.Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head id="Head1" runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;table&gt;
              &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;h1 align="left"&gt;Powershell Test V2&lt;/h1&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Enter Application Name&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;
                  &lt;br /&gt;
                  &lt;/td&gt;&lt;td&gt;
                  &lt;asp:TextBox ID="Input" runat="server" TextMode="SingleLine" Width="200px" Height="20px" &gt;&lt;/asp:TextBox&gt;
              &lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;
                  &amp;nbsp;&lt;/td&gt;&lt;td&gt;
                  &lt;asp:Button ID="ExecuteCode" runat="server" Text="Execute" Width="200" onclick="ExecuteCode_Click" /&gt;
              &lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;h3&gt;Result&lt;/h3&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;
                      &amp;nbsp;&lt;/td&gt;&lt;td&gt;
                      &lt;asp:TextBox ID="ResultBox" TextMode="MultiLine" Width="700" Height="200" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Test.ps1 - PowerShell code</p>
  
  <pre><code>$application = "*application*"
  
  Start $application 
  </code></pre>
  
  <p>Thanks in advance!</p>