I have been trialing a script to get the AD groups and the groups within the groups an AD user in. The script is not effecient as to find a group within a group within a group I have had to use multiple foreach loops. Is it possible to make this command more efficient/smaller rather than using the foreach loop?
function GetADSubGroups ($object)
{
Get-ADPrincipalGroupMembership $object | ForEach {
$_
Get-ADPrincipalGroupMembership $_ | ForEach {
$_
Get-ADPrincipalGroupMembership $_ | ForEach {
$_
Get-ADPrincipalGroupMembership $_
}
}
}
}
GetADSubGroups 'testuser' | select name
You can use a while loop to eliminate all the foreach loops. Also remove the Select
cmdlet's and use dot notation instead. All of this should speed it up by around 25%.
$groups = Get-ADPrincipalGroupMembership 'testuser'
$allGroups =$null
While($groups){
$allGroups += $groups.Name
$groups = $groups.Name | Get-ADPrincipalGroupMembership
}
$allGroups
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.