使用 Exchange 命令行管理程序获取邮件用户列表Get a list of mail users by using the Exchange Management Shell

了解如何使用 Exchange 命令行管理程序 cmdlet 来创建可返回 Exchange 邮箱用户列表的工具。Learn how to use Exchange Management Shell cmdlets to create a tool that returns a list of Exchange mailbox users.

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

通过使用调用 Exchange 命令行管理程序 cmdlet 的托管工具,从 Exchange Online、Exchange Online (Office 365 的一部分)或从 Exchange 2013 开始的 Exchange 版本获取用户列表是一个包含两个步骤的过程。Getting a list of users from Exchange Online, Exchange Online as part of Office 365, or a version of Exchange starting with Exchange 2013 by using a managed tool that calls an Exchange Management Shell cmdlet is a two-step process. 首先,在 Exchange 服务器上建立远程运行空间;然后,运行 cmdlet 以检索远程运行空间中的用户信息。First, you establish a remote runspace on an Exchange server; then, you run the cmdlet to retrieve the user information in the remote runspace.

若要连接到远程运行空间,必须使用符合组织的安全要求的身份验证方案对 Exchange 服务器进行身份验证。To connect to the remote runspace, you have to authenticate with the Exchange server by using the authentication scheme that meets the security requirements of your organization.

本文提供了可用于设置远程运行空间并运行 Exchange 命令行管理程序 cmdlet 以从 Exchange 服务器获取用户列表的代码示例。This article provides code examples that you can use to set up a remote runspace and run an Exchange Management Shell cmdlet to get a list of users from an Exchange server.

获取邮箱用户列表的先决条件Prerequisites for getting a list of mailbox users

若要执行此任务,您需要一个对以下命名空间的引用:To perform this task, you need a reference to the following namespaces:

  • ObjectModelSystem.Collections.ObjectModel
  • 系统管理。自动化System.Management.Automation
  • 系统管理. RemotingSystem.Management.Automation.Remoting
  • 系统管理. 自动化System.Management.Automation.Runspaces

备注

使用 Visual Studio 创建应用程序时,必须在项目中添加对 system.string 程序集的引用。When you are using Visual Studio to create an application, you must add a reference to the System.Management.Automation.dll assembly to the project. 可以在以下位置之一找到程序集:The assembly can be found in one of the following locations:

  • 对于 Windows XP 和 Windows Vista 操作系统,Windows PowerShell 安装目录($PSHOME)。For Windows XP and Windows Vista operating systems, the Windows PowerShell installation directory ($PSHOME).
  • 对于 Windows 7 和 Windows 8 操作系统,以下文件夹: Windows\assembly\ GAC_MSIL \System.Management.Automation。For the Windows 7 and Windows 8 operating systems, the following folder: Windows\assembly\GAC_MSIL\System.Management.Automation.

请勿将 Exchange 2013 管理管理单元加载到运行自动执行 Exchange 命令行管理程序 cmdlet 的应用程序的计算机上的运行空间中。Do not load the Exchange 2013 Management snap-in into the runspace on computers that are running applications that automate Exchange Management Shell cmdlets. 应用程序应改为创建一个远程运行空间,如本文后面所述。The application should instead create a remote runspace, as described later in this article.

连接到 Exchange 服务器上的远程运行空间Connect to a remote runspace on an Exchange server

您用来连接到远程运行空间以使用 Exchange 命令行管理程序 cmdlet 的方法因所使用的身份验证方案而异。The method that you use to connect to a remote runspace to use an Exchange Management Shell cmdlet varies based on the authentication scheme that you are using. 本节提供的代码示例演示如何在使用下表中列出的身份验证方法时连接到远程运行空间。This section provides code examples that show how to connect to a remote runspace when you are using an authentication method listed in the following table.

身份验证方法Authentication method 适用于Applies to URIURI
使用基本身份验证连接到 Exchange Online 上的远程运行空间Connect to a remote runspace on Exchange Online by using basic authentication
Exchange Online 服务器Exchange Online servers
https://outlook.office365.com/PowerShell-LiveID

https://<server>/PowerShell-LiveID
使用证书身份验证连接到远程运行空间Connect to a remote runspace by using certificate authentication
Exchange Online 和 Exchange 本地服务器Exchange Online and Exchange on-premises servers
https://outlook.office365.com/PowerShell

https://<server>/PowerShell

http://<server>/PowerShell
使用 Kerberos 身份验证连接到 Exchange 服务器上的远程运行空间Connect to a remote runspace on an Exchange server by using Kerberos authentication
Exchange Online 和 Exchange 本地服务器Exchange Online and Exchange on-premises servers
https://<server>/PowerShell

http://<server>/PowerShell

使用基本身份验证连接到 Exchange Online 上的远程运行空间Connect to a remote runspace on Exchange Online by using basic authentication

下面的代码示例定义了GetUsersUsingBasicAuth方法,该方法通过使用基本身份验证在远程 Exchange Online 服务器上创建 Exchange 命令行管理程序运行空间。The following code example defines the GetUsersUsingBasicAuth method, which creates an Exchange Management Shell runspace on a remote Exchange Online server by using basic authentication. 然后,此方法调用GetUserInformation方法,如部分中所定义的,从远程运行空间获取邮箱用户的列表,以返回远程服务器上的用户列表。The method then calls the GetUserInformation method, as defined in the section Get a list of mailbox users from a remote runspace, to return a list of users on the remote server.

此方法需要以下参数:This method requires the following parameters:

  • liveIDConnectionUri –一个字符串,包含将对应用程序进行身份验证的 Exchange Online 服务器的 URI。liveIDConnectionUri – A string that contains the URI of the Exchange Online server that will authenticate the application. 如果 Exchange Online 在 Office 365 中运行,则 URI 为 https://outlook.office365.com/PowerShell-LiveID ; 否则为 uri https://<servername>/PowerShell-LiveIDIf Exchange Online is running in Office 365, the URI is https://outlook.office365.com/PowerShell-LiveID; otherwise, the URI is https://<servername>/PowerShell-LiveID.

  • schemaUri –一个字符串,其中包含定义 Exchange 命令行管理程序架构的架构文档的 URI。schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema. 架构 URI 为 https://schemas.microsoft.com/powershell/Microsoft.ExchangeThe schema URI is https://schemas.microsoft.com/powershell/Microsoft.Exchange.

  • 凭据 –一个PSCredential对象,其中包含运行该应用程序的用户的凭据。credentials – A PSCredential object that contains the credentials of the user who is running the application.

  • 计数 –要返回的 Exchange 邮箱用户数。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);
    }
}
  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

使用证书身份验证连接到远程运行空间Connect to a remote runspace by using certificate authentication

下面的代码示例定义了GetUsersUsingCertificate方法,该方法通过使用证书在远程服务器上创建 Exchange 命令行管理程序运行空间。The following code example defines the GetUsersUsingCertificate method, which creates an Exchange Management Shell runspace on a remote server by using a certificate. 然后,此方法调用GetUserInformation方法,如部分中所定义的,从远程运行空间获取邮箱用户的列表,以返回远程服务器上的用户列表。The method then calls the GetUserInformation method, as defined in the section Get a list of mailbox users from a remote runspace, 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 –一个字符串,包含将对证书进行身份验证的服务器的 URI。certConnectionUri – A string that contains the URI of the server that will authenticate the certificate. URI 将是下表中列出的 URI 之一。The URI will be one of those listed in the following table.

    表1。certConnectionUri UriTable 1. certConnectionUri URIs

    ServerServer URIURI
    不使用 SSL 的 Exchange serverExchange server without using SSL
    http://<servername>/PowerShell
    使用 SSL 的 Exchange 服务器Exchange server using SSL
    https://<servername>/PowerShell
    作为 Office 365 的一部分的 Exchange OnlineExchange Online as part of Office 365
    https://outlook.office365.com/PowerShell
  • schemaUri –一个字符串,其中包含定义 Exchange 命令行管理程序架构的架构文档的 URI。schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema. 架构 URI 为 https://schemas.microsoft.com/powershell/Microsoft.ExchangeThe schema URI is https://schemas.microsoft.com/powershell/Microsoft.Exchange.

  • 计数 –要返回的 Exchange 邮箱用户数。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);
    }
}
  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 服务器上的远程运行空间Connect to a remote runspace on an Exchange server by using Kerberos authentication

下面的代码示例定义了GetUsersUsingKerberos方法,该方法通过使用 Kerberos 身份验证在远程服务器上创建 Exchange 命令行管理程序运行空间。The following code example defines the GetUsersUsingKerberos method, which creates an Exchange Management Shell runspace on a remote server by using Kerberos authentication. 然后,此方法调用GetUserInformation方法,如部分中所定义的,从远程运行空间获取邮箱用户的列表,以返回远程服务器上的用户列表。The method then calls the GetUserInformation method, as defined in the section Get a list of mailbox users from a remote runspace, to return a list of users on the remote server.

此方法需要以下参数:This method requires the following parameters:

  • kerberosUri –一个字符串,包含将对应用程序进行身份验证的 Kerberos 服务器的 URI。kerberosUri – A string that contains the URI of the Kerberos server that will authenticate the application. URI 将是下表中列出的 URI 之一。The URI will be one of those listed in the following table.

    表2。kerberosUri UriTable 2. kerberosUri URIs

    ServerServer URIURI
    不使用 SSL 的 Exchange serverExchange server without using SSL
    http://<servername>/PowerShell
    使用 SSL 的 Exchange 服务器Exchange server using SSL
    https://<servername>/PowerShell
  • schemaUri –一个字符串,其中包含定义 Exchange 命令行管理程序架构的架构文档的 URI。schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema. 架构 URI 为 https://schemas.microsoft.com/powershell/Microsoft.ExchangeThe schema URI is https://schemas.microsoft.com/powershell/Microsoft.Exchange.

  • 凭据 –一个PSCredential对象,其中包含运行该应用程序的用户的凭据。credentials – A PSCredential object that contains the credentials of the user who is running the application.

  • 计数 –要返回的 Exchange 邮箱用户数。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);
    }
}
  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

从远程运行空间获取邮箱用户的列表Get a list of mailbox users from a remote runspace

下面的代码示例定义了GetUserInformation方法,该方法返回代表 Exchange 邮箱用户的PSObject实例的集合。The following code example defines the GetUserInformation method, which returns a collection of PSObject instances that represent Exchange mailbox users. 此方法由GetUsersUsingBasicAuthGetUsersUsingCertificateGetUsersUsingKerberos方法调用,以返回远程服务器上的用户列表。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:

  • 计数 –要返回的 Exchange 邮箱用户数。count – The number of Exchange mailbox users to return.

  • 运行空间 –为远程 Exchange 服务器建立的远程运行空间。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();
    }
}
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_ 。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.

另请参阅See also