簡體   English   中英

列出組成員資格組合

[英]Listing group membership combinations

希望我能在這里得到一些幫助,因為我正在努力完成我所追求的目標,我懷疑它甚至是現在實現這一目標的最佳方式!

本質上,我有一個 powershell 腳本(見下文),它成功列出了我放入的某個過濾器的成員。這一切都很順利,並將一個列表輸出到 CSV 中。

然而,下一部分是告訴它只返回特定組組合成員的成員。 例如,用戶可能是第 1 組、第 2 組和第 7 組的成員(假設有 9 個使用該命名方案的組)

所以我試圖返回只與他們在兩個組(第 1 組和第 2 組)中的聲明相匹配的成員的結果,並排除那些可能只在第 1 組但不屬於第 2 組的人......希望這是有道理的。

# 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:\Temp\Groups.csv" -NoTypeInformation

有什么想法我可能會出錯嗎? 也許最好編輯輸出的 CSV 並以這種方式匹配語句。 那么從 CSV 中刪除用戶不是兩者成員的行? 甚至不確定 Import-CSV tbh 是否完全有可能

提前致謝!

如果您想要組“Operations”和“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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM