切换到宽版
  • 5676阅读
  • 0回复

[分享]获取邮件用户的列表 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2015-06-29

从 Exchange Online 中获取的用户列表,Exchange Online 作为 Office 365 的一部分或的开头使用调用 Exchange Management Shell cmdlet 的管理的工具的 Exchange 2013 的 Exchange 版本是一个两步过程。首先,建立 Exchange 服务器 ; 上的远程运行空间然后,运行此 cmdlet 检索远程运行空间中的用户信息。若要连接到远程运行空间,您必须使用满足组织的安全要求的身份验证方案,与 Exchange server 身份验证。本文提供可用于远程运行空间设置和运行 Exchange Management Shell 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 Management Shell cmdlet 的计算机上运行空间加载Exchange 2013的管理单元。下文中所述,应用程序而是应创建远程运行空间。




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



用于连接到远程运行空间以使用 Exchange 命令行管理程序 cmdlet 会有所不同,该方法根据您使用的身份验证方案。本节提供的代码示例,演示如何使用下表中列出的身份验证方法时,连接到远程运行空间。
身份验证方法
应用于
URI
<span id="mt20" class="sentence" data-guid="df64dc2eb4a0b85091dd31eb4923eaac" data-source="Connect to a remote runspace on Exchange Online by using basic authentication" xml:space="preserve">使用基本身份验证连接到 Exchange Online 上远程运行空间
Exchange Online 服务器
https://outlook.office365.com/PowerShell-LiveID
https://<server>/PowerShell-LiveID
<span id="mt24" class="sentence" data-guid="df64dc2eb4a0b85091dd31eb4923eaac" data-source="Connect to a remote runspace by using certificate authentication" xml:space="preserve">使用证书身份验证连接到远程运行空间
Exchange Online 和 Exchange 本地服务器上
https://outlook.office365.com/PowerShell
https://<server>/PowerShell
http://<server>/PowerShell
<span id="mt29" class="sentence" data-guid="bc54d75cc6d52e3c5adcbc17c75258a9" data-source="Connect to a remote runspace on an Exchange server by using Kerberos authentication" xml:space="preserve">使用 Kerberos 身份验证连接到 Exchange 服务器上远程运行空间
Exchange Online 和 Exchange 本地服务器上
https://<server>/PowerShell
http://<server>/PowerShell


使用基本身份验证连接到 Exchange Online 上远程运行空间



<span id="mt34" class="sentence" data-guid="19127a85bc412e2cb6c33b77f1cc812b" data-source="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. 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." xml:space="preserve">下面的代码示例定义GetUsersUsingBasicAuth方法,通过使用基本身份验证的 Exchange Online 的远程服务器创建 Exchange Management Shell 运行空间。该方法然后调用GetUserInformation方法中,定义从远程运行空间获取邮箱用户的列表,请一节中要返回在远程服务器上的用户列表。
此方法需要以下参数:

  • <span id="mt36" class="sentence" data-guid="f4bf8ba36a7378a016e71485f2085fbe" data-source="liveIDConnectionUri – A string that contains the URI of the Exchange Online server that will authenticate the application. If Exchange Online is running in Office 365, the URI is https://outlook.office365.com/PowerShell-LiveID; otherwise, the URI is https://<servername>/PowerShell-LiveID." xml:space="preserve">liveIDConnectionUri – 一个字符串,包含 Exchange Online 服务器进行身份验证应用程序的 URI。Exchange Online 运行 Office 365 中,如果 URI 是 https://outlook.office365.com/PowerShell-LiveID;否则,URI 是 https://<servername>/PowerShell-LiveID。

  • <span id="mt37" class="sentence" data-guid="a3e992c9e2ee383ff8ecb200e4c657bb" data-source="schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema. The schema URI is http://schemas.microsoft.com/powershell/Microsoft.Exchange." xml:space="preserve">schemaUri – 一个字符串,包含定义 Exchange Management Shell 架构的架构文档的 URI。架构 URI 是 http://schemas.microsoft.com/powershell/Microsoft.Exchange

  • <span id="mt38" class="sentence" data-guid="2689ab04dddc4c968db34e235cb04748" data-source="credentials – A PSCredential object that contains the credentials of the user who is running the application." xml:space="preserve">credentialsPSCredential对象,其中包含正在运行的应用程序的用户的凭据。

  • <span id="mt39" class="sentence" data-guid="18d2209a30582e636dd3517f9cb0b0db" data-source="count – The number of Exchange mailbox users to return." xml:space="preserve">count – 返回的 Exchange 邮箱用户数。


C#
VB

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);}}


使用证书身份验证连接到远程运行空间



<span id="mt41" class="sentence" data-guid="0aa80757acb643895e7ae357f04d8968" data-source="The following code example defines the GetUsersUsingCertificate method, which creates an Exchange Management Shell runspace on a remote server by using a certificate. 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." xml:space="preserve">下面的代码示例定义GetUsersUsingCertificate方法,这会创建远程服务器上的 Exchange 命令行管理程序运行空间,通过使用证书。该方法然后调用GetUserInformation方法中,定义从远程运行空间获取邮箱用户的列表,请一节中要返回在远程服务器上的用户列表。
此方法需要以下参数:

  • <span id="mt43" class="sentence" data-guid="0dd6893ccd4e532edbf5931acec8dbee" data-source="thumbprint – A string that contains the thumbprint of the certificate that is used to authenticate the application." xml:space="preserve">thumbprint – 一个字符串,包含用于验证应用程序的证书的指纹。

  • <span id="mt44" class="sentence" data-guid="f033cc8a9833576eddc33433bf9fef3c" data-source="certConnectionUri – A string that contains the URI of the server that will authenticate the certificate. The URI will be one of those listed in the following table." xml:space="preserve">certConnectionUri – 一个字符串,包含进行身份验证证书的服务器的 URI。URI 将其中一种下表中列出。
    表 1。  certConnectionUriUri
    服务器
    URI
    无需使用 SSL 的 Exchange 服务器
    http://<servername>/PowerShell
    使用 SSL 的 Exchange 服务器
    https://<servername>/PowerShell
    Exchange Online 作为 Office 365 的一部分
    https://outlook.office365.com/PowerShell

  • <span id="mt54" class="sentence" data-guid="a3e992c9e2ee383ff8ecb200e4c657bb" data-source="schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema. The schema URI is http://schemas.microsoft.com/powershell/Microsoft.Exchange." xml:space="preserve">schemaUri – 一个字符串,包含定义 Exchange Management Shell 架构的架构文档的 URI。架构 URI 是 http://schemas.microsoft.com/powershell/Microsoft.Exchange

  • <span id="mt55" class="sentence" data-guid="18d2209a30582e636dd3517f9cb0b0db" data-source="count – The number of Exchange mailbox users to return." xml:space="preserve">count – 返回的 Exchange 邮箱用户数。


C#
VB

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);}}


使用 Kerberos 身份验证连接到 Exchange 服务器上远程运行空间



<span id="mt57" class="sentence" data-guid="480033fc5b8219e848cc3c631358ad14" data-source="The following code example defines the GetUsersUsingKerberos method, which creates an Exchange Management Shell runspace on a remote server by using Kerberos authentication. 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." xml:space="preserve">下面的代码示例定义GetUsersUsingKerberos方法,该远程服务器上的 Exchange 命令行管理程序运行空间创建使用 Kerberos 身份验证方法。该方法然后调用GetUserInformation方法中,定义从远程运行空间获取邮箱用户的列表,请一节中要返回在远程服务器上的用户列表。
此方法需要以下参数:

  • <span id="mt59" class="sentence" data-guid="57b2a477f3fc4b7e842af0203ad69551" data-source="kerberosUri – A string that contains the URI of the Kerberos server that will authenticate the application. The URI will be one of those listed in the following table." xml:space="preserve">kerberosUri – 一个字符串,包含 Kerberos 服务器进行身份验证应用程序的 URI。URI 将其中一种下表中列出。
    表 2。  kerberosUriUri
    服务器
    URI
    无需使用 SSL 的 Exchange 服务器
    http://<servername>/PowerShell
    使用 SSL 的 Exchange 服务器
    https://<servername>/PowerShell

  • <span id="mt67" class="sentence" data-guid="a3e992c9e2ee383ff8ecb200e4c657bb" data-source="schemaUri – A string that contains the URI of the schema document that defines the Exchange Management Shell schema. The schema URI is http://schemas.microsoft.com/powershell/Microsoft.Exchange." xml:space="preserve">schemaUri – 一个字符串,包含定义 Exchange Management Shell 架构的架构文档的 URI。架构 URI 是 http://schemas.microsoft.com/powershell/Microsoft.Exchange

  • <span id="mt68" class="sentence" data-guid="2689ab04dddc4c968db34e235cb04748" data-source="credentials – A PSCredential object that contains the credentials of the user who is running the application." xml:space="preserve">credentialsPSCredential对象,其中包含正在运行的应用程序的用户的凭据。

  • <span id="mt69" class="sentence" data-guid="18d2209a30582e636dd3517f9cb0b0db" data-source="count – The number of Exchange mailbox users to return." xml:space="preserve">count – 返回的 Exchange 邮箱用户数。


C#
VB

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);}}



从远程运行空间获取邮箱用户的列表




<span id="mt71" class="sentence" data-guid="e95a0c306616fb7142b88c60a5169b45" data-source="The following code example defines the GetUserInformation method, which 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." xml:space="preserve">下面的代码示例定义GetUserInformation方法返回一个代表 Exchange 邮箱用户的PSObject实例的集合。调用此方法由GetUsersUsingBasicAuthGetUsersUsingCertificateGetUsersUsingKerberos方法可从远程服务器返回的用户的列表。
此方法需要以下参数:

  • <span id="mt73" class="sentence" data-guid="18d2209a30582e636dd3517f9cb0b0db" data-source="count – The number of Exchange mailbox users to return." xml:space="preserve">count – 返回的 Exchange 邮箱用户数。

  • <span id="mt74" class="sentence" data-guid="4476c5950d8c4db2804651fda1e195f4" data-source="runspace – The remote runspace that is established for the remote Exchange server." xml:space="preserve">runspace – 建立的远程 Exchange 服务器的远程运行空间。


C#
VB

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();}}

<span id="mt75" class="sentence" data-guid="952eacceb5df1c8d12158268ab8e6018" data-source="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." xml:space="preserve">GetUserInformation方法将返回不能超过count邮箱用户。若要简化此示例的代码,该方法不筛选或否则限制返回的邮箱用户。
分享到
快速回复
限60 字节
 
上一个 下一个