列出组成员组合



希望我能在这里得到一些思考上的帮助,因为我正在努力完成我追求的目标,我怀疑这甚至是现在实现这一目标的最佳方式!

本质上,我有一个powershell脚本(见下文(,它成功地列出了我放入的某个过滤器的成员。这一切都很顺利,并将列表输出到CSV中。

然而,下一部分是告诉它只返回特定组组合的成员。例如,一个用户可能是第1组、第2组和第7组的成员(比如说,在具有该命名方案的9个组中(

因此,我试图返回那些只符合他们属于两组(第一组和第二组(的成员的结果,并排除那些可能只属于第一组但不属于第二组的成员……希望这是有道理的。

# first im narowing down group search to all groups starting with Group and then a number (this is an example). This will return 9 groups. Group 1 through to Group 9
$Groups = (Get-AdGroup -filter * | Where-object { $_.name -like "Group *" } | select-object name -expandproperty name)
# Just standard Array
$Array = @()
$Data = [ordered]@{}
# so now im wanting to search for members in each of those groups we narrowed down to above 
Foreach ($Group in $Groups) {
# This bit defines my search criteria. It works perfectly if i just return all users. But if i only want to display members that are in Group 1 AND Group 2....it does not return any results.
$Members = Get-ADGroupMember -identity $Group | Where-Object { ($Group.name -like "Group 1") -and ($Group.name -like "Group 2") } | Get-ADUser -Properties * | select-object  givenName, sn, sAMAccountName, mail 
# Eventually that will be displayed in the object below...this bit works fine
foreach ($Member in $Members) {
$Data."update" = "modify"
$Data."region" = $Group
$Data."login" = $Member.mail
$Data."first_name" = $Member.givenName
$Data."last_name" = $Member.sn
$Data."approver_level" = "BlankForNow"
#
$DataPSObject = New-Object PSObject -property $Data
#
$Array += $DataPSObject
}
}
#
$Array | Sort-Object -Property login | Export-Csv "D:TempGroups.csv" -NoTypeInformation

有什么想法我会出错吗?也许以这种方式编辑输出的CSV和匹配语句更好。因此,从CSV中删除用户不是两者成员的行?甚至不确定导入CSV tbh 是否完全可能

提前感谢!

如果您想要"Operations"one_answers"ServiceDeskLevel2"组的通用成员

# Get groups members
$membersGroup1 = Get-ADGroupMember "Operations"
$membersGroup2 = Get-ADGroupMember "ServiceDeskLevel2"
# Compares both groups and put common members in the $res list
$res = Compare-Object $membersGroup1 $membersGroup2 -PassThru -IncludeEqual -ExcludeDifferent 
# Output the name of the common members from $res
$res | Format-List -Property name

最新更新