# Get-HighestEA-DomainWide.ps1 # Searches the whole AD domain for computer names matching EAL or EAD # and prints the highest. $regex = '^EA(\d+)(L|D)$' # Bind to RootDSE to get domain DN try { $root = [ADSI]"LDAP://RootDSE" $base = [string]$root.defaultNamingContext Write-Host "Searching domain base: $base" } catch { Write-Host "FAILED: Cannot bind to LDAP RootDSE" Write-Host "Reason: $($_.Exception.Message)" exit 1 } $entry = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$base") $searcher = New-Object System.DirectoryServices.DirectorySearcher($entry) $searcher.PageSize = 1000 $searcher.Filter = "(&(objectCategory=computer)(name=EA*))" $null = $searcher.PropertiesToLoad.Add("name") $maxAllNum = -1; $maxAllName = $null $maxLNum = -1; $maxLName = $null $maxDNum = -1; $maxDName = $null $matched = 0 foreach ($r in $searcher.FindAll()) { $name = [string]$r.Properties["name"][0] if ($name -match $regex) { $matched++ $num = [int]$Matches[1] $suf = $Matches[2] if ($num -gt $maxAllNum) { $maxAllNum = $num; $maxAllName = $name } if ($suf -eq 'L' -and $num -gt $maxLNum) { $maxLNum = $num; $maxLName = $name } if ($suf -eq 'D' -and $num -gt $maxDNum) { $maxDNum = $num; $maxDName = $name } } } Write-Host "" Write-Host "Matched EAL/D: $matched" if ($maxAllNum -ge 0) { Write-Host "Highest overall: $maxAllName (Number: $maxAllNum)" } else { Write-Host "Highest overall: (none found)" } if ($maxLNum -ge 0) { Write-Host "Highest L: $maxLName (Number: $maxLNum)" } else { Write-Host "Highest L: (none found)" } if ($maxDNum -ge 0) { Write-Host "Highest D: $maxDName (Number: $maxDNum)" } else { Write-Host "Highest D: (none found)" }