将变量从 asp.net 页面传递到 powershell 脚本

Passing variables from asp.net page to powershell script

我目前正在开发一个测试 asp.net 页面,该页面将 运行 在将打开应用程序的同一台计算机上运行 PowerShell 脚本。我在我的 PowerShell 脚本中创建了一个名为 application 的变量。我希望从 ASP.net 页面向该变量传递信息。

我已经设法将 ASP.net 页面转到 运行 PowerShell 脚本,该脚本工作正常但无法传递变量。

理论上我应该能够在 ASP.net 页面的文本框中输入计算器,它应该会更新 PowerShell 脚本中的应用程序变量,从而打开计算器。

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>

测试。ps1 - PowerShell 代码

$application = "*application*"

Start $application 

提前致谢!

您没有保存修改后的脚本。程序运行旧版本 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 );

但这不是一个好方法。您应该向您的脚本添加参数并使用参数调用它,并且您应该验证 Input.Text,因为将用户输入直接传递给您的脚本是不安全的。

param([String]$application)