Find Exchange Servers in the Local Active Directory Site using PowerShell

来源:互联网 发布:专业的vb振动电机 编辑:程序博客网 时间:2024/05/01 19:39


原文:http://mikepfeiffer.net/2010/04/find-exchange-servers-in-the-local-active-directory-site-using-powershell/

Using the Get-ExchangeServer cmdlet is the best way to get information about Exchange servers using PowerShell, but there may be times where you want to obtain this information without using the EMS cmdlets. For instance, you may be want to write a PowerShell script that works with Exchange on a machine without the Exchange tools installed.

The following function uses an ADSI DirectorySearcher to search the current Active Directory site for Exchange 2010 servers.

Function Get-ExchangeServerInSite {    $ADSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]    $siteDN = $ADSite::GetComputerSite().GetDirectoryEntry().distinguishedName    $configNC=([ADSI]"LDAP://RootDse").configurationNamingContext    $search = new-object DirectoryServices.DirectorySearcher([ADSI]"LDAP://$configNC")    $objectClass = "objectClass=msExchExchangeServer"    $version = "versionNumber>=1937801568"    $site = "msExchServerSite=$siteDN"    $search.Filter = "(&($objectClass)($version)($site))"    $search.PageSize=1000    [void] $search.PropertiesToLoad.Add("name")    [void] $search.PropertiesToLoad.Add("msexchcurrentserverroles")    [void] $search.PropertiesToLoad.Add("networkaddress")    $search.FindAll() | %{        New-Object PSObject -Property @{            Name = $_.Properties.name[0]            FQDN = $_.Properties.networkaddress |                %{if ($_ -match "ncacn_ip_tcp") {$_.split(":")[1]}}            Roles = $_.Properties.msexchcurrentserverroles[0]        }    }}

Here is the output from running the above function:

As you can see we get the netbios name of the server, the FQDN and the roles value provided by themsExchCurrentServerRoles attribute. The server roles can be determined by comparing the value to the table listed inthis technet article. The role values are defined as follows:

  • Mailbox Role – 2
  • Client Access Role – 4
  • Unified Messaging Role – 16
  • Hub Transport Role – 32
  • Edge Transport Role – 64

    For example, you can see from the output that HC1 and HC2 both return a value of 36 for the server role. Given that 4 + 32 = 36, we know HC1 and HC2 are running both the HUB and CAS roles. We can also see that MB1 and MB2 are running only the Mailbox server role, and litware-edge is running the edge role.

    If you need to translate these role values, we can add the values and descriptions to a hash table:

    $role = @{    2  = "MB"    4  = "CAS"    16 = "UM"    32 = "HT"    64 = "ET"}

    And then use the following code to get the textual descriptions:

    foreach ($server in Get-ExchangeServerinSite) {  $roles = ($role.keys | ?{$_ -band $server.roles} | %{$role.Get_Item($_)}) -join ", "  $server | select Name, @{n="Roles";e={$roles}}}

    The output would look something like this:

    Again, this code is no replacement for the Get-ExchangeServer cmdlet, but it could be useful if you need to grab this information without EMS cmdlets. In an upcoming post, I’ll provide a useful (hopefully) example of how you may use the Get-ExchangeServerinSite function.

  • 0 0
    原创粉丝点击