Powershell in c# and permissions

荒凉一梦 提交于 2020-01-06 19:37:06

问题


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.

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:

One example is a user trying to load Exchange snapin and run cmdlet: Get-MailboxPermission

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.

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?


回答1:


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);

read this article for additional info: http://msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx



来源:https://stackoverflow.com/questions/12747154/powershell-in-c-sharp-and-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!