Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load Exchange powershell snap-in: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception

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

like image 395
Nicholas J. Markkula Avatar asked Sep 18 '12 15:09

Nicholas J. Markkula


1 Answers

After further investigation I figured out that .NET 4.5 is an in place update meaning that .NET 4.0 is overwritten and replace with .NET 4.5 when installed. I don't know what changed in .NET 4.5 that causes this but the issue is resolved by uninstalling .NET 4.5 and switching back to Visual Studio 2010. Hopefully Microsoft will have some update in the near future that will resolve the issue and allow me to use Visual Studio 2012 again.

See the following article for more info about the in place update. http://www.devproconnections.com/article/net-framework/net-framework-45-versioning-faces-problems-141160

like image 68
Nicholas J. Markkula Avatar answered Sep 19 '22 19:09

Nicholas J. Markkula