Created
February 4, 2018 17:12
-
-
Save zloeber/0394b41b41740ff59461688663f35d2a to your computer and use it in GitHub Desktop.
List Duplicate AzureAD Dynamic Groups Based On Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Finds a good portion of possible dupliate dynamic groups in Azure AD. Will not find super complicated member filter duplicates but should find most common filters (like all PCs and the zillions of 'Subsidiary*' groups that InTune upgrades have created on the back end) | |
Requires the AzureADPreview module to work correctly! | |
More on dynamic membership in Azure AD: | |
https://docs.microsoft.com/en-us/azure/active-directory/active-directory-groups-dynamic-membership-azure-portal | |
#> | |
Remove-Module AzureAD -Force -ErrorAction:SilentlyContinue | |
Import-Module AzureADPreview | |
Connect-AzureAD | |
function Normalize-DynamicGroupFilter ($Filter) { | |
# Somewhat normalizes the Odata filter on dynamic member groups for comparison sake | |
if ($null -ne $filter) { | |
$filter -replace '\(','' ` | |
-replace '\)','' ` | |
-replace '\ -eq',' eq' ` | |
-replace '\ -any',' any' ` | |
-replace '\ -ne',' ne' | |
} | |
else { | |
$null | |
} | |
} | |
Function Get-AzureADDuplicateDynamicGroups { | |
# Gets a list of dynamic groups that have duplicate filters | |
$CurrentDyngroups = Get-AzureADMSGroup -All:$True | Where {($_.GroupTypes -eq 'DynamicMembership')} | |
Foreach ($DynGroup in $CurrentDynGroups) { | |
$DynGroup | Add-Member -MemberType NoteProperty -Name 'NormalizedFilter' -Value (Normalize-DynamicGroupFilter $DynGroup.MembershipRule) | |
} | |
$CurrentDynGroups | Group-Object NormalizedFilter | |
} | |
Get-AzureADDuplicateDynamicGroups | Foreach { | |
if ($_.Count -gt 1) { | |
Write-Output "Normalized Filter = $($_.Name) : Groups found = $($_.Count)" | |
Write-Output '' | |
$_.Group | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment