Skip to content

Commit 830f23e

Browse files
authored
Automated remediation of user-created public M365 groups
1 parent c8fc0d8 commit 830f23e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Fix-PublicM365Groups.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
$ConfigFile = $PWD.Path + '\O365Monitor-Config.xml'
2+
$xml = New-Object System.Xml.XmlDocument
3+
$xml.Load($ConfigFile)
4+
$ConfigParams = $xml.SelectSingleNode("//o365app")
5+
6+
$LogFile = $PWD.Path + '\O365Montior-PublicGroups.csv'
7+
8+
# Initialize configuration variables from config xml file
9+
$TenantID = $ConfigParams.SelectSingleNode("tenantid").InnerText
10+
$APPObjectID = $ConfigParams.SelectSingleNode("appid").InnerText
11+
12+
Connect-MgGraph -ClientId $APPObjectID -TenantId $TenantID -CertificateName "CN=O365Monitor" -ErrorAction SilentlyContinue -Errorvariable ConnectionError | Out-Null
13+
14+
if($ConnectionError -ne $null)
15+
{
16+
Write-Host "$ConnectionError" -Foregroundcolor Red
17+
Exit
18+
}
19+
20+
if (Test-Path -Path $LogFile -PathType Leaf){
21+
rm $LogFile
22+
}
23+
24+
$NotificationFlag = 0
25+
$Headers = '"ID","Group Name","Group Description"'
26+
$GroupList = Get-MgGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All
27+
$PublicGroupList = $GroupList | Where-Object -Property Visibility -eq "Public"
28+
29+
Add-Content $LogFile $Headers
30+
31+
foreach ($Group in $PublicGroupList)
32+
{
33+
Update-MgGroup -GroupId $Group.Id -Visibility "Private"
34+
Write-Host -NoNewLine "."
35+
$Entry = '"' + $Group.Id + '","' + $Group.DisplayName + '","' + $Group.Description + '"'
36+
Add-Content $LogFile $Entry
37+
$NotificationFlag++
38+
}
39+
40+
if($NotificationFlag){
41+
Write-Output "Results can be found in $LogFile"
42+
}
43+
else{
44+
Write-Output "Congratulations, no public M365 groups found."
45+
}
46+
47+
Disconnect-MgGraph | Out-Null

0 commit comments

Comments
 (0)