I am struggling getting Exchange mManagement Shell commands to run in my C# code. Here' the problem.
I have access to a test Exchange Server running Exchange 2010.
I have run my Exchange Mgmt Shell commands directly on the server inside the Exchange Mgmt Shell
Simple Example: get-mailbox
I created a console app to run my PS commands
I placed the exe file on the Exchange Server and tried to run it
I get an error on the PSSnapInInfo info = rsConfig.AddPSSnapIn line of code:
//Creating and Opening a Runspace
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
Assuming the server is 64-bit, it might be related to how the snap-in was registered. For example, if you're running the x86 version of PowerShell it runs in WOW. Therefore, when you run InstallUtil to register the snap-in it gets registered specifically for x86, which in turn is not available to x64. Assuming this, if your C# application was compiled specifically for the x86 platform I would bet that it would work. This is one solution, but not always acceptable.
Ensure that you're running the correct version of PowerShell (x86/x64) and rerun the snap-in registration with the correct version of InstallUtil. For x64, you'll need to run the InstallUtil that lives under the .NET Framework64 directory.
One way to check you're in the correct version of PowerShell is to examine the environment variable, env:PROCESSOR_ARCHITECTURE in your shell.
> get-item env:PROCESSOR_ARCHITECTURE Name Value ---- ----- PROCESSOR_ARCHITECTURE AMD64
> get-item env:PROCESSOR_ARCHITECTURE Name Value ---- ----- PROCESSOR_ARCHITECTURE x86