繁体   English   中英

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

[英]Passing variables from asp.net page to powershell script

我目前正在开发一个测试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 

提前致谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM