-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.ps1
More file actions
356 lines (272 loc) · 9.3 KB
/
Copy pathgraph.ps1
File metadata and controls
356 lines (272 loc) · 9.3 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
### Enable/Disable SPN ###
# -------------------------------
# CONFIGURATION
# -------------------------------
$TenantId = ""
$ClientId = ""
$ClientSecret = ""
$TargetSpnName = ""
# -------------------------------
# GET ACCESS TOKEN
# -------------------------------
$TokenUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$TokenBody = @{
client_id = $ClientId
scope = "https://graph.microsoft.com/.default"
client_secret = $ClientSecret
grant_type = "client_credentials"
}
$TokenResponse = Invoke-RestMethod `
-Method POST `
-Uri $TokenUrl `
-Body $TokenBody `
-ContentType "application/x-www-form-urlencoded"
$AccessToken = $TokenResponse.access_token
$Headers = @{
Authorization = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
# -------------------------------
# FIND SERVICE PRINCIPAL
# -------------------------------
$SpnUri = "https://graph.microsoft.com/v1.0/servicePrincipals?`$filter=displayName eq '$TargetSpnName'"
$SpnResponse = Invoke-RestMethod `
-Method GET `
-Uri $SpnUri `
-Headers $Headers
if ($SpnResponse.value.Count -eq 0) {
Write-Host "Service Principal not found"
exit
}
$ServicePrincipalId = $SpnResponse.value[0].id
Write-Host "Service Principal ID:" $ServicePrincipalId
# -------------------------------
# DISABLE SERVICE PRINCIPAL
# -------------------------------
$DisableBody = @{
accountEnabled = $true
} | ConvertTo-Json
$DisableUri = "https://graph.microsoft.com/v1.0/servicePrincipals/$ServicePrincipalId"
Invoke-RestMethod `
-Method PATCH `
-Uri $DisableUri `
-Headers $Headers `
-Body $DisableBody
Write-Host "Service Principal has been disabled successfully"
### Validate SPN ###
# ------------------------------------------------
# CONFIGURATION
# ------------------------------------------------
$TenantId = ""
$ClientId = ""
$ClientSecret = ""
# Recommended: use AppId (Application ID)
$AppId = ""
# ------------------------------------------------
# GET ACCESS TOKEN
# ------------------------------------------------
$TokenUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$TokenBody = @{
client_id = $ClientId
scope = "https://graph.microsoft.com/.default"
client_secret = $ClientSecret
grant_type = "client_credentials"
}
try {
$TokenResponse = Invoke-RestMethod `
-Method POST `
-Uri $TokenUrl `
-Body $TokenBody `
-ContentType "application/x-www-form-urlencoded"
}
catch {
Write-Host "❌ Failed to acquire token" -ForegroundColor Red
Write-Host $_
return
}
$Headers = @{
Authorization = "Bearer $($TokenResponse.access_token)"
}
# ------------------------------------------------
# RESOLVE SERVICE PRINCIPAL USING APP ID
# ------------------------------------------------
$UriSPN = "https://graph.microsoft.com/v1.0/servicePrincipals?`$filter=appId eq '$AppId'"
Write-Host "Resolving Service Principal using AppId..."
Write-Host $UriSPN
try {
$SPNResponse = Invoke-RestMethod `
-Method GET `
-Uri $UriSPN `
-Headers $Headers
}
catch {
Write-Host "❌ Failed to query Service Principals" -ForegroundColor Red
Write-Host $_
return
}
# Validate response
if (-not $SPNResponse.value -or $SPNResponse.value.Count -eq 0) {
Write-Host "❌ No Service Principal found for AppId: $AppId" -ForegroundColor Red
return
}
# Debug output (helps in troubleshooting)
Write-Host ""
Write-Host "Service Principal Found:"
$SPNResponse.value | Select-Object displayName, id, appId | Format-List
# Extract Object ID
$ServicePrincipalId = $SPNResponse.value[0].id
if (-not $ServicePrincipalId) {
Write-Host "❌ Service Principal ID is empty" -ForegroundColor Red
return
}
# ------------------------------------------------
# GET SERVICE PRINCIPAL DETAILS
# ------------------------------------------------
$UriDetails = "https://graph.microsoft.com/v1.0/servicePrincipals('$ServicePrincipalId')?`$select=displayName,appId,accountEnabled"
Write-Host ""
Write-Host "Fetching Service Principal details..."
Write-Host $UriDetails
try {
$Response = Invoke-RestMethod `
-Method GET `
-Uri $UriDetails `
-Headers $Headers
}
catch {
Write-Host "❌ Failed to retrieve Service Principal details" -ForegroundColor Red
Write-Host $_
return
}
# ------------------------------------------------
# OUTPUT
# ------------------------------------------------
Write-Host ""
Write-Host "===== Service Principal Details ====="
Write-Host ("Display Name : {0}" -f $Response.displayName)
Write-Host ("App ID : {0}" -f $Response.appId)
Write-Host ("Object ID : {0}" -f $ServicePrincipalId)
Write-Host ("Account Enabled : {0}" -f $Response.accountEnabled)
### Enable/Disable User
# ------------------------------------------------
# CONFIGURATION
# ------------------------------------------------
$TenantId = ""
$ClientId = ""
$ClientSecret = ""
# User (use UPN or Object ID)
$UserId = "" # or GUID
# ------------------------------------------------
# GET ACCESS TOKEN
# ------------------------------------------------
$TokenUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$TokenBody = @{
client_id = $ClientId
scope = "https://graph.microsoft.com/.default"
client_secret = $ClientSecret
grant_type = "client_credentials"
}
try {
$TokenResponse = Invoke-RestMethod `
-Method POST `
-Uri $TokenUrl `
-Body $TokenBody `
-ContentType "application/x-www-form-urlencoded"
}
catch {
Write-Host "❌ Failed to acquire token" -ForegroundColor Red
Write-Host $_
return
}
$Headers = @{
Authorization = "Bearer $($TokenResponse.access_token)"
"Content-Type" = "application/json"
}
# ------------------------------------------------
# DISABLE USER
# ------------------------------------------------
$Uri = "https://graph.microsoft.com/v1.0/users/$UserId"
$Body = @{
accountEnabled = $true
} | ConvertTo-Json
Write-Host "Disabling user: $UserId"
Write-Host $Uri
try {
Invoke-RestMethod `
-Method PATCH `
-Uri $Uri `
-Headers $Headers `
-Body $Body
Write-Host "✅ User disabled successfully"
}
catch {
Write-Host "❌ Failed to disable user" -ForegroundColor Red
Write-Host $_
}
### Remove From Global Admin ###
# ------------------------------------------------
# CONFIGURATION
# ------------------------------------------------
$TenantId = ""
$ClientId = ""
$ClientSecret = ""
$UserUPN = ""
# Global Admin Role Template ID
$GlobalAdminRoleTemplateId = "62e90394-69f5-4237-9190-012177145e10"
# ------------------------------------------------
# GET TOKEN
# ------------------------------------------------
$TokenUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$TokenBody = @{
client_id = $ClientId
scope = "https://graph.microsoft.com/.default"
client_secret = $ClientSecret
grant_type = "client_credentials"
}
$TokenResponse = Invoke-RestMethod -Method POST -Uri $TokenUrl -Body $TokenBody -ContentType "application/x-www-form-urlencoded"
$Headers = @{
Authorization = "Bearer $($TokenResponse.access_token)"
"Content-Type" = "application/json"
}
# ------------------------------------------------
# GET USER
# ------------------------------------------------
$User = Invoke-RestMethod -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$UserUPN" -Headers $Headers
$UserId = $User.id
# ------------------------------------------------
# GET GLOBAL ADMIN ROLE (ACTIVATE IF NEEDED)
# ------------------------------------------------
$Roles = Invoke-RestMethod -Method GET -Uri "https://graph.microsoft.com/v1.0/directoryRoles" -Headers $Headers
$GlobalAdminRole = $Roles.value | Where-Object { $_.roleTemplateId -eq $GlobalAdminRoleTemplateId }
# If role not found, activate it
if (-not $GlobalAdminRole) {
Write-Host "Activating Global Administrator role..."
$ActivateBody = @{
roleTemplateId = $GlobalAdminRoleTemplateId
} | ConvertTo-Json
Invoke-RestMethod -Method POST `
-Uri "https://graph.microsoft.com/v1.0/directoryRoles" `
-Headers $Headers `
-Body $ActivateBody
# Re-fetch roles
$Roles = Invoke-RestMethod -Method GET -Uri "https://graph.microsoft.com/v1.0/directoryRoles" -Headers $Headers
$GlobalAdminRole = $Roles.value | Where-Object { $_.roleTemplateId -eq $GlobalAdminRoleTemplateId }
}
$RoleId = $GlobalAdminRole.id
# ------------------------------------------------
# CHECK MEMBERSHIP
# ------------------------------------------------
$Members = Invoke-RestMethod -Method GET `
-Uri "https://graph.microsoft.com/v1.0/directoryRoles/$RoleId/members" `
-Headers $Headers
$UserMember = $Members.value | Where-Object { $_.id -eq $UserId }
if (-not $UserMember) {
Write-Host "User is NOT a Global Administrator"
return
}
# ------------------------------------------------
# REMOVE USER FROM ROLE
# ------------------------------------------------
$RemoveUri = "https://graph.microsoft.com/v1.0/directoryRoles/$RoleId/members/$UserId/`$ref"
Write-Host "Removing Global Admin role from user..."
Invoke-RestMethod -Method Delete -Uri $RemoveUri -Headers $Headers
Write-Host "✅ User removed from Global Administrator role"