2

I have this code, I send remotely a powershell command "date" to my exchange server (server01) and it works, I'm receiving a result in a messagebox.

but, if I send the command "Get-Mailbox" the debbugers stops with this error: The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program.

If I go to the server01 and runs powershell and execute "Get-Mailbox danielr" is the same error that I get. But Exchange Management Shell execute the Get-Mailbox command fine.

So, I guess I'm connected to the Window Powershell cmd..but, to execute the "Get-Mailbox danielr" and others Exchange Management commands I have to connect to the Exchange Management Shell.

What do I need to change to make it works? (to connect to the exchange management shell, not to powershell. THANKS A LOT!!

public void CreateMailBoxExchange()
    {
        string loginName = "administrator"; 
        string loginPassword = "123456";
        SecureString ssLoginPassword = new SecureString();
        foreach (char x in loginPassword)
            ssLoginPassword.AppendChar(x);


        PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", remoteMachineCredentials);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
        //connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();
        command.AddCommand("date");
        //command.AddCommand("Get-Mailbox");
        //command.AddParameter("Identity", "danielr");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            powershell.Invoke();
            var results = powershell.Invoke();
            runspace.Close();
            foreach (PSObject obj in results)
            {
                StringBuilder stringBuilder = new StringBuilder();
                //stringBuilder.AppendLine(obj.ToString());
                MessageBox.Show(obj.ToString());
            }

        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

3 Answers3

1

Depending on which version of Exchange you are running, you may need to execute the following code:

For Exch 2007

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin

For Exch 2010

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010

For Exch 2013 (try this)

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
websch01ar
  • 2,103
  • 2
  • 13
  • 22
0

You should be able to just include the Exchange Management snapin before trying the Get-Mailbox command. Here's how I do that in PowerShell:

Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin"
CitizenRon
  • 875
  • 7
  • 7
0

If you are remote from the Exchange Server which your code suggests then you'd have to use something like:

string loginName = "administrator"; 
string loginPassword = "123456";
SecureString ssLoginPassword = new SecureString();
foreach (char x in loginPassword)
    ssLoginPassword.AppendChar(x);

PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", remoteMachineCredentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Can you help me telling me where should I put the code (between that I got) Please! – Emmanuel_InfTech Sep 02 '14 at 17:15
  • OK let me try. Thanks Michael – Emmanuel_InfTech Sep 02 '14 at 17:48
  • I update my code with yours and the debbugger stops with this error: 'System.Management.Automation.Remoting.PSRemotingTransportException' en System.Management.Automation.dll Información adicional: Connecting to remote server server01 failed with the following error message : The WinRM client sent a request to an HTTP server and got a response saying the requested HTTP URL was not available. This is usually returned by a HTTP server that does not support the WS-Management protocol – Emmanuel_InfTech Sep 02 '14 at 17:53
  • Will also need to make sure that PS Remoting is enabled on the 2010 Exchange Server http://wiki.openiam.com/display/IDManager/Setting+Exchange+2010+Server+for+Remote+Powershell – Michael Petch Sep 02 '14 at 18:55
  • With :5985/wsman I get that error I mentioned.------ and with :5985/Powershell I get the follow: The WS-Management service cannot process the request. Cannot find the Microsoft.Exchange session configuration in the WSMan: drive on the server01 computer.------- I guess with /Powershell now I'm accessing but I need one more thing ("configurate microsoft.exchange session") do you know how can I do that? – Emmanuel_InfTech Sep 02 '14 at 20:31
  • On your server can you launch a powershell and issue this command: `Register-PSSessionConfiguration -Name Microsoft.PowerShell` . When prompted just select A for "Yes to All" – Michael Petch Sep 02 '14 at 21:30
  • Thanks Mike, I already do that and now I have... [image](http://k32.kn3.net/C/F/D/1/A/6/4D6.png) ....but as you can see that new MICROSOFT.EXCHANGE has not "access allowed" permission. How can I set that? – Emmanuel_InfTech Sep 02 '14 at 22:02
  • You'll probably have to ask an Exchange user group/forum about that. I don't have an Exchange Server available to test with at the moment. – Michael Petch Sep 02 '14 at 23:34