I have the following code that creates a PowerShell runspace with the Exchange 2010 snap in loaded.
Dim runspaceConfig = RunspaceConfiguration.Create()
Dim snapInException As PSSnapInException = Nothing
runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)
Dim runspace = RunspaceFactory.CreateRunspace(runspaceConfig)
runspace.Open()
Since installing Visual Studio 2012 I started getting the following error when executing the line that adds the snap-in to the runspace config.
System.Management.Automation.Runspaces.PSSnapInException occurred
HResult=-2146233087
Message=Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.E2010 because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
Source=System.Management.Automation
WasThrownFromThrowStatement=False
StackTrace:
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadCustomPSSnapIn(PSSnapInInfo mshsnapinInfo)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo, PSSnapInException& warning)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.DoAddPSSnapIn(String name, PSSnapInException& warning)
at System.Management.Automation.Runspaces.RunspaceConfiguration.AddPSSnapIn(String name, PSSnapInException& warning)
I have been able to confirm that nlog is somehow causing this issue. The combination of creating an nlog logger prior to creating the powershell runspace results in the error.
If I remove the nlog config section from my app config and just create an empty nlog logger then the snap-in is loaded with no error. Also, If I leave the nlog config present in my app config but don’t create an nlog logger the snap-in is also successfully loaded.
- I have tried building the project in both x64 and x86.
- I have re-installed the exchange management tools.
- I have tried testing on another machine in the exchange environment.
If anyone can provide any suggestions that may help me solve this problem I will be greatful.
Thank you