C # Actions AD and Exchange Server summary (ii)

Source: Internet
Author: User

C # Actions AD and Exchange Server summary (ii)

Previous C # operations AD and Exchange Server summary (i) write the action on AD, after you create a new ad user, you usually need to open an Exchange mailbox for this user, and then write how to operate exchange remotely.

Third, the Exchange server operation

Operation Exchange uses a new DLL that requires the installation of the Windows Power shell after installation in the path: C:\Program Files (x86) \reference assemblies\microsoft\ windowspowershell\v1.0 (test machine for 64-bit) found System.Management.Automation.dll, introduced in the project.

1. Create a new mailbox for AD users

In the previous section we joined and enabled a user Employee01 in AD, and below we continue to write code in the project created in the previous section to open the mailbox for that user.

Create a new ExchangeMange.cs file, and add the following method:

public void Addexchangeuser (string identity, String alias) {string runasusername = @ "contoso\ Administrator Account";            String runaspassword = "Administrator Password";            SecureString Ssrunaspassword = new SecureString ();            foreach (char x in Runaspassword) {Ssrunaspassword.appendchar (x);            } pscredential Credentials = new Pscredential (Runasusername, Ssrunaspassword); var conninfo = new Wsmanconnectioninfo (New Uri ("Http://exchange server Ip/powershell"), "http://schemas.microsof            T.com/powershell/microsoft.exchange ", credentials);            Conninfo.authenticationmechanism = Authenticationmechanism.basic;            var runspace = Runspacefactory.createrunspace (Conninfo);            var command = new command ("Enable-mailbox"); Command.            Parameters.Add ("Identity", identity); Command.            Parameters.Add ("Alias", alias); Runspace.            Open (); var pipEline = Runspace.            Createpipeline ();            PIPELINE.COMMANDS.ADD (command); var results = pipeline.            Invoke (); Console.WriteLine ("Number of channel errors:" + pipeline.)            Error.count); Runspace.        Dispose (); }

Code Explanation:

    • First, you need to generate administrator credentials, and this administrator must have permissions to administer the Exchange server
    • Generate a connection type, incoming Exchange Server IP, scheme to be used, and administrator credentials
    • Open a command space, create a pipeline channel, pass in the PowerShell command to run, execute the command
    • Releasing connection Resources

The PowerShell command corresponding to this code corresponds to a locally run "enable-mailbox-identity ' rzh.com/companya/employee01 '-alias ' Employee01 '"

Write the test code:

static void Main (string[] args)        {            Exchangemange manage = new Exchangemange ();            Console.WriteLine ("Start to enable user Maibox ...");            Try            {                manage. Addexchangeuser ("[Email protected]", "Employee01");            }            catch (System.Management.Automation.RuntimeException ex)            {                Console.WriteLine ("Enable user Maibox error ...") ;                Console.WriteLine (ex);                Console.ReadLine ();            }            Console.WriteLine ("Finish to enable user Maibox ...");            Console.ReadLine ();        }

The run code will report the following error at this time

The error message is detailed, explaining the cause of the error, and the server does not support the authentication mechanism requested by the client.

Some configuration is required for the client and server side.

2. Configuring Clients and servers

Client Configuration:

Run gpedit.msc into local Group Policy manager, Computer Configuration management templates Windows components WinRM winrm client

Enable allow for encrypted communication, enable trusted hosts, and add the Exchange Server IP address to trustedhostslist

Server-side configuration:

Enable IIS Basic Authentication

SSL is not required in SSL settings

Run the code again

Using the example above, you can see the whole process of manipulating exchange with the type and method of System.Management.Automation, and the only difference is the command command, as long as we know the PowerShell commands, You can replace the command segment in the code above to manipulate it.

Iii. Summary and Induction

1. Summary of AD Operations

    • Understanding DirectoryEntry Types
    • Learn how to create ad various types of methods
    • Learn to set the properties of an ad type

2. Summary of Exchange operations

    • Understand the types and methods under System.Management.Automation
    • Configuring client and Server Side
    • Mastering PowerShell commands

This is my own little summary, hoping to help readers

C # Actions AD and Exchange Server summary (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.