MVC控制器运行空间模拟 [英] MVC Controller with Runspace Impersonation
问题描述
试图使用模拟从MVC 3控制器执行PowerShell命令,但保留收到请求的注册表访问是不允许的。异常时,调用Runspace.Open()
Trying to execute a Powershell cmdlet from a MVC 3 Controller using impersonation but keep receiving an "Requested registry access is not allowed." exception when calling Runspace.Open()
StringBuilder stringBuilder = new StringBuilder();
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
using (new Impersonator("username", "domain", "password"))
{
Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);
runspace.Open();
Pipeline pipeLine = runspace.CreatePipeline();
string script = "get-process";
pipeLine.Commands.AddScript(script);
Collection<PSObject> commandResults = pipeLine.Invoke();
foreach (PSObject obj in commandResults)
{
stringBuilder.AppendLine(obj.Properties["ProcessName"].Value.ToString());
}
调试显示注册表错误是由于注册表项读取正在尝试的HKCU \环境。 。运行上面没有模仿成功的作品
Debugging shows the registry error is due to a Registry Key Read being attempted on HKCU\Environment. Running the above with no impersonation works successfully.
请注意:模拟类在这里找到:的 http://platinumdogs.wordpress.com/2008/10/30/net-c-impersonation-with-network-credentials/
Note: Impersonation class was found here: http://platinumdogs.wordpress.com/2008/10/30/net-c-impersonation-with-network-credentials/
这是为什么会发生或者什么任何想法做才能解决这个问题?
Any ideas on why this is happening or what can be done to resolve it?
更新:
得到一些睡眠后,我的理由是,移动Runspace.Open()的模拟线以上将允许运行空间访问所需注册表数据(环境变量),这确实有帮助。
After getting some sleep I reasoned that moving the Runspace.Open() above the impersonation line would allow the runspace to access the required registry data (Environment Variables) and this indeed helped.
现在的代码工作正常使用内置的cmdlet,但是当我加载Microsoft.Exchange.Management.PowerShell.Admin 并尝试任何交易所的cmdlet的应用程序崩溃了的。
Now the code works fine with the built in cmdlets but when I load "Microsoft.Exchange.Management.PowerShell.Admin" and try any of the Exchange Cmdlets the Application is crashing out.
推荐答案
成功!
在的情况下,这是非常有用别人这里就是我得到了它的工作:
In the event this is useful to someone else here's how I got it to work:
- 安装Exchange管理工具
- 应用最新的服务包
- 确保您添加的参数为域控制器(微软 - KB943937)
这篇关于MVC控制器运行空间模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!