PowerShell 脚本返回多个安全组的成员 [英] PowerShell script to return members of multiple security groups
问题描述
我需要使用 PowerShell 返回多个安全组的所有成员.方便的是,所有组都以相同的字母开头.
I need to return all members of multiple security groups using PowerShell. Handily, all of the groups start with the same letters.
我可以使用以下代码返回所有相关安全组的列表:
I can return a list of all the relevant security groups using the following code:
Get-ADGroup -filter 'Name -like"ABC*"' | Select-Object Name
而且我知道我可以使用以下代码返回特定安全组的成员资格列表:
And I know I can return the membership list of a specific security group using the following code:
Get-ADGroupMember "Security Group Name" -recursive | Select-Object Name
然而,我似乎无法将它们放在一起,尽管我认为我所追求的应该是这样的(请随时纠正我,这就是我在这里的原因!):
However, I can't seem to put them together, although I think what I'm after should look something like this (please feel free to correct me, that's why I'm here!):
$Groups = Get-ADGroup -filter 'Name -like"ABC*"' | Select-Object Name
ForEach ($Group in $Groups) {Get-ADGroupMember -$Group -recursive | Select-Object Name
任何有关如何正确构建结构的想法将不胜感激!
Any ideas on how to properly structure that would be appreciated!
谢谢,
克里斯
推荐答案
如果您不关心用户在哪个组中,而只想要一个很大的用户列表 - 这样做:
If you don't care what groups the users were in, and just want a big ol' list of users - this does the job:
$Groups = Get-ADGroup -Filter {Name -like "AB*"}
$rtn = @(); ForEach ($Group in $Groups) {
$rtn += (Get-ADGroupMember -Identity "$($Group.Name)" -Recursive)
}
那么结果:
$rtn | ft -autosize
这篇关于PowerShell 脚本返回多个安全组的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!