-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReport-usage.ps1
322 lines (281 loc) · 11.7 KB
/
Report-usage.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# This script uses an ADAL app with certificate and app only permissions for authentication
# Feel free to change how you get an auth token, as there are many ways
$certStore = "Cert:\CurrentUser\My"
$tenantid = (Get-Content .\tenantid.txt).Trim()
$appid = (Get-Content .\appid.txt).Trim()
$thumb = (Get-Content .\cert-thumb.txt).Trim()
$connection = Connect-AzureAD -TenantId $tenantid -ApplicationId $appid -CertificateThumbprint $thumb
#$token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens["AccessToken"].AccessToken
# Get ADAL access token against Graph
$loginUri = 'https://login.microsoftonline.com'
$resource = 'https://graph.microsoft.com'
$certificate = Get-Item "$certStore\$thumb"
$authority = "$loginUri/$tenantid"
$context = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($authority)
$cac = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate]::new($appId, $certificate)
$tokenResult = $context.AcquireTokenAsync($resource, $cac).GetAwaiter().GetResult()
$token = $tokenResult.AccessToken
$useServiceUser = $false
if ($useServiceUser) {
Write-Host "Prompt for service user to access planner"
Connect-PnPOnline -Scopes "Group.Read.All"
$serviceToken = Get-PnPAccessToken
# ID of AAD user to use for checking Planner
$serviceUserId = (Get-PnPAccessToken -Decoded).Payload.oid
}
$today = (Get-Date)
$warningDate = (Get-Date).AddDays(-30)
$date = $today.ToString("yyyy'-'MM'-'dd")
$report = @()
$reportFile = ".\ObsoleteGroups.html"
$lastFile = ".\ObsoleteGroups.csv"
function Get-GroupUsageReport() {
try {
$query = "https://graph.microsoft.com/beta/reports/getOffice365GroupsActivityDetail(period='D30')?`$format=application/json&`$top=200"
$headers = @{"Authorization" = "Bearer " + $token }
$response = Invoke-RestMethod -Method Get -ContentType "application/json" -Uri $query -Headers $headers -UseBasicParsing
$reportItems = $response.value
$nextLink = $response."@odata.nextLink"
while ($null -ne $nextLink) {
$response = Invoke-RestMethod -Method Get -ContentType "application/json" -Uri $nextLink -Headers $headers -UseBasicParsing
$nextLink = $response."@odata.nextLink"
$reportItems += $response.value
}
Write-Host "Returned $($reportItems.length) groups"
return ($reportItems | Sort-Object groupDisplayName) | ? isDeleted -eq $false
}
catch {
Write-Host $_
exit
}
}
function Get-GroupByName($displayName) {
try {
$query = "https://graph.microsoft.com/v1.0/groups?`$filter=displayName eq '$displayName'&`$expand=owners"
$headers = @{"Authorization" = "Bearer " + $token }
$response = Invoke-RestMethod -Method Get -ContentType "application/json" -Uri $query -Headers $headers -UseBasicParsing
return $response.value
}
catch {
Write-Host $_
exit
}
}
function Renew-Group($groupId) {
$query = "https://graph.microsoft.com/v1.0/groups/$groupId/renew"
$headers = @{"Authorization" = "Bearer " + $token }
$response = Invoke-RestMethod -Method POST -ContentType "application/json" -Uri $query -Headers $headers -UseBasicParsing
return $response.value
}
function Add-Member($groupId, $userId) {
Add-AzureADGroupMember -ObjectId $groupId -RefObjectId $userId
Start-Sleep 5 # give it some time to sync
}
function Remove-Member($groupId, $userId) {
Remove-AzureADGroupMember -ObjectId $groupId -MemberId $userId
}
function Get-PlannerActive($groupId) {
if ( [string]::IsNullOrWhiteSpace($serviceToken)) {
return $false
}
try {
$plansQuery = "https://graph.microsoft.com/v1.0/groups/$groupId/planner/plans"
$headers = @{"Authorization" = "Bearer " + $serviceToken }
$response = Invoke-RestMethod -Method GET -ContentType "application/json" -Uri $plansQuery -Headers $headers -UseBasicParsing
$plans = $response.value
foreach ($plan in $plans) {
$planId = $plan.id
$tasksQuery = "https://graph.microsoft.com/v1.0/planner/plans/$planId/tasks"
$response = Invoke-RestMethod -Method GET -ContentType "application/json" -Uri $tasksQuery -Headers $headers -UseBasicParsing
if ($response.value.length -gt 0) {
$task = $response.value[0];
$taskDate = Get-Date -Date $task.createdDateTime
if ( $taskDate -gt $warningDate) {
return $true
}
}
}
return $false
}
catch {
Write-Host "Failed to load plans"
}
}
function Get-AllowSharing($groupId) {
$settings = Get-AzureADObjectSetting -TargetObjectId $groupId -TargetType Groups
$template = $settings | ? TemplateId -eq '08d542b9-071f-4e16-94b0-74abb372e3d9'
if ($null -ne $template) {
return $template.Values.Value
}
# $query = "https://graph.microsoft.com/v1.0/groups/$groupId/settings"
# $headers = @{"Authorization" = "Bearer " + $token }
# $settings = Invoke-RestMethod -Method Get -ContentType "application/json" -Uri $query -Headers $headers -UseBasicParsing
# $template = $settings.value | ? templateId -eq '08d542b9-071f-4e16-94b0-74abb372e3d9'
# if ($null -ne $template) {
# return [bool]::Parse($template.values.value)
# }
return $true
}
$previousItems = @()
if ( Test-Path $lastFile -PathType Leaf ) {
$previousReportFile = Get-Content -Path $lastFile -Encoding UTF8
$previousItems = ConvertFrom-Csv -Delimiter ';' -InputObject $previousReportFile
}
$htmlhead = "<html>
<style>
BODY{font-family: Arial; font-size: 8pt;}
H1{font-size: 22px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}
H2{font-size: 18px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}
H3{font-size: 16px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}
TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}
TH{border: 1px solid #969595; background: #dddddd; padding: 5px; color: #000000;}
TD{border: 1px solid #969595; padding: 5px; }
td.pass{background: #B7EB83;}
td.warn{background: #FFF275;}
td.fail{background: #FF2626; color: #ffffff;}
td.info{background: #85D4FF;}
td:contains('False'){background-color: #FFF275}
</style>
<body>
<div align=center>
<p><h1>Report of Potentially Obsolete Office 365 Groups</h1></p>
<p><h3>Generated: $date - $domain</h3></p></div>"
# Get a list of all Office 365 Groups in the tenant
Write-Host "Extracting list of Office 365 Groups to be checked..."
$groupStats = Get-GroupUsageReport
$groupStats | % {
Write-Host "$($_.groupDisplayName) - refresh date $($_.reportRefreshDate)"
$groupData = Get-GroupByName -displayName $_.groupDisplayName
if ($groupData.length -gt 1) {
Write-Host "`tMultiple groups returned - skipping"
return
}
if ($groupData -eq $null) {
Write-Host "`tGroup deleted - skipping"
return
}
if ($groupData.groupTypes.Contains("DynamicMembership")) {
$ownerCount = "n/a"
}
else {
$ownerCount = $groupData.owners.count
}
if (-not $groupData.groupTypes.Contains("DynamicMembership") -and $useServiceUser) {
Add-Member -groupId $groupData.id -userId $serviceUserId
$plannerActive = Get-PlannerActive -groupId $groupData.id
#Remove-Member -groupId $groupData.id -userId $serviceUserId
}
$hasTeams = ($groupData.resourceProvisioningOptions -ne $null -and $groupData.resourceProvisioningOptions.Contains("Team"))
$hasYammer = ($groupData.resourceBehaviorOptions -ne $null -and $groupData.resourceBehaviorOptions.Contains("YammerProvisioning"))
$prevGroupData = $previousItems | ? GroupId -eq $groupData.Id
if ($prevGroupData -ne $null) {
$lastMsgDate = $prevGroupData.LastMessageActivity
}
else {
$lastMsgDate = $date
}
if ([int]$_.exchangeMailboxTotalItemCount -gt [int]$prevGroupData.MessageCount ) {
$lastMsgDate = $date
}
$messageActive = (Get-Date -Date $lastMsgDate) -gt $warningDate
$yammerActive = $false
if ($hasYammer) {
if ($prevGroupData.yammerPostedMessageCount -ne $null) {
$lastYammerDate = $prevGroupData.LastYammerActivity
}
elseif ([int]$_.yammerPostedMessageCount -eq 0) {
$lastYammerDate = "2018-01-01"
}
else {
$lastYammerDate = $date
}
if ([int]$_.yammerPostedMessageCount -gt [int]$prevGroupData.YammerCount ) {
$lastYammerDate = $date
}
$yammerActive = ((Get-Date -Date $lastYammerDate) -gt $warningDate) -and ([int]$_.yammerPostedMessageCount -gt 0)
}
if ($_.lastActivityDate) {
$spoActive = (Get-Date -Date $_.lastActivityDate) -gt $warningDate
}
else {
$spoActive = $false
$_.lastActivityDate = "Never"
}
if ($spoActive -and $messageActive -or ($hasYammer -and $yammerActive)) {
$status = "Pass"
}
elseif ($spoActive -or $messageActive) {
$status = "Warn"
}
else {
$status = "Fail"
}
#TODO: Get owner count
$reportLine = [PSCustomObject][Ordered]@{
GroupId = $groupData.Id
GroupName = $_.groupDisplayName
ManagedBy = $_.ownerPrincipalName
Owners = $ownerCount
Members = $_.memberCount
ExternalGuests = $_.externalMemberCount
AllowExternalMembers = Get-AllowSharing -groupId $groupData.Id
Description = $groupData.description
LastSPOActivity = $_.lastActivityDate
MessageCount = $_.exchangeMailboxTotalItemCount
LastMessageActivity = $lastMsgDate
TeamEnabled = if ($hasTeams) { "Yes" } else { "No" }
YammerCount = $_.yammerPostedMessageCount
LastYammerActivity = $lastYammerDate
Classification = $groupData.classification
PlannerActive = $plannerActive
Active = $status
}
$report += $reportLine
if ($status -eq 'Pass') {
Write-Host "`t$status" -ForegroundColor Green
}
if ($status -eq 'Warn') {
Write-Host "`t$status" -ForegroundColor Yellow
}
if ($status -eq 'Fail') {
Write-Host "`t$status" -ForegroundColor Red
}
# if (($spoActive -or $messageActive)) {
# if ((Get-Date -Date $groupData.renewedDateTime) -lt $warningDate) {
# Write-Host "Auto-renewing $($_.groupDisplayName)"
# Renew-Group -groupId $groupData.Id
# }
# }
}
$htmlfooter = "
<p>
Messages includes both Teams activity and e-mail activity.
</p>
</body>
<script>
function contains(selector, text) {
var elements = document.querySelectorAll(selector);
return Array.prototype.filter.call(elements, function(element){
return RegExp(text).test(element.textContent);
});
}
//debugger;
var nodes = contains('td','Pass');
for (var i in nodes) {
nodes[i].className = 'Pass';
}
var nodes = contains('td','Warn');
for (var i in nodes) {
nodes[i].className = 'Warn';
}
var nodes = contains('td','Fail');
for (var i in nodes) {
nodes[i].className = 'Fail';
}
</script>
</html>
"
$htmlbody = $report | ConvertTo-Html -Fragment
$htmlreport = $htmlhead + $htmlbody + $htmlfooter
$htmlreport | Out-File $reportFile -Encoding UTF8
($report | ConvertTo-Csv -NoTypeInformation -Delimiter ';') | Out-File -FilePath $lastFile -Encoding utf8