Run the Exchange Powershell command with C #

I am trying to run EMC commands in C #. I run this from my personal computer on which the exchange management tools are installed.

Our exchange servers work with them for 2007.

The fact is that when I start Powershell or EMC, I need to start as another user with permissions to the Exchange 2007 server, since my individual profile does not have these permissions.

As they say, this is my code that I ran on my personal computer:

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 Microsoft.Exchange.Management.Powershell.Admin snap-in is not installed on this computer.

I get it when I try to add Microsoft.Exchange.Management.Powershell.Admin snapIn.

I feel this has something to do with my permissions for my individual profile, but I'm not quite sure. If it's true, how can I fix it.


EDIT

The reason I say this seems to be right, because I can open powershell and add snapin. However, when I run a command, such as get-mailboxstatistics myUserId , it gives the error message MyServer\MyStorageGroup . However, when I shift-rightCLick and act as another user and use the credentials of my Exchange admin account, I can execute these commands.

+6
source share
2 answers

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

As this post suggests, please check if the 2007 version of the tools was installed, since this Snapin is not available in the 2010 version.

Try the following:

Open the powershell editor of your choice and add PSSnapin. If it works, Snapin is available; if not, it really is not installed on your computer.

If this is available, try setting the build configuration from x86 to 64 bits or vice versa.

In the end, you can install the .dll into the question manually. Referring to this answer from Keith Hill, you should issue the following Powershell commands

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

Errors like this often pose a 32 bit / 64 bit problem. For example, snapin can be registered as 32 bits, and your C # program - 64 bits or vice versa.

Sometimes you can fix this by running another version of InstallUtil, for example.

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

After fixing this, I think you will encounter yet another problem with how you create the team. You do not specify arguments when creating the command. Instead, you write something like:

 Command createCMD = new Command("Get-Mailbox"); createCMD.Parameters.Add(null, "ID"); 
+4
source

Source: https://habr.com/ru/post/958886/


All Articles