[英]PowerShell script to return members of multiple security groups
I need to return all members of multiple security groups using PowerShell. 我需要使用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! 关于如何正确构建的任何想法将不胜感激!
Thanks, 谢谢,
Chris 克里斯
This is cleaner and will put in a csv. 这是更清洁,将放入csv。
Import-Module ActiveDirectory
$Groups = (Get-AdGroup -filter * | Where {$_.name -like "**"} | select name -expandproperty name)
$Table = @()
$Record = [ordered]@{
"Group Name" = ""
"Name" = ""
"Username" = ""
}
Foreach ($Group in $Groups)
{
$Arrayofmembers = Get-ADGroupMember -identity $Group | select name,samaccountname
foreach ($Member in $Arrayofmembers)
{
$Record."Group Name" = $Group
$Record."Name" = $Member.name
$Record."UserName" = $Member.samaccountname
$objRecord = New-Object PSObject -property $Record
$Table += $objrecord
}
}
$Table | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
Get-ADGroupMember "Group1" -recursive | Select-Object Name | Export-Csv c:\path\Groups.csv
I got this to work for me... I would assume that you could put "Group1, Group2, etc." 我让这个为我工作......我会假设你可以把“Group1,Group2等” or try a wildcard.
或尝试使用通配符。 I did pre-load AD into PowerShell before hand:
我事先将AD预先加载到PowerShell中:
Get-Module -ListAvailable | Import-Module
This will give you a list of a single group, and the members of each group. 这将为您提供单个组的列表以及每个组的成员。
param
(
[Parameter(Mandatory=$true,position=0)]
[String]$GroupName
)
import-module activedirectory
# optional, add a wild card..
# $groups = $groups + "*"
$Groups = Get-ADGroup -filter {Name -like $GroupName} | Select-Object Name
ForEach ($Group in $Groups)
{write-host " "
write-host "$($group.name)"
write-host "----------------------------"
Get-ADGroupMember -identity $($groupname) -recursive | Select-Object samaccountname
}
write-host "Export Complete"
If you want the friendly name, or other details, add them to the end of the select-object query. 如果您需要友好名称或其他详细信息,请将它们添加到select-object查询的末尾。
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)
}
Then the results: 然后结果:
$rtn | ft -autosize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.