Skip to content

Instantly share code, notes, and snippets.

@justincjahn
Created November 25, 2014 06:11
Show Gist options
  • Save justincjahn/58db206be39f100945e3 to your computer and use it in GitHub Desktop.
Save justincjahn/58db206be39f100945e3 to your computer and use it in GitHub Desktop.
Fetch a list of Exchange servers from a Windows domain.
<#
.SYNOPSIS
Return a list of available Exchange Servers.
.DESCRIPTION
Fetches a list of Exchange servers from an Active Directory domain.
.NOTES
VERSION: 1.0.0
AUTHOR: Justin Jahn <jjahn@aeafcu.org>
COMPANY: Jahn Digital
COPYRIGHT: GNU Lesser GPL <https://www.gnu.org/licenses/lgpl.html>
#>
Function Get-ExchangeServers {
# Fetch the domain's configuration context
$oConfig = ([ADSI]"LDAP://RootDse").configurationNamingContext
# Create a new search within the configuration context, looking for all exchange servers.
$oSearch = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://$oConfig")
$oSearch.Filter = "objectClass=msExchExchangeServer"
[void] $oSearch.PropertiesToLoad.Add("name")
[void] $oSearch.PropertiesToLoad.Add("msexchcurrentserverroles")
[void] $oSearch.PropertiesToLoad.Add("networkaddress")
# Loop through each server and generate a return object
$oSearch.FindAll() | % {
New-Object PSObject -Property @{
Name = $_.Properties.name[0]
FQDN = $_.Properties.networkaddress | % {
if ($_ -match "ncacn_ip_tcp") {
$_.split(":")[1]
}
}
RawRoles = $_.Properties.msexchcurrentserverroles[0]
Roles = $(
$server = $_
$hRoles = @{
2 = "MB"
4 = "CAS"
16 = "UM"
32 = "HT"
64 = "ET"
}
$hRoles.keys | ? {
$_ -band $server.Properties.msexchcurrentserverroles[0]
} | % {
$hRoles.Get_Item($_)
}
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment