Create a new directory object using .Net for the current AD domain.
$root= New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
Use ADSI to get the configuration partition of AD.
$cfgNC = [adsi]("LDAP://" + $root.Get("configurationNamingContext"))
Create a directory search object
$search = New-Object System.DirectoryServices.DirectorySearcher($cfgNC)
Filter on the Exchange Server object class - the below image should help illustrate what we are doing here. (other classes you can search on can be found here)
$search.filter = '(objectclass=msExchExchangeServer)'


Use the FindAll method to execute the search.
$ExchServer = $search.FindAll()
Finally, return the names of all the results.
$ExchServer | foreach {$_.properties.name}
Which in the example from the screenshot would produce a result:
EXCH
You can now combine this with any of the previous WMI scripts to run them against all the Exchange servers in your environment, e.g. for Top 10 largest mailboxes on each server:
$root= New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$cfgNC = [adsi]("LDAP://" + $root.Get("configurationNamingContext"))
$search = New-Object System.DirectoryServices.DirectorySearcher($cfgNC)
$search.filter = '(objectclass=msExchExchangeServer)'
$ExchServer = $search.FindAll()
foreach ($server in $ExchServer)
{
$ExchangeServers = $ExchangeServers + $ExchServer | foreach {$_.properties.name}
}
foreach ($computer in $ExchangeServers) {
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer | sort-object Size -Descending | select-object -First 10 MailboxDisplayName,Servername,StorageGroupName,StoreName,Size}
No comments:
Post a Comment