$ous="admin","bladmin","af","dd"
for($i=0;$i -lt 2;$i++)
{
$ou= $ous[$i]
$usernames = get-aduser -filter * -SearchBase "ou=$ou,dc=testj,dc=com"
foreach ($username in $usernames)
{
$user = get-aduser $username.name -Properties *
$groups = $user.memberof
$groupall=$null
foreach ($group in $groups)
{
$groupall += $group.split("=")[1].split(",")[0] + ","
}
$user.surname + "," + $user.Description + "," + $groupall |Out-File d:usercollect.txt -Append
}
}
==================
查询单个用户,使用 -filter参数,速度要快很多,如下:
get-aduser -filter 'name -like "*admin*"'