使用 Exchange 命令行管理程序获取邮件用户列表

了解如何使用 Exchange 命令行管理程序 cmdlet 创建返回 Exchange 邮箱用户列表的工具。

适用于: Exchange Online |Exchange Server 2013 |Office 365

使用调用 Exchange 命令行管理程序 cmdlet 的托管工具从 Exchange Online、作为 Office 365 的一部分的 Exchange Online 或 Exchange 2013 版本获取用户列表是一个两步过程。 首先,在 Exchange 服务器上建立远程运行空间;然后,运行 cmdlet 以检索远程运行空间中的用户信息。

若要连接到远程运行空间,必须使用符合组织安全要求的身份验证方案向 Exchange 服务器进行身份验证。

本文提供可用于设置远程运行空间和运行 Exchange 命令行管理程序 cmdlet 以从 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 命令行管理程序 cmdlet 的应用程序的计算机上的运行空间中。 应用程序应改为创建远程运行空间,如本文后面所述。

连接到 Exchange 服务器上的远程运行空间

用于连接到远程运行空间以使用 Exchange 命令行管理程序 cmdlet 的方法因你正在使用的身份验证方案而异。 本部分提供代码示例,演示在使用下表中列出的身份验证方法时如何连接到远程运行空间。

身份验证方法 适用对象 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 命令行管理程序运行空间。 然后,该方法调用 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 命令行管理程序运行空间。 然后,该方法调用 GetUserInformation 方法,如 从远程运行空间获取邮箱用户列表"部分中所定义,以返回远程服务器上的用户列表。

此方法需要以下参数:

  • thumbprint – 包含用于对应用程序进行身份验证的证书指纹的字符串。

  • certConnectionUri – 一个字符串,其中包含将对证书进行身份验证的服务器 URI。 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 命令行管理程序运行空间。 然后,该方法调用 GetUserInformation 方法,如 从远程运行空间获取邮箱用户列表"部分中所定义,以返回远程服务器上的用户列表。

此方法需要以下参数:

  • kerberosUri – 包含对应用程序进行身份验证的 Kerberos 服务器的 URI 的字符串。 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 方法邮箱用户返回的计数不超过。 为了简化此示例的代码,该方法不会筛选或以其他方式限制返回的邮箱用户。

另请参阅