-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-GraphSubscriptions.ps1
More file actions
417 lines (340 loc) · 13.7 KB
/
Get-GraphSubscriptions.ps1
File metadata and controls
417 lines (340 loc) · 13.7 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<#
.SYNOPSIS
Microsoft Graph Subscription Analyzer - PowerShell Edition
.DESCRIPTION
Identifies which applications are creating Microsoft Graph subscriptions in your tenant.
Helps resolve subscription quota issues and provides visibility into subscription ownership.
.PARAMETER ClientId
The Azure AD App Registration Client ID
.PARAMETER TenantId
The Azure AD Tenant ID
.PARAMETER FilterTranscripts
If specified, only show callTranscript-related subscriptions
.PARAMETER OutputPath
Path to save the JSON report (optional)
.EXAMPLE
.\Get-GraphSubscriptions.ps1 -ClientId "your-client-id" -TenantId "your-tenant-id"
.EXAMPLE
.\Get-GraphSubscriptions.ps1 -ClientId "your-client-id" -TenantId "your-tenant-id" -FilterTranscripts
.EXAMPLE
.\Get-GraphSubscriptions.ps1 -ClientId "your-client-id" -TenantId "your-tenant-id" -OutputPath "C:\Reports"
.NOTES
Author: Dylan Stetts
Requires: MSAL.PS module (will auto-install if missing)
Permissions Required: Subscription.Read.All, Application.Read.All (Delegated)
.LINK
https://github.com/dylanstetts/listGraphWebhooks
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, HelpMessage = "Azure AD App Registration Client ID")]
[string]$ClientId,
[Parameter(Mandatory = $true, HelpMessage = "Azure AD Tenant ID")]
[string]$TenantId,
[Parameter(Mandatory = $false, HelpMessage = "Only show callTranscript subscriptions")]
[switch]$FilterTranscripts,
[Parameter(Mandatory = $false, HelpMessage = "Path to save JSON report")]
[string]$OutputPath = $PSScriptRoot
)
#Requires -Version 5.1
# Script configuration
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
# ============================================================================
# Module Management
# ============================================================================
function Install-RequiredModule {
param([string]$ModuleName)
if (-not (Get-Module -ListAvailable -Name $ModuleName)) {
Write-Host "Installing required module: $ModuleName..." -ForegroundColor Yellow
try {
Install-Module -Name $ModuleName -Scope CurrentUser -Force -AllowClobber
Write-Host "✓ Module installed successfully" -ForegroundColor Green
}
catch {
Write-Error "Failed to install $ModuleName. Please run: Install-Module -Name $ModuleName -Scope CurrentUser"
exit 1
}
}
}
# Check and install MSAL.PS module
Install-RequiredModule -ModuleName "MSAL.PS"
Import-Module MSAL.PS -ErrorAction Stop
# ============================================================================
# Authentication Functions
# ============================================================================
function Get-GraphAccessToken {
param(
[string]$ClientId,
[string]$TenantId
)
Write-Host "`n" -NoNewline
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "AUTHENTICATION" -ForegroundColor Cyan
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "Client ID: $ClientId"
Write-Host "Tenant ID: $TenantId"
Write-Host "Scopes: Subscription.Read.All, Application.Read.All"
Write-Host "`nOpening browser for interactive login..."
Write-Host "Please sign in with your Global Admin account.`n"
try {
$token = Get-MsalToken `
-ClientId $ClientId `
-TenantId $TenantId `
-Scopes "https://graph.microsoft.com/Subscription.Read.All", "https://graph.microsoft.com/Application.Read.All" `
-Interactive `
-ErrorAction Stop
Write-Host " Authentication successful" -ForegroundColor Green
return $token.AccessToken
}
catch {
Write-Error "Authentication failed: $_"
exit 1
}
}
# ============================================================================
# Graph API Functions
# ============================================================================
function Invoke-GraphRequest {
param(
[string]$Uri,
[string]$AccessToken,
[string]$Method = "GET"
)
$headers = @{
'Authorization' = "Bearer $AccessToken"
'Content-Type' = 'application/json'
}
try {
$response = Invoke-RestMethod -Uri $Uri -Headers $headers -Method $Method -ErrorAction Stop
return $response
}
catch {
Write-Error "Graph API request failed: $_"
throw
}
}
function Get-AllSubscriptions {
param([string]$AccessToken)
Write-Host "`n" -NoNewline
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "FETCHING SUBSCRIPTIONS" -ForegroundColor Cyan
Write-Host "="*80 -ForegroundColor Cyan
$allSubscriptions = @()
$url = "https://graph.microsoft.com/v1.0/subscriptions"
$pageCount = 0
while ($url) {
$pageCount++
Write-Host "Fetching page $pageCount... " -NoNewline
$response = Invoke-GraphRequest -Uri $url -AccessToken $AccessToken
$subscriptions = $response.value
$allSubscriptions += $subscriptions
Write-Host "Retrieved $($subscriptions.Count) subscriptions" -ForegroundColor Green
$url = $response.'@odata.nextLink'
}
Write-Host "`n Total subscriptions retrieved: $($allSubscriptions.Count)" -ForegroundColor Green
return $allSubscriptions
}
function Get-ApplicationDetails {
param(
[string]$AppId,
[string]$AccessToken,
[hashtable]$Cache
)
# Check cache first
if ($Cache.ContainsKey($AppId)) {
return $Cache[$AppId]
}
try {
$filterQuery = "appId eq '$AppId'"
$encodedFilter = [System.Web.HttpUtility]::UrlEncode($filterQuery)
$uri = "https://graph.microsoft.com/v1.0/servicePrincipals?`$filter=$encodedFilter&`$select=displayName,appId,id"
$response = Invoke-GraphRequest -Uri $uri -AccessToken $AccessToken
if ($response.value -and $response.value.Count -gt 0) {
$sp = $response.value[0]
$appInfo = @{
displayName = $sp.displayName
appId = $AppId
servicePrincipalId = $sp.id
}
}
else {
$appInfo = @{
displayName = "Unknown (Not found in tenant)"
appId = $AppId
servicePrincipalId = $null
}
}
$Cache[$AppId] = $appInfo
return $appInfo
}
catch {
Write-Warning "Could not fetch app details for $AppId : $_"
$appInfo = @{
displayName = "Error fetching details"
appId = $AppId
servicePrincipalId = $null
}
$Cache[$AppId] = $appInfo
return $appInfo
}
}
function Get-TranscriptSubscriptions {
param([array]$Subscriptions)
return $Subscriptions | Where-Object {
$_.resource -match 'transcript|communications/onlineMeetings'
}
}
# ============================================================================
# Report Generation Functions
# ============================================================================
function New-SubscriptionReport {
param(
[array]$Subscriptions,
[string]$AccessToken,
[bool]$FilterTranscripts
)
Write-Host "`n" -NoNewline
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "GENERATING REPORT" -ForegroundColor Cyan
Write-Host "="*80 -ForegroundColor Cyan
# Determine which subscriptions to process
$transcriptSubs = Get-TranscriptSubscriptions -Subscriptions $Subscriptions
if ($FilterTranscripts) {
$subsToProcess = $transcriptSubs
Write-Host "Filtering to callTranscript subscriptions only..."
}
else {
$subsToProcess = $Subscriptions
Write-Host "Processing all subscriptions..."
}
Write-Host "✓ Found $($transcriptSubs.Count) callTranscript-related subscriptions" -ForegroundColor Green
Write-Host "Processing $($subsToProcess.Count) subscriptions for report`n"
# Group by application
$appCache = @{}
$appsMap = @{}
Write-Host "Fetching application details..." -ForegroundColor Cyan
$index = 0
foreach ($sub in $subsToProcess) {
$index++
$appId = if ($sub.applicationId) { $sub.applicationId } else { "Unknown" }
if (-not $appsMap.ContainsKey($appId)) {
Write-Host " [$index/$($subsToProcess.Count)] Fetching details for app: $appId"
$appDetails = Get-ApplicationDetails -AppId $appId -AccessToken $AccessToken -Cache $appCache
$appsMap[$appId] = @{
applicationId = $appId
displayName = $appDetails.displayName
servicePrincipalId = $appDetails.servicePrincipalId
subscriptions = @()
}
}
$appsMap[$appId].subscriptions += @{
id = $sub.id
resource = $sub.resource
changeType = $sub.changeType
expirationDateTime = $sub.expirationDateTime
notificationUrl = $sub.notificationUrl
clientState = $sub.clientState
}
}
# Create report object
$report = @{
generatedAt = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
totalSubscriptions = $Subscriptions.Count
transcriptSubscriptions = $transcriptSubs.Count
reportedSubscriptions = $subsToProcess.Count
uniqueApplications = $appsMap.Count
applications = @()
}
# Sort applications by subscription count (descending)
$sortedApps = $appsMap.GetEnumerator() | Sort-Object { $_.Value.subscriptions.Count } -Descending
foreach ($app in $sortedApps) {
$report.applications += $app.Value
}
return $report
}
function Write-ConsoleReport {
param([hashtable]$Report)
Write-Host "`n" -NoNewline
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "MICROSOFT GRAPH SUBSCRIPTION REPORT" -ForegroundColor Cyan
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "Generated: $($Report.generatedAt)"
Write-Host "Total Subscriptions: $($Report.totalSubscriptions)"
Write-Host "CallTranscript Subscriptions: $($Report.transcriptSubscriptions)"
Write-Host "Reported Subscriptions: $($Report.reportedSubscriptions)"
Write-Host "Unique Applications: $($Report.uniqueApplications)"
Write-Host "="*80 -ForegroundColor Cyan
foreach ($app in $Report.applications) {
Write-Host "`n📱 Application: $($app.displayName)" -ForegroundColor Yellow
Write-Host " App ID: $($app.applicationId)" -ForegroundColor Gray
if ($app.servicePrincipalId) {
Write-Host " Service Principal ID: $($app.servicePrincipalId)" -ForegroundColor Gray
}
Write-Host " Subscription Count: $($app.subscriptions.Count)" -ForegroundColor White
Write-Host " Subscriptions:" -ForegroundColor White
foreach ($sub in $app.subscriptions) {
Write-Host " • ID: $($sub.id)" -ForegroundColor Gray
Write-Host " Resource: $($sub.resource)" -ForegroundColor Gray
Write-Host " Change Type: $($sub.changeType)" -ForegroundColor Gray
Write-Host " Expires: $($sub.expirationDateTime)" -ForegroundColor Gray
if ($sub.notificationUrl) {
Write-Host " Notification URL: $($sub.notificationUrl)" -ForegroundColor Gray
}
Write-Host ""
}
}
Write-Host "="*80 -ForegroundColor Cyan
}
function Save-JsonReport {
param(
[hashtable]$Report,
[string]$OutputPath
)
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$filename = "subscription_report_$timestamp.json"
$filepath = Join-Path -Path $OutputPath -ChildPath $filename
try {
$Report | ConvertTo-Json -Depth 10 | Out-File -FilePath $filepath -Encoding UTF8
Write-Host "`n Report saved to: $filepath" -ForegroundColor Green
}
catch {
Write-Warning "Failed to save JSON report: $_"
}
}
# ============================================================================
# Main Execution
# ============================================================================
function Main {
Write-Host "`n" -NoNewline
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "Microsoft Graph Subscription Analyzer - PowerShell Edition" -ForegroundColor Cyan
Write-Host "="*80 -ForegroundColor Cyan
Write-Host "This tool identifies which applications are creating subscriptions"
Write-Host "for Microsoft Graph resources in your tenant."
Write-Host "="*80 -ForegroundColor Cyan
try {
# Authenticate
$accessToken = Get-GraphAccessToken -ClientId $ClientId -TenantId $TenantId
# Get all subscriptions
$subscriptions = Get-AllSubscriptions -AccessToken $accessToken
# Generate report
$report = New-SubscriptionReport `
-Subscriptions $subscriptions `
-AccessToken $accessToken `
-FilterTranscripts $FilterTranscripts.IsPresent
# Display console report
Write-ConsoleReport -Report $report
# Save JSON report
Save-JsonReport -Report $report -OutputPath $OutputPath
Write-Host "`n Analysis complete!" -ForegroundColor Green
Write-Host ""
}
catch {
Write-Host "`n Error: $_" -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor Red
exit 1
}
}
# Run the script
Main