Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to connect to connect to power-shell using .Net C#. I need to get details of mailbox, create new mailbox and delete existing mailbox.
I use "Get-Mailbox","New-Mailbox" & "Remove-Mailbox".

But when I invoke the command it throws me below error.

The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,verify that the path is correct and try again.

I connecting to power-shell remotely.

What I have tried:

Runspace runspace = null;
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
PSCredential remoteCredential = new PSCredential(@"userid", StringToSecureString("password"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "ip address",5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
connectionInfo.OperationTimeout = 4 * 60 * 1000;
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
runspace.Open();
    using (PowerShell ps = PowerShell.Create())
    {
       ps.Runspace = runspace;
       ps.Commands.AddCommand("Get-Mailbox");
       ps.Commands.AddParameter("Identity", "user@domain.com");
 
      var result = ps.Invoke();
      foreach (PSObject res in result)
      {
        Console.WriteLine("{0,-25}1}",res.Members["DisplayName"].Value,res.Members["PrimarySMTPAddress"].Value);
       }
      }
            runspace.Close();
            runspace.Dispose();

And this

string shellUri = "http://schemas.microsoft.com/powershellMicrosoft.PowerShell";
PSCredential remoteCredential = new PSCredential(@"userid", StringToSecureString("password"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "ip address", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
connectionInfo.OperationTimeout = 4 * 60 * 1000;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
runspace.Open();

            //Get Mail Box
            Pipeline pipelineGetMailBox = runspace.CreatePipeline();

            Command getMailBox = new Command("Get-Mailbox");
            getMailBox.Parameters.Add("OrganizationalUnit", "OU Name");
            getMailBox.Parameters.Add("Identity", "user@domain.com");

            pipelineGetMailBox.Commands.Add(getMailBox);

            Collection<PSObject> mailboxGetResult = pipelineGetMailBox.Invoke();

            pipelineGetMailBox.Dispose();

            runspace.Close();
            runspace.Dispose();
Posted
Updated 12-Jan-17 21:48pm
v2

1 solution

Where are you deleting the existing mailboxes?
Also check if the command you currently trying to fire works manually in the powershell.

You also have to fire is as a 'dot' script. That would be:

. .\Get-Mailbox.{extension}


You should also use set-ExecutionPolicy Unrestricted or set-ExecutionPolicy AllSigned
Checkout: Execution Policy instructions

First make sure you can run it manually in the PowerShell. Than copy the script to your code with the same params etc.
 
Share this answer
 
Comments
Member 12586674 16-Jan-17 5:54am    
Hi,
I'm able to run command manually in Exchange PowerShell. But getting same error when trying from my application.
Wessel Beulink 17-Jan-17 4:41am    
What command you run manually can you post it?
Member 12586674 19-Jan-17 0:21am    
Get-Mailbox -Identity email@domain.com
Wessel Beulink 24-Jan-17 8:14am    
Sorry for my late reaction. Are you on a Exchange Server 2016 else Get-Mailbox is not a cmdlet as shown in your warning. See this article: bb123685
Member 12586674 30-Jan-17 4:42am    
Hi Wessel.
I use exchange 2010.
I found one link but even that solution doesn't seem to fit in
http://stackoverflow.com/questions/10408103/c-sharp-and-powershell-on-exchange-2010-the-term-group-object-is-not-recogni

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900