-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick Delete Duplicate O365 Groups.ps1
57 lines (46 loc) · 2.08 KB
/
Quick Delete Duplicate O365 Groups.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# A simple helper script for mass deleting duplicate O365 groups
$FlexAssetName = "Email Groups"
$FilterID = (Get-ITGlueFlexibleAssetTypes -filter_name $FlexAssetName).data
$ITGOrganizations = Get-ITGlueOrganizations -page_size 1000
foreach ($ITGOrg in $ITGOrganizations.data) {
$orgID = $ITGOrg.id
$ExistingGroups = @()
$i = 1
while ($i -le 10 -and ($ExistingGroups | Measure-Object).Count -eq (($i-1) * 200)) {
$ExistingGroups_Partial = Get-ITGlueFlexibleAssets -page_size 200 -page_number $i -filter_flexible_asset_type_id $Filterid.id -filter_organization_id $orgID
if (!$ExistingGroups_Partial -or $ExistingGroups_Partial.Error) {
# We got an error querying groups, wait and try again
Start-Sleep -Seconds 2
$ExistingGroups_Partial = Get-ITGlueFlexibleAssets -page_size 200 -page_number $i -filter_flexible_asset_type_id $Filterid.id -filter_organization_id $orgID
if (!$ExistingGroups_Partial -or $ExistingGroups_Partial.Error) {
Write-Error "An error occurred trying to get the existing O365 groups from ITG. Exiting..."
Write-Error $ExistingGroups_Partial.Error
exit 1
}
}
$ExistingGroups += ($ExistingGroups_Partial).data
Write-Host "- Got group set $i"
$TotalGroups = ($ExistingGroups | Measure-Object).Count
Write-Host "- Total: $TotalGroups"
$i++
}
if (!$ExistingGroups) {
Write-Warning "No O365 groups found for: $($ITGOrg.attributes.name)"
continue
}
$ObjectIDs = $ExistingGroups.attributes.traits.objectid | Sort-Object -Unique
$Removed = @()
foreach ($ObjectID in $ObjectIDs) {
$Groups = $ExistingGroups | Where-Object { $_.attributes.traits.objectid -like $ObjectID } | Sort-Object { Get-Date($_.attributes.'updated-at') }
if (($Groups | Measure-Object).Count -gt 1) {
$DeleteGroup = $Groups | Select-Object -Skip 1 | Select-Object -First 1
if ($DeleteGroup.id) {
Remove-ITGlueFlexibleAssets -id $DeleteGroup.id -Confirm:$false
$Removed += 1
Write-Host "Removed: "$DeleteGroup.id
}
}
}
$DeletedCount = $Removed.Count
Write-Host "Removed $DeletedCount groups for: $($ITGOrg.attributes.name)" -ForegroundColor Green
}