Get Mail fqdn from AD-User without ActiveDirectory Module

Posted on: October 19, 2016 Posted by: Philipp Comments: 0

Get Mail fqdn from AD-User without ActiveDirectory Module

Hier kleines Codeschnipsel welches die Mail-Domänen von ADUser auswertet und deren Häufigkeit zählt.
Es wird das Attribut mail geprüft und kein ActiveDirectory Modul verwendet

Die Rückgabe sieht wie folgt aus:

demo1.de count = 1
demo2.de count = 296
demo3.de count = 189


$AD_searchroot = "DC=semi-precious,DC=de"
$AD_SearchFilter = "(&(objectCategory=User)(samaccountname=*))"

$Searchroot = $AD_searchroot
$SearchFilter = $AD_SearchFilter
$objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$searchroot")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$ObjSearcher.CacheResults = $false
$ObjSearcher.tombstone = $true
$objSearcher.SearchRoot = $objOU
$objSearcher.PageSize = 4000
$objSearcher.Filter = $AD_SearchFilter
$objSearcher.SearchScope = "Subtree"
$Result = $objSearcher.findAll()

$mail = $Result.Properties.mail  | foreach {($_.split("@"))[1]}
$unique = $mail| sort-object –Unique
foreach ($obj in $unique)</pre>
<pre>{"$($obj) count =  $(($mail | where {$_ -eq $obj}).count)"}