In continuation to my previous post “Check Exchange 2003 vitals with PowerShell”, I also have a code block that you can replace if you want to query all exchange servers in your environment dynamically with script instead of using text file as in the code I posted earlier.
In the code I posted earlier, the following lines read the file servers.txt.
# Read file and store server names in variable
$Servers = (Get-Content .\servers.txt)
Replace it with the following, which queries Active Directory for objectclass msExchExchangeServer and returns all servers found.
# Get Exchange Servers from Active Directory
function GetExchangeServers()
{
# Set variables to connect to Active Directory, searching Configuration Naming Context
$root= New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$configpartition = [adsi]("LDAP://CN=Microsoft Exchange,CN=Services," + $root.configurationNamingContext)
# Set variables for search criteria
$search = New-Object System.DirectoryServices.DirectorySearcher($configpartition)
$search.filter = '(objectclass=msExchExchangeServer)'
# Perform Serach, this will return all Exchange Server objects
$ExchServer = $search.FindAll()
# Output Name property of each server object stored in ExchServer array
$ExchServer | foreach {$_.properties.name}
# Comment the line above and uncomment the line below if you want to search specific Exchange servers by name
# The example filter below will only output servers that have MBX in their name (i.e. MBX01, SRVMBX45 etc.)
#$ExchServer | foreach {$_.properties.legacyexchangedn -match "MBX"}
}
You will also have to replace the lines:
# Get vitals for each server stored in $Servers array
$Servers | %{getVitals}
with the following:
# Get vitals for each server stored in $Servers array
GetExchangeServers | %{getVitals}
Comments welcome.