Public/Get-MDSExchServerFromLDAP.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Function Get-MDSExchServerFromLDAP {
    <#
    .SYNOPSIS
        Gets a list of Exchange Servers registered in LDAP.

    .DESCRIPTION
        Gets a list of Exchange Servers registered in LDAP. Can return Exchange Servers of a particular
        role and/or return a single random server.

    .PARAMETER Role
        Returns a list of servers with a specific role installed. Valid arguments include
        'Mailbox','ClientAccess','UnifiedMessaging','HubTransport','EdgeTransport'

    .PARAMETER Random
        Returns a single random server from the list. May be used with or without -Role

    .EXAMPLE
        Get-MDSExchServerFromLDAP

        Returns all Exchange Servers in LDAP

    .EXAMPLE
       Get-MDSExchServerFromLDAP -Role ClientAccess

        Returns all Client Access Servers in LDAP

    .EXAMPLE
        Get-MDSExchServerFromLDAP -Role HubTransport -Random

        Returns a random Hub Transport Server in LDAP

    .NOTES
        Author: Rick A
        Based on source: http://mikepfeiffer.net/2010/04/find-exchange-servers-in-the-local-active-directory-site-using-powershell/
    #>


    [cmdletbinding()]
    param(
        [parameter()]
        [ValidateSet('Mailbox','ClientAccess','UnifiedMessaging','HubTransport','EdgeTransport',ignorecase=$True)]
        [string]$Role,

        [parameter()]
        [switch]$Random
    )

    Begin {
        If ($Role) {
            Switch ($Role) {
                Mailbox                {$ServerRoleInteger = 2;continue}
                ClientAccess        {$ServerRoleInteger = 4;continue}
                UnifiedMessaging    {$ServerRoleInteger = 16;continue}
                HubTransport        {$ServerRoleInteger = 32;continue}
                EdgeTransport        {$ServerRoleInteger = 64;continue}
            }
        }

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

    Process {
        Try {
            $configNC = ([ADSI]"LDAP://RootDse").configurationNamingContext
            $search = New-Object DirectoryServices.DirectorySearcher([ADSI]"LDAP://$configNC")
            $objectClass = "objectClass=msExchExchangeServer"
            $version = "versionNumber>=1937801568"
            $search.Filter = "(&($objectClass)($version))"
            $search.PageSize = 1000
            [void] $search.PropertiesToLoad.AddRange(("name","msexchcurrentserverroles","networkaddress"))
            $ServerList = $search.FindAll() | ForEach-Object{
                $ServerRoles = $_.Properties.msexchcurrentserverroles[0]
                $RolesHumanReadable = (
                    $RoleAbbreviation.keys |
                        Where-Object {$_ -band $ServerRoles} |
                        ForEach-Object{$RoleAbbreviation.Get_Item($_)}
                ) -join ","
                New-Object PSObject -Property @{
                    Name = $_.Properties.name[0]
                    FQDN = $_.Properties.networkaddress |
                    ForEach-Object{if ($_ -match "ncacn_ip_tcp") {$_.split(":")[1]}}
                    msexchcurrentserverroles = $_.Properties.msexchcurrentserverroles[0]
                    Roles = $RolesHumanReadable
                }
            } | Sort-Object Name | Select-Object Name,FQDN,msexchcurrentserverroles,Roles

            If ($ServerRoleInteger) {
                $Output = $ServerList | Where-Object {($_.msexchcurrentserverroles -band $ServerRoleInteger) -eq $ServerRoleInteger}
            }
            Else {$Output = $ServerList}

            If ($Random) {$Output | Select-Object Name,FQDN,Roles | Get-Random}
            Else {$Output | Select-Object Name,FQDN,Roles}
        }
        Catch {
            Write-Error $PSItem
        }
    }
}

<#
### Test the function
$Count = 0
Do {
    $ExchServer = Get-ExchangeServerFromLDAP -Role HubTransport -Random
    $TestConnection = $False
    Try {$TestConnection = Test-Connection $ExchServer.FQDN -Count 1 -ErrorAction Stop}
    Catch {}
    If ($Count -le 2) {$ExchServer.fqdn;$Count++;$TestConnection = $False}
}
Until ($TestConnection -ne $False)

### Use Case

Do {
    $ExchServer = Get-ExchangeServerFromLDAP -Role HubTransport -Random
    $TestConnection = $False
    Try {$TestConnection = Test-Connection $ExchServer.FQDN -Count 1 -ErrorAction Stop}
    Catch {}

}
Until ($TestConnection -ne $False)
$ExchServer

#>