user489041
user489041

Reputation: 28294

Create Exchange Mailboxes on Remote Exchange 2010 Server in C#

Title pretty much explains it all. I have a C# application that is not running on the Exchange Server. I need to be able to create mailboxes. I tried to use this tutorial, but it requires the PowerShell IIS Virutal directory to:

  1. Not Require SSL
  2. Allow Basic Authentication

Which are things that we cant do. This leads me to two possible solutions. Is there a way to modify the tutorial listed above to not require those two restrictions, or is there a way to do it without using power shell at all?

Here is the code, in case you dont feel like going to the link:


using System;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace PowerShellTest
{
    class Program
    {
        static void Main(string[] args)
        {

            // Prepare the credentials that will be used when connecting
            // to the server. More info on the user to use on the notes
            // below this code snippet.
            string runasUsername = @"username";
            string runasPassword = "password";
            SecureString ssRunasPassword = new SecureString();
            foreach (char x in runasPassword)
                ssRunasPassword.AppendChar(x);
            PSCredential credentials =
                new PSCredential(runasUsername, ssRunasPassword);

            // Prepare the connection
            var connInfo = new WSManConnectionInfo(
                new Uri("http://ServersIpAddress/PowerShell"),
                "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
                credentials);
            connInfo.AuthenticationMechanism =
                AuthenticationMechanism.Basic;

            // Create the runspace where the command will be executed
            var runspace = RunspaceFactory.CreateRunspace(connInfo);

            // generate the command parameters
            var testNumber = 18;
            var firstName = "Test";
            var lastName = "User" + testNumber;
            var username = "tuser" + testNumber;
            var domainName = "pedro.test.local";
            var password = "ActiveDirectoryPassword1234";
            var ssPassword = new SecureString();
            foreach (char c in password)
                ssPassword.AppendChar(c);

            // create the PowerShell command
            var command = new Command("New-Mailbox");
            command.Parameters.Add("Name", firstName + " " + lastName);
            command.Parameters.Add("Alias", username);
            command.Parameters.Add(
                "UserPrincipalName", username + "@" + domainName);
            command.Parameters.Add("SamAccountName", username);
            command.Parameters.Add("FirstName", firstName);
            command.Parameters.Add("LastName", lastName);
            command.Parameters.Add("Password", ssPassword);
            command.Parameters.Add("ResetPasswordOnNextLogon", false);
            command.Parameters.Add(
                "OrganizationalUnit", "NeumontStudents");

            // Add the command to the runspace's pipeline
            runspace.Open();
            var pipeline = runspace.CreatePipeline();
            pipeline.Commands.Add(command);

            // Execute the command
            var results = pipeline.Invoke();

            runspace.Dispose();

            if (results.Count > 0)
                Console.WriteLine("SUCCESS");
            else
                Console.WriteLine("FAIL");

        }
    }
}


Upvotes: 1

Views: 7002

Answers (2)

Start-Automating
Start-Automating

Reputation: 8367

The code from that blog post is a little broken. The Pipeline class is an overly complex way to create a command, and the way that it's written involves creating a pair of runspaces (one local, one remote), instead of just the remote one.

Additionally, "Basic" and "http" in IIS do not mean "no security and no encryption in PowerShell". Everything sent over the WinRM layer is encrypted by default.

This Link from the Exchange Team covers the right way to do this in C# fairly well.

So:

  • You don't have to worry about the IIS "Basic", because there's another layer of security.
  • You can cut your code in half and make it faster if you use the C# from the Exchange team

Also to be 100% crystal clear:

You cannot manage Exchange 2010 remotely except thru PowerShell.

Hope This Helps

Upvotes: 1

Dirk
Dirk

Reputation: 2388

You can set up a so-called runspace with many different AuthenticationMechanism's

Visit the MSDN site for code samples using:

  • Basic Authentication (which is used in your example)
  • Certificate Authentication
  • Kerberos Authentication
  • Negotiated Authentication

http://msdn.microsoft.com/en-us/library/ff326159(v=exchg.140).aspx

In any case, no need to give up on PowerShell just yet.

Upvotes: 1

Related Questions