I cannot use the Active Directory Module to get the SamAccountName of the users in a specific AD-group. How can I do this with ADSI?
I've tried:
$Group = [ADSI]"LDAP://DN of the AD group"
$Group.Member | ForEach-Object {
$Searcher = [adsisearcher]"(samAccountName=$_)"
$searcher.FindOne().Properties
}
But I see this message:
The samAccountName search filter is invalid.
How can I do this?
I ran this modified version of your code on my own system, so I could see what the search string actually looked like:
Note the point where I let
$searchKey
come to the console. When I do this, I see values with the full distinguished name instead of justsamAccountName
. Based on this result I changed the code to look for that value instead ofsamAccountName
, and then I saw (presumably) expected results: