Question

So, I import the System.Management.Automation dll and I'm trying to run a New-Mailbox command with params

so I use:

RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException psEx = null;
config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out psEx);

That's all fine and dandy... but when I go to run the application I get the following: 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.

So, I did some research online and found that I need to change from Any CPU to x86 as the platform target.

However, when I do that I get a HTTP Error 503. The service is unavailable. error

I am almost positive that I have to run it as a 32 bit process so that it can use the snapin (which from other reading seems to be what the snapin is running under)

I did change the app pool to Enable 32-bit Applications to True. Which is when I get the error.

I've read other posts... but, I'm not sure how to get past this Service unavailable thing.

I've tried using a x64 build and get Could not load file or assembly 'EmailAdminWeb2' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Was it helpful?

Solution

You don't use this at all:

config.AddPSSnapIn("your snapin here", out psEx);

instead.... just use a connection as follows:

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://yourdomainhere/Powershell/Microsoft.Exchange"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", PsCreds);
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

Now run your commands and you're good to go.

Quick notes:

  • Make sure you're app is targeting x64 not Any CPU or x86
  • If you're using .net 4.5 (or 4) make sure you're app pools are set properly (v4.0 not v2.0) and that you have Enable 32bit apps set to false
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top