Exchange 管理シェルを使用してメール ユーザーの一覧を取得する

Exchange 管理シェル コマンドレットを使用して、Exchange メールボックスのユーザーの一覧を取得するツールを作成する方法について説明します。

適用対象: Exchange Online、Exchange Server 2013、Office 365

Exchange 管理シェル コマンドレットを呼び出す管理ツールを使用して、Exchange Online、Office 365 の一部としての Exchange Online、Exchange 2013 以降のバージョンの Exchange からユーザーの一覧を取得することは、2 段階のプロセスです。 最初に、Exchange サーバー上のリモートの実行空間を確立し、次にリモートの実行空間内でユーザー情報を取得するコマンドレットを実行します。

リモートの実行空間に接続するには、組織のセキュリティ要件を満たしている認証スキームを使用して、Exchange サーバーとの認証を行う必要があります。

この記事では、リモートの実行空間を設定し、Exchange サーバーからユーザーの一覧を取得するための Exchange 管理シェル コマンドレットを実行するのに使用できるコード例を提供します。

メールボックスのユーザーの一覧を取得するための前提条件

このタスクを実行するには、次の名前空間への参照が必要です。

  • System.Collections.ObjectModel
  • System.Management.Automation
  • System.Management.Automation.Remoting
  • System.Management.Automation.Runspaces

注:

Visual Studio を使用してアプリケーションを作成する場合は、System.Management.Automation.dll アセンブリへの参照をプロジェクトに追加する必要があります。 アセンブリは次の場所のいずれかにあります。

  • オペレーティング システムが Windows XP および Windows Vista の場合、Windows PowerShell のインストール ディレクトリ ($PSHOME)。
  • オペレーティング システムが Windows 7 および Windows 8 の場合は次のフォルダー:Windows\assembly\GAC_MSIL\System.Management.Automation。

Exchange 2013 の管理スナップインを、Exchange 管理シェル コマンドレットを自動化するアプリケーションを実行中のコンピューターの実行空間に読み込まないでください。 代わりに、この記事で後述するように、アプリケーションはリモートの実行空間を作成する必要があります。

Exchange サーバーのリモートの実行空間に接続する

Exchange 管理シェル コマンドレットを使用するリモートの実行空間への接続に使用する方法は、使用している認証スキームによって異なります。 このセクションでは、次の表に記載されている認証方法を使用している場合に、リモートの実行空間に接続する方法を示すコード例を提供します。

認証方法 適用対象 URI
基本認証を使用して Exchange Online のリモートの実行空間に接続します。 Exchange Online サーバー https://outlook.office365.com/PowerShell-LiveID
https://<server>/PowerShell-LiveID
証明書による認証を使用してリモートの実行空間に接続します。 Exchange Online と Exchange オンプレミス サーバー https://outlook.office365.com/PowerShell
https://<server>/PowerShell
http://<server>/PowerShell
Kerberos 認証を使用して Exchange サーバーのリモートの実行空間に接続する Exchange Online と Exchange オンプレミス サーバー https://<server>/PowerShell
http://<server>/PowerShell

基本認証を使用して Exchange Online のリモートの実行空間に接続します。

次のコード例では、GetUsersUsingBasicAuth メソッドを定義します。これは、基本認証を使用してリモート Exchange Online サーバーに Exchange Management Shell 実行空間を作成します。 次に、 メソッドは、リモート実行スペースからメールボックス ユーザーの一覧を取得するセクションで定義されている GetUserInformation メソッドを呼び出して、リモート サーバー上のユーザーの一覧を返します。

このメソッドは、以下のパラメーターを必要とします。

  • liveIDConnectionUri – アプリケーションを認証するExchange Online サーバーの URI を含む文字列。 Exchange Online が Office 365 で実行されている場合、URI は https://outlook.office365.com/PowerShell-LiveID です。それ以外の場合、URI は https://<servername>/PowerShell-LiveID です。
  • schemaUri – Exchange 管理シェルのスキーマを定義するスキーマ ドキュメントの URI を含む文字列。 スキーマ URI は https://schemas.microsoft.com/powershell/Microsoft.Exchange です。
  • credentials – アプリケーションを実行しているユーザーの資格情報を含む PSCredential オブジェクト。
  • count – 取得する Exchange メールボックスのユーザー数。
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);
    }
}
  Function GetUsersUsingBasicAuth( _
    ByVal LiveIDConnectionUri As String, ByVal ScehmaUri As String, _
    ByVal Credentials As PSCredential, ByVal Count As Integer) As Collection(Of PSObject)
    Dim ConnectionInfo As WSManConnectionInfo = _
        New WSManConnectionInfo(New Uri(LiveIDConnectionUri), ScehmaUri, Credentials)
    ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic
    Dim RemoteRunspace As Runspace
    RemoteRunspace = RunspaceFactory.CreateRunspace(ConnectionInfo)
    Return GetUserInformation(Count, RemoteRunspace)
  End Function

証明書による認証を使用してリモートの実行空間に接続します。

次のコード例では 、GetUsersUsingCertificate メソッドを 定義します。このメソッドは、証明書を使用してリモート サーバー上に Exchange Management Shell 実行空間を作成します。 次に、 メソッドは、リモート実行スペースからメールボックス ユーザーの一覧を取得するセクションで定義されている GetUserInformation メソッドを呼び出して、リモート サーバー上のユーザーの一覧を返します。

このメソッドは、以下のパラメーターを必要とします。

  • thumbprint – アプリケーションを認証するために使用される証明書の拇印を含む文字列。

  • certConnectionUri – アプリケーションを認証するサーバーの URI を含む文字列。 URI は、次の表に記載されているいずれかになります。

    表 1. certConnectionUri URI

    Server URI
    SSL を使用しない Exchange サーバー http://<servername>/PowerShell
    SSL を使用する Exchange サーバー https://<servername>/PowerShell
    Office 365 の一部としての Exchange Online https://outlook.office365.com/PowerShell
  • schemaUri – Exchange 管理シェルのスキーマを定義するスキーマ ドキュメントの URI を含む文字列。 スキーマ URI は https://schemas.microsoft.com/powershell/Microsoft.Exchange です。

  • count – 取得する Exchange メールボックスのユーザー数。

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);
    }
}
  Function GetUsersUsingCertificate( _
    ByVal Thumbprint As String, ByVal CertConnectionUri As String, _
    ByVal SchemaUri As String, ByVal Count As Integer) As Collection(Of PSObject)
    Dim ConnectionInfo As WSManConnectionInfo
    ConnectionInfo = New WSManConnectionInfo(New Uri(CertConnectionUri), SchemaUri, Thumbprint)
    Dim RemoteRunspace As Runspace
    RemoteRunspace = RunspaceFactory.CreateRunspace(ConnectionInfo)
    Return GetUserInformation(Count, RemoteRunspace)
  End Function

Kerberos 認証を使用して Exchange サーバーのリモートの実行空間に接続する

次のコード例では 、GetUsersUsingKerberos メソッドを 定義します。このメソッドは、Kerberos 認証を使用してリモート サーバー上に Exchange Management Shell 実行空間を作成します。 次に、 メソッドは、リモート実行スペースからメールボックス ユーザーの一覧を取得するセクションで定義されている GetUserInformation メソッドを呼び出して、リモート サーバー上のユーザーの一覧を返します。

このメソッドは、以下のパラメーターを必要とします。

  • kerberosUri – アプリケーションを認証する Kerberos サーバーの URI を含む文字列。 URI は、次の表に記載されているいずれかになります。

    表 2. kerberosUri URI

    Server URI
    SSL を使用しない Exchange サーバー http://<servername>/PowerShell
    SSL を使用する Exchange サーバー https://<servername>/PowerShell
  • schemaUri – Exchange 管理シェルのスキーマを定義するスキーマ ドキュメントの URI を含む文字列。 スキーマ URI は https://schemas.microsoft.com/powershell/Microsoft.Exchange です。

  • credentials – アプリケーションを実行しているユーザーの資格情報を含む PSCredential オブジェクト。

  • count – 取得する Exchange メールボックスのユーザー数。

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);
    }
}
  Function GetUsersUsingKerberos( _
    ByVal KerberosUri As String, ByVal ScehmaUri As String, _
    ByVal Credentials As PSCredential, ByVal Count As Integer) As Collection(Of PSObject)
    Dim ConnectionInfo As WSManConnectionInfo = _
        New WSManConnectionInfo(New Uri(KerberosUri), ScehmaUri, Credentials)
    ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos
    Dim RemoteRunspace As Runspace
    RemoteRunspace = RunspaceFactory.CreateRunspace(ConnectionInfo)
    Return GetUserInformation(Count, RemoteRunspace)
  End Function

リモートの実行空間からメールボックスのユーザーの一覧を取得します。

次のコード例は、GetUserInformation メソッドを定義します。これは、Exchange メールボックスのユーザーを表す PSObject のコレクションを返します。 このメソッドは、GetUsersUsingBasicAuthGetUsersUsingCertificateGetUsersUsingKerberos メソッドによって呼び出され、リモート サーバーからユーザーの一覧を返します。

このメソッドは、以下のパラメーターを必要とします。

  • count – 取得する Exchange メールボックスのユーザー数。
  • runspace – リモートの Exchange サーバーに対して確立されたリモート実行空間。
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();
    }
}
Function GetUserInformation(ByVal Count As Integer, ByVal RemoteRunspace As Runspace) As Collection(Of PSObject)
    Dim RemotePowerShell As PowerShell = PowerShell.Create
    RemotePowerShell.AddCommand("Get-Users")
    RemotePowerShell.AddParameter("ResultSize", Count)
    ' Open the remote runspace on the server.
    RemoteRunspace.Open()
    ' Associate the runspace with the Exchange Management Shell.
    RemotePowerShell.Runspace = RemoteRunspace
    ' Invoke the Exchange Management Shell to run the command.
    Return RemotePowerShell.Invoke
End Function

GetUserInformation メソッドは、count のメールボックスのユーザーのみを返します。 この例のコードを簡略化するために、このメソッドは、返されるメールボックス ユーザーのフィルター処理やその他の方法による制限を行いません。

関連項目