从PowerShell获取变量信息到asp.net页面

时间:2015-03-16 14:28:19

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

我目前正在开发一个测试asp.net页面,该页面将在服务器上运行PowerShell脚本。

PowerShell脚本检查我存储在变量$ userinput中的用户输入是否与变量$ name匹配。

我已经设法从asp.net页面获取变量到PowerShell脚本。 Default.aspx.cs脚本在 userinput 上查找并替换,然后将脚本保存为修改版本。

我想在asp.net页面上打印出来自PowerShell脚本的变量$ result。我在asp.net页面上定义了一个名为Output的输出标签。

但是,在运行时,标签不会显示变量$ result。

中的信息

提前致谢

Test.ps1

$name = "test"
$userinput = "*userinput*"


if(($userinput) -eq $name)
    {

    $result = "Name $userinput already taken!" 

    }
    else
    { 

     $result = "Name $userinput availble!" 

    }

Default.aspx的

<!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>Compliant Cloud</title>
    <link rel="stylesheet" href="stylesheets\style.css" type="text/css" />
</head>
<body>

    <div id="logo">
            <img src="images\logo.png" alt="logo" height="100" width="250"/>
        </div>


<form id="form1" runat="server">
        <table>

            <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>
                    <asp:Label ID="Output" runat="server" Text="Output Text" Width="300px" Height="20px" ></asp:Label>
            </td></tr>
            <tr><td>
                &nbsp;</td><td>
                <asp:Button ID="ExecuteCode" runat="server" Text="Execute" Width="200" onclick="ExecuteCode_Click" />
            </td></tr>

        </table>

</form>
</body>
</html>

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 label
            Output.Text = string.Empty;

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



            string strContent = null;
            using (StreamReader objReader = new StreamReader("C:\\Users\\sysadmin\\Desktop\\Test.ps1"))
            {
                strContent = objReader.ReadToEnd();
            }
            strContent = strContent.Replace("*userinput*", Input.Text);


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

           //this.executePowerShellCode(strContent);



            // Add the script to the PowerShell object
           shell.Commands.AddScript("C:\\Users\\sysadmin\\Desktop\\Test_modified.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 < >
                Output.Text = Server.HtmlEncode(builder.ToString());
            }


            //string url = "testpage.html";
          //  Response.Redirect(url);


        }
    }
}

0 个答案:

没有答案