您的位置:首页 > 运维架构 > Shell

Find Exchange Servers in the Local Active Directory Site using PowerShell

2015-05-21 16:51 399 查看

原文: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 the
msExchCurrentServerRoles attribute. The server roles can be determined by comparing the value to the table listed in

this technet article. The role values are defined as follows:

Mailbox Role – 2Client Access Role – 4Unified Messaging Role – 16Hub Transport Role – [b]32Edge 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐