powershell - Export AD users with list of specific groups -
i've been trying extract of ad users , select mail
, name
, memberof
. need list specific groups memberof
output end list each user contains name, email address , specific groups match name , not of groups member of.
get-aduser username -properties memberof | select-object memberof
i can't seem find way of doing end either noteproperty above or empty pipeline. there way achieve trying do?
the memberof
attribute contains list of distinguishedname
(dn) values, each corresponding group.
retrieve groups interested in, before run get-aduser
, way can compare group dn entry in memberof
:
$groupdns = get-adgroup -filter {name -like "*finance*" -or name -like "*creditcontrol*"} | select-object -expandproperty distinguishedname
now, can use dn's filter group memberships calculated property, so:
$userinfo = foreach($username in @("bob","alice","joe")){ $user = get-aduser -identity $username -properties mail,memberof |select name,mail,memberof $user | select-object name,mail,@{label="groupdns";expr = {$_.memberof | where-object {$groups -contains $_}}} }
without doing new get-adgroup
query each memberof
entry.
if want string of group names, rather noteproperty containing array of strings, fill groups hashtable , use "look up" memberof
entries using containskey()
method:
$groups = @{} get-adgroup -filter {name -like "*finance*" -or name -like "*creditcontrol*"} | foreach-object { $groups[$_.distinguishedname] = $_ } $userinfo = foreach($username in @("bob","alice","joe")){ $user = get-aduser -identity $username -properties mail,memberof |select name,mail,memberof $user | select-object name,mail,@{label="groups";expr = { ($_.memberof | where-object {$groups.containskey($_)} | foreach-object { $groups[$_].name}) -join ";" }} } $userinfo | export-csv c:\aduserinfo.csv -notypeinformation
Comments
Post a Comment