C# 是否以其他用户身份从ASP.net web应用程序运行powershell?

C# 是否以其他用户身份从ASP.net web应用程序运行powershell?,c#,asp.net,powershell,iis,C#,Asp.net,Powershell,Iis,我希望能够以其他用户的身份从ASP.NET应用程序运行powershell脚本。我可以成功运行powershell命令并查看输出,但我无法确定如何作为其他用户执行它。 我遵循了以下指南: 它只是一个文本框,我在其中输入powershell命令,输出将显示在另一个文本框中 我已经创建了一个新的IIS站点和一个作为域用户运行的应用程序池,并将新站点与池相关联。我可以看到,唯一存在的w3wp.exe进程正在以我在池中指定的域用户身份运行。如果我运行该命令: write-output $Env:USE

我希望能够以其他用户的身份从ASP.NET应用程序运行powershell脚本。我可以成功运行powershell命令并查看输出,但我无法确定如何作为其他用户执行它。 我遵循了以下指南:

它只是一个文本框,我在其中输入powershell命令,输出将显示在另一个文本框中

我已经创建了一个新的IIS站点和一个作为域用户运行的应用程序池,并将新站点与池相关联。我可以看到,唯一存在的w3wp.exe进程正在以我在池中指定的域用户身份运行。如果我运行该命令:

write-output $Env:USERNAME
它返回系统帐户用户名“SERVERNAME$”。 有人知道为什么吗?我错过了什么?有没有其他方法可以达到同样的效果

代码

HTML:


PowerShell命令线束
PowerShell命令

后果
C#

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统、管理、自动化;
使用系统文本;
命名空间PowerShellExecution
{
公共部分类默认值:System.Web.UI.Page
{
公共文本框ResultBox;
公共文本框输入;
受保护的无效页面加载(对象发送方、事件参数e)
{
ResultBox=(TextBox)this.FindControl(“ResultBox”);
输入=(文本框)this.FindControl(“输入”);
}
受保护的void ExecuteCode\u单击(对象发送方,事件参数e)
{
//清除结果文本框
ResultBox.Text=string.Empty;
//初始化PowerShell引擎
var shell=PowerShell.Create();
//将脚本添加到PowerShell对象
shell.Commands.AddScript(Input.Text);
//AddScript(“C:\\TestSite\\test.ps1”);
//执行脚本
var results=shell.Invoke();
//显示结果,将BaseObject转换为字符串
//注意:对于类似控制台的输出,使用| out字符串
如果(results.Count>0)
{
//我们使用字符串生成器来创建结果文本
var builder=新的StringBuilder();
foreach(结果中的var psObject)
{
//将基础对象转换为字符串并将其附加到字符串生成器。
//为换行符添加\r\n
Append(psObject.BaseObject.ToString()+“\r\n”);
}
//用HTML编码字符串(防止出现“危险”字符的安全问题,如<>
ResultBox.Text=Server.HtmlEncode(builder.ToString());
}
}
}
}

这是因为您已在主机服务器上发布了网站。当您访问网站时,您正在访问在Azure(或服务器所在位置)上运行的应用程序,因此发送在该服务器计算机上运行的命令

很抱歉,您不能以网站用户的身份在您的计算机上运行这些命令,因为脚本调用方法是以确保用户环境安全的方式开发的,否则未知的危险代码只需按下按钮即可在其末端执行

如果您使用Azure,请注意它是一个“平台即服务”,您的角色是在预配置的系统上部署网站和构建应用程序,并由Azure管理。不允许任何系统级别的更改,包括用户名更改。请尝试

我也曾尝试过改变它,但经过艰苦的学习

<%@ 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 Command Harness</h1></td></tr>
            <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
            <tr><td>&nbsp;</td><td>PowerShell Command</td></tr>
            <tr><td>
                <br />
                </td><td>
                <asp:TextBox ID="Input" runat="server" TextMode="MultiLine" Width="433px" Height="73px" ></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>
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;

namespace PowerShellExecution
{
    public partial class Default : System.Web.UI.Page
    {
        public TextBox ResultBox;
        public TextBox Input;

        protected void Page_Load(object sender, EventArgs e)
        {
            ResultBox = (TextBox)this.FindControl("ResultBox");
            Input = (TextBox)this.FindControl("Input");
        }

        protected void ExecuteCode_Click(object sender, EventArgs e)
        {
            // Clean the Result TextBox
            ResultBox.Text = string.Empty;

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

            // Add the script to the PowerShell object
            shell.Commands.AddScript(Input.Text);
            // shell.Commands.AddScript("C:\\TestSite\\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());
            }
        }
    }
}