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