【发布时间】:2012-01-19 20:19:38
【问题描述】:
我有一个网站,我正试图从 powershell 运行空间运行 vbscript(听起来很奇怪,但这是我认为的最简单的方法)。我的问题是 powershell 脚本作为本地计算机帐户运行,而不是运行它的用户。任何有关解决此问题的帮助将不胜感激。我尝试使用在http://support.microsoft.com/kb/306158 找到的模拟上下文,但没有成功。提前致谢。
protected void btnRemMaint_Click(object sender, EventArgs e)
{
txtOutput.Text = "";
txtOutput.Text = "Remove Maintenance Results";
txtOutput.Text += Environment.NewLine;
string servers;
servers = txtServers.Text.Replace(Environment.NewLine, ",");
while (servers[servers.Length - 1].ToString() == ",")
servers = servers.Substring(0, servers.Length - 1);
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
string script = "C:\\inetpub\\wwwroot\\VRS\\Admins\\PSScripts\\AMMaintMode.vbs";
string mode = "/maint:0";
string serv = "/server:" + servers;
string ccserv = "/ccserver:calntmgt501";
using (
RunspacePool runspace = RunspaceFactory.CreateRunspacePool())
{
// open it
runspace.Open();
// create a pipeline and feed it the script text
PowerShell pipeline = PowerShell.Create();
pipeline.RunspacePool = runspace;
pipeline.AddCommand("cscript");
pipeline.AddArgument(script);
pipeline.AddArgument(mode);
pipeline.AddArgument(serv);
pipeline.AddArgument(ccserv);
// execute the script
IAsyncResult pipeResults = pipeline.BeginInvoke();
PSDataCollection<PSObject> pipeOutput = pipeline.EndInvoke(pipeResults);
//pipeline.Invoke();
for (int i = 0; i < pipeOutput.Count; i++)
{
txtOutput.Text += pipeOutput[i].BaseObject.ToString();
txtOutput.Text += Environment.NewLine;
}
runspace.Close();
}
impersonationContext.Undo();
txtServers.Text = "";
}
输出结果如下:
Microsoft (R) Windows Script Host 版本 5.8 版权所有 (C) Microsoft 公司。保留所有权利。
AppManager 维护模式 CLI V2.1
正在连接到控制中心...服务器 calntmgt501\NQCCDB 通过 Windows 验证 * 连接服务器失败。验证凭据。错误: * 用户“BLACKROCK\USPMVVMT001$”登录失败。
【问题讨论】:
-
APP Pool 的运行方式是什么?
-
抱歉耽搁了。 BLACKROCK\USPMVVMT001$ 是系统内置账户。
-
应用池标识是执行代码的账户。那么为什么你说它没有在用户运行时运行呢?
-
我需要它以访问网站的用户身份运行,而不是应用程序池。很抱歉没有说清楚。
-
您是否通过集成的 Windows 身份验证从网站用户那里接收凭据?
标签: c# powershell