开发者

Error when using Impersonation with Powershell and Exchange 2007 from C#

I'm using a Windows system service to create a mailbox in Ecxhange 2007 using Powershell. As it's running as a system service I'm wrapping the powershell call using impersonation (using advapi32.dll) to run it as a user with the appropriate exchange permissions, however, I'm receiving the following error message.

Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.Admin because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.

There is a Microsoft KB article (KB943937) describing this issue and the fix is to install exchange SP1 RU1, but I am currently running SP3 RU1. I am using the -DomainController parameter as specified in the KB article but I'm still receiving the same error.

If I run the service as the use开发者_如何转开发r that I am impersonating the code runs fine so I don't think it's a problem with the code. I've tried running this on Windows XP and Server 2008 with the same problem.

Calling System.Security.Principal.WindowsIdentity.GetCurrent().Name before the powershell code I can see that the impersonation is working correctly.

Has anyone come across this before?


For anyone coming across this in the future, the problem was with where the impersonation was starting.

You have to start the impersonation after adding the snapin but before creating the runspace.

RunspaceConfiguration rsconfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsconfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
BeginImpersonation();
myRunspace = RunspaceFactory.CreateRunspace(rsconfig);

See this article for the full source code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜