Export (0) Print
Expand All

Calling Exchange Management Shell Cmdlets from Managed Code

Exchange Server 2010

This section contains three examples that show how methods can use Exchange Management Shell cmdlets from an application.

The examples in this section use the following methods:

  • GetUsersUsingBasicAuth – Creates an Exchange Management Shell runspace on a remote server by using Windows basic authentication. This method returns a collection of PSObject instances that represent Microsoft Exchange Server 2010 mailbox users.

  • GetUsersUsingCertificate – Creates an Exchange Management Shell runspace on a remote server by using certificate authentication. This method returns a collection of PSObject instances that represent Exchange mailbox users.

  • GetUsersUsingKerberos – Creates an Exchange Management Shell runspace on a remote server by using Kerberos authentication. This method returns a collection of PSObject instances that represent Exchange mailbox users.

  • GetUsersUsingNegotiatedAuth – Creates an Exchange Management Shell runspace on a remote server by using negotiated authentication. This method returns a collection of PSObject instances that represent Exchange mailbox users.

  • GetUserInformation – Returns a collection of PSObject instances that represent Exchange mailbox users. This method is called by both the GetUsersUsingLiveID and GetUsersUsingCertificate methods.

The examples in this section require a reference to the following namespaces:

  • System.Collections.ObjectModel

  • System.Management.Automation

  • System.Management.Automation.Remoting

  • System.Management.Automation.Runspaces

Note Note

When you are using Microsoft Visual Studio to create an application, you must add a reference to the System.Mangagement.Automation.dll assembly to the project. The assembly can be found in one of the following locations:

  • For the Windows XP and Windows Vista operating systems, in the Windows PowerShell installation directory ($PSHOME).

  • For the Windows 7 operating system, in the following folder: Windows\assembly\GAC_MSIL\System.Management.Automation.

The following code example shows a method that creates an Exchange Management Shell runspace on a remote server. The method then calls the GetUserInformation method to return a list of users on the remote server.

This method requires the following parameters:

  • liveIDConnectionUri – A string that contains the URI of the server that will authenticate the application.

  • schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema.

  • credentials – A PSCredential object that contains the credentials of the user who is running the application.

  • count – The number of Exchange mailbox users to return.

public Collection<PSObject> GetUsersUsingBasicAuth(
    string liveIDConnectionUri, string schemaUri, PSCredential credentials, int count)
{
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
        new Uri(liveIDConnectionUri),
        schemaUri, credentials);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
    {
        return GetUserInformation(count, runspace);
    }
}
Note Note

The following is the URI of the Exchange Management Shell schema: http://schemas.microsoft.com/powershell/Microsoft.Exchange.

The following code example shows a method that creates an Exchange Management Shell runspace on a remote server. The method then calls the GetUserInformation method to return a list of users on the remote server.

This method requires the following parameters:

  • thumbprint – A string that contains the thumbprint of the certificate that is used to authenticate the application.

  • certConnectionUri – A string that contains the URI of the server that will authenticate the certificate.

  • schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema.

  • count – The number of Exchange mailbox users to return.

public Collection<PSObject> GetUsersUsingCertificate(
    string thumbprint, string certConnectionUri, string schemaUri, int count)
{
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
        new Uri(certConnectionUri),
        schemaUri,
        thumbprint)

    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
    {
        return GetUserInformation(count, runspace);
    }
}
Note Note

The following is the URI of the Exchange Management Shell schema: http://schemas.microsoft.com/powershell/Microsoft.Exchange.

The following code example shows a method that creates an Exchange Management Shell runspace on a remote server. The method then calls the GetUserInformation method to return a list of users on the remote server.

This method requires the following parameters:

  • kerberosUri – A string that contains the URI of the Kerberos server that will authenticate the application.

  • schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema.

  • credentials – A PSCredential object that contains the credentials of the user who is running the application.

  • count – The number of Exchange mailbox users to return.

public Collection<PSObject> GetUsersUsingKerberos(
    string kerberosUri, string schemaUri, PSCredential credentials, int count)
{
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
        new Uri(kerberosUri),
        schemaUri, credentials);
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
    {
        return GetUserInformation(count, runspace);
    }
}

The following code example shows a method that creates an Exchange Management Shell runspace on a remote server. The method then calls the GetUserInformation method to return a list of users on the remote server.

This method requires the following parameters:

  • targetUri – A string that contains the URI of the server that will authenticate the application.

  • schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema.

  • credentials – A PSCredential object that contains the credentials of the user who is running the application.

  • count – The number of Exchange mailbox users to return.

public Collection<PSObject> GetUsersUsingNegotiatedAuth(int count, PSCredential credential)
{
  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
    new Uri(targetUri),
    schemaUri, credentials);

   connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;

  using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
  {
    return GetUserInformation(count, runspace);
  }
}

The following code example shows a method that returns a collection of PSObject instances that represent Exchange mailbox users. This method is called by the GetUsersUsingBasicAuth, GetUsersUsingCertificate, and GetUsersUsingKerberos methods to return the list of users from the remote server.

This method requires the following parameters:

  • count – The number of Exchange mailbox users to return.

  • runspace – The remote runspace that is established for the remote Exchange server.

public Collection<PSObject> GetUserInformation(int count, Runspace runspace)
{
    using (PowerShell powershell = PowerShell.Create())
    {
        powershell.AddCommand("Get-Users");
        powershell.AddParameter("ResultSize", count);


        runspace.Open();


        powershell.Runspace = runspace;


        return powershell.Invoke();
    }
}

The GetUserInformation method will return no more than count mailbox users. To simplify the code for this example, the method does not filter or otherwise limit the mailbox users that are returned.

Show:
© 2014 Microsoft