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

时间:2015-02-11 09:41:02

标签: c# asp.net .net variables powershell

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

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

理论上我应该能够在ASP.net页面的文本框中输入例如calc,它应该在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>

Test.ps1 - PowerShell代码

$application = "*application*"

Start $application 

提前致谢!

1 个答案:

答案 0 :(得分:0)

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