私はSystem.Management.Automation名前空間を活用し、PowerShellコンソールオブジェクトを作成します。実行のために、コマンドまたは.ps1ファイル(両方とも引数付き)を渡すことができます。別のシェルよりもはるかにクリーンで、powershell.exeを呼び出す必要があります。
他の事項は、アプリケーションプールのIDであることを確認することが適切です。 PowerShellのコマンドやスクリプトを実行するために必要な権限があることを確認してください。次に、スクリプトファイルを実行する場合は、Set-ExecutionPolicyを適切に設定してください。ここで
は、これらのオブジェクトを使用してAA PowerShellコンソールであるかのようにテキストボックスのWebフォームから提出されたコードを実行するコマンドだ - のアプローチを説明する必要があります
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
{
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();
// Add the script to the PowerShell object
shell.Commands.AddScript(Input.Text);
// 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());
}
}
}
}
ページのロード時にこれを行うには、あなたがしたいですよあなたのニーズを満たす関数を作成し、 "Page_Load"関数から呼び出します。
ここでは、Visual Studioで最初から最後までページを作成し、これを完了する方法について説明します。http://grokgarble.com/blog/?p=142。
お手数であれば、適切な印を付けてマークを付けてください。
おかげで、それについての完全なソースコードと ジェフ
任意のソリューション? – Kiquenet