C#中的Powershell和权限 [英] Powershell in c# and permissions
问题描述
我从以 SYSTEM 帐户运行的 Windows 服务运行 PowerShell 脚本.为了增加权限,我让用户选择一个用户,然后我使用 LogonUser 方法模拟整个线程.
I am running PowerShell scripts from a Windows service running as SYSTEM account. To increase rights I am letting users select a user that I later impersonate the whole thread as using LogonUser method.
这样我就可以访问网络驱动器,当我尝试在 PowerShell 中打印当前用户时也可以使用.
This works so that I can access network drives and when I try to print the current user in PowerShell that works too.
但我有一个不起作用的情况:
But I have a case when it does not work:
一个示例是用户尝试加载 Exchange 管理单元并运行 cmdlet:Get-MailboxPermission
One example is a user trying to load Exchange snapin and run cmdlet: Get-MailboxPermission
然后他得到错误:术语Get-MailboxPermission"未被识别为 cmdlet、函数、脚本文件或可运行程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.术语Get-MailboxPermission"不被识别为 cmdlet、函数、脚本文件或可运行程序的名称.检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试.
Then he get error: The term 'Get-MailboxPermission' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. The term 'Get-MailboxPermission' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
我猜这是权限问题,我想知道是否还有其他什么需要更改为 RunSpace 等以获得增加的权限?还有其他想法吗?
I am guessing that this is permissions issue and I wonder if there is anything else that needs to be changed to RunSpace etc to get the increased permissions? Any other ideas?
推荐答案
哪个 PowerShell 版本?
Which PowerShell version?
看起来您没有添加交换管理单元.尝试运行此命令之前:
Looks like you don't have the exchange snapin added. Try running this command before:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
或者,通过代码,在运行空间初始化中,使用以下代码:
or, via code, in the runspace initialization, use this code:
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open(rsConfig);
阅读这篇文章了解更多信息:http://msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx
read this article for additional info: http://msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx
这篇关于C#中的Powershell和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!