-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge_nested_Group_Members.ps1
30 lines (23 loc) · 1.08 KB
/
Merge_nested_Group_Members.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Author: Bender, André (ANB)
# Creation date: 21.03.2024
############################################
$NestedGroup = "MECM_App_FinalisierungRollback_AuthMembers"
$FinalGroup = "MECM_App_FinalisierungRollback_Auth"
# Collects GroupMembers and NestedGroups
#$GroupMembers = @(Get-ADGroupMember -Identity $NestedGroup | Where-Object { $_.objectClass -eq "user" })
#$NestedGroups = @(Get-ADGroupMember -Identity $NestedGroup | Where-Object { $_.objectClass -eq "group" })
$GroupMembers = Get-ADGroupMember -identity $NestedGroup -recursive
<#foreach ($Group in $NestedGroups){
$NestedGroupMembers = Get-ADGroupMember -Identity $Group
}#>
#$TotalMembers = $GroupMembers + $NestedGroupMembers
# Clears final group for members not in nested group
Get-ADGroupMember -Identity $FinalGroup | ForEach-Object {
if ($GroupMembers.Name -notcontains $_.Name) {
Remove-ADGroupMember -Identity $FinalGroup -Members $_ -Confirm:$false
}
}
# Adds members to final group
foreach ($Member in $GroupMembers) {
Add-ADGroupMember -Identity $FinalGroup -Members $Member -Confirm:$false
}