私は、ASP.NETアプリケーションから別のユーザーとしてpowershellスクリプトを実行できるようにします。私は正常にpowershellコマンドを実行し、出力を見ることができますが、私は別のユーザーとしてそれを実行する方法を把握することはできません。 私は、このガイドに従っている:ASP.net Webアプリケーションからpowershellを別のユーザーとして実行しますか?
http://jeffmurr.com/blog/?p=142
それは私が私のPowerShellコマンドを入力し、出力を別のテキストボックスに表示されますちょうどテキストボックスです。
新しいIISサイトと、ドメインユーザーとして実行され、新しいサイトをプールに関連付けたアプリケーションプールを作成しました。存在する唯一のw3wp.exeプロセスが、プールで指定したドメインユーザーとして実行されていることがわかります。それでもコマンドを実行した場合:
write-output $Env:USERNAME
システムアカウントのユーザー名 "SERVERNAME $"が返されます。 なぜ誰も知っていますか?私は何を逃したのですか?同じことを達成する別の方法がありますか?
コード...
HTML:
<%@ 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> </td><td><h1 align="left">PowerShell Command Harness</h1></td></tr>
<tr><td> </td><td> </td></tr>
<tr><td> </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>
</td><td>
<asp:Button ID="ExecuteCode" runat="server" Text="Execute" Width="200" onclick="ExecuteCode_Click" />
</td></tr>
<tr><td> </td><td><h3>Result</h3></td></tr>
<tr><td>
</td><td>
<asp:TextBox ID="ResultBox" TextMode="MultiLine" Width="700" Height="200" runat="server"></asp:TextBox>
</td></tr>
</table>
</div>
</form>
</body>
</html>
C#
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());
}
}
}
}