[英]C# Powershell Runspace as User, not machine account
我有一个网站,我试图从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脚本主机版本5.8版权所有(C)Microsoft Corporation。 版权所有。
AppManager维护模式CLI V2.1
正在连接到控制中心...通过Windows身份验证的服务器calntmgt501 \\ NQCCDB *无法连接到服务器。 验证凭据。 错误:*用户“ BLACKROCK \\ USPMVVMT001 $”的登录失败。
如果该网站使用Windows身份验证,特别是Kerberos,则必须启用委派。 这就是所谓的“双跳”问题。 更具体地说,您将需要域管理员来启用约束委派。 “受限”部分意味着您希望将委派限制为仅Web服务器需要连接到的那些服务。
否则,您需要做的就是让Domain Admin进入您的网站,然后您可以使用其凭据执行您想要的任何操作。 那将是一个巨大的安全问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.