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.
1 2 3 | # 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # 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:
1 2 | # Get vitals for each server stored in $Servers array $Servers | %{getVitals} |
with the following:
1 2 | # Get vitals for each server stored in $Servers array GetExchangeServers | %{getVitals} |
Comments welcome.