首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当试图使用模拟在远程计算机上运行PowerShell脚本时,“不允许请求注册表访问”

当试图使用模拟在远程计算机上运行PowerShell脚本时,“不允许请求注册表访问”
EN

Stack Overflow用户
提问于 2014-03-25 14:11:09
回答 1查看 6.8K关注 0票数 10

这是我第一次尝试从PowerShell应用程序执行C#脚本。我使用PowerShell是因为我需要在远程机器上执行的.exe的输出。我能够使用WMI在远程计算机上运行.exe,但无法获得所需的输出。

无论如何,在过去的一天里,我一直在做这件事,我环顾网络,在这里寻找类似的问题,但似乎找不出问题。我试图在远程计算机上从我的PowerShell 4.0应用程序中运行一个简单的.NET命令。当我以管理员身份运行Visual 2013时,以下代码执行良好:

代码语言:javascript
复制
PowerShell ps = PowerShell.Create();
ps.AddScript(@"Invoke-Command {c:\path\to\file.exe /p} -computername <computerName>");
results = ps.Invoke();

我得到了预期的结果。然而,当我以非管理员的身份运行VS时,代码似乎执行得很好(没有异常),但是我没有得到任何结果。在浏览了一下之后,我添加了以下模拟:

代码语言:javascript
复制
using (var impersonator = new Impersonator("username", "domain", "password"))
{
    PowerShell ps = PowerShell.Create();
    ps.AddScript(@"Invoke-Command {c:\path\to\file.exe /p} -computername <computerName>");
    results = ps.Invoke();
}

然而,ps.Invoke方法开始抛出一个System.Security.SecurityException -“不允许请求的注册表访问”。下面是堆栈跟踪:

在System.Environment.GetEnvironmentVariable(String变量处的Microsoft.Win32.RegistryKey.OpenSubKey(字符串名、布尔值可写)、System.Management.Automation.ModuleIntrinsics.GetExpandedEnvironmentVariable(String名称处的EnvironmentVariableTarget目标、System.Management.Automation.ModuleIntrinsics..ctor(ExecutionContext上下文处的System.Management.Automation.ModuleIntrinsics.SetModulePath()处(System.Management.Automation.ModuleIntrinsics..ctor(ExecutionContext上下文处)、System.Management.Automation.ExecutionContext..ctor(AutomationEngine引擎上的PSHost hostInterface、PSHost hostInterface、在System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface,RunspaceConfiguration runspaceConfiguration,InitialSessionState iss)在System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper() at System.Management.Automation.Runspaces.LocalRunspace.OpenHelper(Boolean syncCall)在System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall) System.Management.Automation.Runspaces.RunspaceBase.Open() at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse,( System.Management.Automation.PowerShell.CoreInvokeHelperTInput,TOutput at System.Management.Automation.PowerShell.CoreInvokeTInput,TOutput at System.Management.Automation.PowerShell.CoreInvokeTOutput at System.Management.Automation.PowerShell.Invoke(IEnumerable input,PSInvocationSettings设置)在System.Management.Automation.PowerShell.Invoke()

当我运行的管理员帐户不仅在我的机器上,而且在整个企业的机器上都有访问注册表的权限时,我不知道为什么要获得SecurityException。我甚至不确定它是在哪个注册表上得到异常,我的机器还是远程机器。

EN

回答 1

Stack Overflow用户

发布于 2014-03-30 20:02:22

在模拟之前为您的RunSpace对象创建基础PowerShell:

代码语言:javascript
复制
PowerShell ps = PowerShell.Create();
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
powerShell.Runspace = runspace;

using (var impersonator = new Impersonator("username", "domain", "password"))
{
    ps.AddScript(@"Invoke-Command {c:\path\to\file.exe /p} -computername <computerName>");
    results = ps.Invoke();
}
runspace.Close()

RunSpace对象封装用于脚本执行的OS环境。访问的关键可能是HKCU\Environment。这就是我在使用Perfmon时所看到的。RunSpace可能使用HKCU\Environment填充诸如$PATH之类的变量。

因此,在创建RunSpace时,您希望当前用户能够访问HKCU\Environment。

提取模拟块的RunSpace.Open在其他地方被提到为避免注册表访问问题的黑客攻击。。但是,仅仅创建PowerShell对象并不能保证调用Runspace.Open()。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22637128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档