Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Exchange Powershell command from C#

I am trying to run EMC commands in C#. I am running this from my personal PC that has exchange management tools installed on it.

Our exchange servers have 2007 running on them.

The thing is, when I run Powershell or EMC, I need to run as a different user that has exchange server 2007 permissions since my individual profile doesn't have these permissions.

That being said, this is my code I have running on my personal PC:

RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException snapEx = null;

PSSnapInInfo info = config.AddPSSnapIn("Microsoft.Exchange.Management.Powershell.Admin", out snapEx);

Runspace runspace = RunspaceFactory.CreateRunspace(config);
runspace.Open();

Command createCMD = new Command("Get-Mailbox ID");

Pipeline pipe = runspace.CreatePipeline();
pipe.Commands.Add(createCMD);

Collection<PSObject> results = pipe.Invoke();

The error I am getting is:

The Windows PowerShell snap-in Microsoft.Exchange.Management.Powershell.Admin is not installed on this computer.

I am getting it when I try and add the Microsoft.Exchange.Management.Powershell.Admin snapIn.

I feel this has something to do with my permissions on my individual profile, but I am not entirely sure. If it is true, how do I fix this.


EDIT

The reason I say it sounds like permissions is because I am able to open powershell and add the snapin. However when I run a command such as get-mailboxstatistics myUserId it throws an error saying MyServer\MyStorageGroup does not exist. However, when I shift-rightCLick and run as different user and use the credentials of my exchange admin account, I am able to run these commands.

like image 299
Johnrad Avatar asked Nov 26 '13 21:11

Johnrad


2 Answers

If an error says it is not installed on your computer, why do you suspect it has something to do with permissions?

As this post suggests, please check if you have installed the 2007 version of the tools, as the Snapin in question is not available on the 2010 version.

Try the following steps:

Open up a powershell editor of your choice and add the PSSnapin there. If it works, the Snapin is available, if not, it is really not installed on your machine.

If it is available try to set your build configuration from x86 to 64bit or vice versa.

Eventually you can install the .dll in question by hand. Referring to this answer from Keith hill you have to issue the following Powershell commands

$snapinPath = 'Microsoft.Exchange.Management.PowerShell.Admin.dll'
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i $snapinPath
like image 154
Marco Avatar answered Oct 23 '22 06:10

Marco


Errors like this are often a 32 bit/64 bit problem. For example, the snapin might be registered as a 32 bit and your C# program is 64 bit or vice versa.

Sometimes you can fix this by running the other version of InstallUtil, e.g.

$snapinPath = 'Microsoft.Exchange.Management.PowerShell.Admin.dll'
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe /i $snapinPath

After fixing that, I think you'll hit another problem with how you're creating the command. You don't specify arguments when creating a command. Instead, you write something like:

Command createCMD = new Command("Get-Mailbox");
createCMD.Parameters.Add(null, "ID");
like image 4
Jason Shirk Avatar answered Oct 23 '22 04:10

Jason Shirk