Because we don't have the active directory module available on all our systems we're using ADSI
instead. The following code retrieves a user object from AD by using the AdsiSearcher
:
$ADUser = ([AdsiSearcher]"(samaccountname=$SamAccountName)").FindOne()
This results in finding the property primarygroupid
which represents the domain primary group for user, usually number 513
. When we have this number we would like to find the distinguishedName
of the group. However, the code below does that just fine I was wondering if there is a better filter
that can be used instead of filtering after the FindAll()
method?
$searcher = [adsisearcher]'objectclass=group'
$searcher.PropertiesToLoad.Add('primarygrouptoken')
$searcher.PropertiesToLoad.Add('distinguishedName')
$searcher.FindAll() |
Where-Object { $_.Properties.primarygrouptoken -eq 513}
Something like this would be great but it's not possible:
([adsisearcher]”(&(objectCategory=group)(primaryGroupid=513))”).FindOne()