-
Notifications
You must be signed in to change notification settings - Fork 214
/
AdminAPI.ps1
364 lines (311 loc) · 10.9 KB
/
AdminAPI.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
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
# This file contains functions utilising https://admin.microsoft.com API
# Approves the delegated admin request
# Sep 22nd 2021
function Approve-MSPartnerDelegatedAdminRequest
{
<#
.SYNOPSIS
Assigns Delegated Admin Permissions (DAP) for the given partner organisation.
.DESCRIPTION
Assigns Delegated Admin Permissions (DAP) for the given partner organisation.
.Parameter TenantId
TenantId of the partner organisation.
.Parameter Domain
Any registered domain of the partner organisation.
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>Approve-AADIntMSPartnerDelegatedAdminRequest -Domain company.com
responseCode message
------------ -------
success
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>Approve-AADIntMSPartnerDelegatedAdminRequest -TenantId c7e52a77-e461-4f2e-a652-573305414be9
responseCode message
------------ -------
success
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName='TenantId',Mandatory=$True)]
[guid]$TenantId,
[Parameter(ParameterSetName='Domain',Mandatory=$True)]
[String]$Domain,
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
if($Domain)
{
$TenantId = Get-TenantID -Domain $Domain
}
$body = @{
"authorizeDelegateAdminData" = [ordered]@{
"msppId" = 0
"partnerId" = $TenantId
"companyName" = " "
"indirectCSPId" = ""
"userTenantId" = (Read-Accesstoken $AccessToken).tid
"enableDap" = $true
"invitationType" = "Administration"
"address" = $null
"roles" = @(
"62e90394-69f5-4237-9190-012177145e10" # Global Administrator
"729827e3-9c14-49f7-bb1b-9608f156bbb8" # Helpdesk Administrator
)
"userPrincipalName" = $null
}
}
try
{
$response = Invoke-AdminAPI -Method Post -AccessToken $AccessToken -Url "fd/commerceMgmt/partnermanage/partners/csp/delegatedaccess?api-version=2.1" -Body ($body | ConvertTo-Json)
}
catch
{
Write-Error ($_.ErrorDetails.Message | ConvertFrom-Json).error.message
return
}
$response
}
}
# Gets the list of partners
# Dec 9th 2021
function Get-MSPartners
{
<#
.SYNOPSIS
Shows organisation's partners.
.DESCRIPTION
Shows organisation's partners using Admin API.
.Parameter AccessToken
Access token to retrieve partners.
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>Get-AADIntMSPartners
Identity : b1f6d5cc-f1d3-41d9-b88c-1d177aaf171b
DisplayName : Partner Ltd
Email : pmanager@company.com
Website : http://www.company.com
Phone : +1234567890
Relationship : Indirect Reseller and Admin
TypeDetail : PartnerAdmin
CanDelete : False
CanRemoveDap : True
AllDataRetrieved : True
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
try
{
$response = Invoke-AdminAPI -AccessToken $AccessToken -Url "admin/api/Partners/GetPartners" -Method Post
}
catch
{
if($_.ErrorDetails.Message)
{
throw $_.ErrorDetails.Message
}
else
{
throw $_.Exception.Message
}
return
}
$response
}
}
# Returns access token for the requested resource
# Dec 9th 2021
function Get-AccessTokenUsingAdminAPI
{
<#
.SYNOPSIS
Gets access token for the requested resource using Admin API.
.DESCRIPTION
Gets access token for the requested resource using Admin API.
The client id of the retuned access token is 00000006-0000-0ff1-ce00-000000000000 (Microsoft Office 365 Portal).
.Parameter Access
Some supported type of access token.
.Parameter TokenType
The type of the access token to return. Can be one of "PortalAT","BusinessStoreAT"
.Parameter Resource
Gets an access token for the given resource. Can be one of "https://management.core.windows.net/","https://graph.microsoft.com/"
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>$at = Get-AADIntAccessTokenUsingAdminAPI -TokenType PortalAT
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>$at = Get-AADIntAccessTokenUsingAdminAPI -Resource https://management.core.windows.net/
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName='Normal',Mandatory=$True)]
[ValidateSet('PortalAT','BusinessStoreAT')]
[String]$TokenType,
[Parameter(ParameterSetName='Resource',Mandatory=$True)]
[ValidateSet('https://graph.microsoft.com/','https://management.core.windows.net/')]
[String]$Resource,
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$False)]
[Microsoft.PowerShell.Commands.WebRequestSession]$WebSession,
[Parameter(Mandatory=$False)]
[bool]$SaveToCache=$False
)
Process
{
if($TokenType)
{
$access_token = Invoke-AdminAPI -AccessToken $AccessToken -Url "admin/api/users/getuseraccesstoken?tokenType=$TokenType" -Method Get -WebSession $WebSession
}
else
{
$access_token = Invoke-AdminAPI -AccessToken $AccessToken -Url "admin/api/users/token" -Method Post -Body "=$Resource"
}
if($SaveToCache)
{
Add-AccessTokenToCache -AccessToken $access_token
}
else
{
$access_token
}
}
}
# Approves the delegated admin request
# Dec 11th 2021
function Remove-MSPartnerDelegatedAdminRoles
{
<#
.SYNOPSIS
Removes Delegated Admin Permissions (DAP) from the given partner organisation.
.DESCRIPTION
Removes Delegated Admin Permissions (DAP) from the given partner organisation.
.Parameter TenantId
TenantId of the partner organisation.
.Parameter Domain
Any registered domain of the partner organisation.
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>Remove-AADIntMSPartnerDelegatedAdminRoles -Domain company.com
responseCode message
------------ -------
success
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>Remove-AADIntMSPartnerDelegatedAdminRoles -TenantId c7e52a77-e461-4f2e-a652-573305414be9
responseCode message
------------ -------
success
#>
[cmdletbinding()]
Param(
[Parameter(ParameterSetName='TenantId',Mandatory=$True)]
[guid]$TenantId,
[Parameter(ParameterSetName='Domain',Mandatory=$True)]
[String]$Domain,
[Parameter(Mandatory=$False)]
[String]$AccessToken
)
Process
{
if($Domain)
{
$TenantId = Get-TenantID -Domain $Domain
}
try
{
$response = Invoke-AdminAPI -Method Delete -AccessToken $AccessToken -Url "fd/commerceMgmt/partnermanage/partners/csp/$TenantId/delegatedaccess?api-version=2.1"
}
catch
{
Write-Error ($_.ErrorDetails.Message | ConvertFrom-Json).error.message
return
}
$response
}
}
# Returns tenant organisation information
# Jan 26th 2022
function Get-TenantOrganisationInformation
{
<#
.SYNOPSIS
Returns organisation information for the given tenant.
.DESCRIPTION
Returns organisation information for the given tenant using commercial API used to get Partner Tenant information.
.Parameter AccessToken
Access Token used to fetch information. Can be any standard user of any tenant.
.Parameter TenantId
TenantId of the target tenant.
.Parameter Domain
Domain name of the target tenant.
.Example
PS C:\>Get-AADIntAccessTokenForAdmin -SaveToCache
PS C:\>Get-AADIntTenantOrganisationInformation -Domain "company.com"
TenantId : 043050e2-7993-416a-ae66-108ab1951612
CompanyName : Company Ltd
StreetAddress : 10 Wall Street
ApartmentOrSuite : 666
City : New York
StateOrProvince : NY
PostalCode : 10005
CountryCode : US
PhoneNumber : 1234567890
FirstName : John
LastName : Doe
#>
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(ParameterSetName='Domain',Mandatory=$True)]
[String]$Domain,
[Parameter(ParameterSetName='TenantId',Mandatory=$True)]
[guid]$TenantId
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://admin.microsoft.com" -ClientId "d3590ed6-52b3-4102-aeff-aad2292ab01c"
if($Domain)
{
[guid]$TenantId = [guid](Get-TenantID -Domain $Domain)
}
if($TenantId -eq ([guid](Read-AccessToken $AccessToken).tid))
{
Write-Error "Can't query information from your own tenant. Log in to another tenant and try again."
return
}
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Accept" = "application/json; charset=utf-8"
}
$response = Invoke-WebRequest -UseBasicParsing -uri "https://admin.microsoft.com/fd/commerceMgmt/partnermanage/partners/csp/$($TenantId.toString())/delegatedaccess?invType=Administration&api-version=2.1" -Headers $headers
# Content is utf-8 encoded json, but response headers don't have encoding information
$responseBytes = New-Object byte[] $response.RawContentLength
$response.RawContentStream.Read($responseBytes,0,$response.RawContentLength)
$responseObj = ConvertFrom-Json -InputObject ([text.encoding]::UTF8.GetString($responseBytes))
$tenantInfo = $responseObj.authorizeDelegateAdminData
$attributes = [ordered]@{
"TenantId" = $tenantInfo.partnerId
"CompanyName" = $tenantInfo.companyName
"StreetAddress" = $tenantInfo.address.line1
"ApartmentOrSuite" = $tenantInfo.address.line2
#"Line3" = $tenantInfo.address.line3
"City" = $tenantInfo.address.city
"StateOrProvince" = $tenantInfo.address.state
"PostalCode" = $tenantInfo.address.postalCode
"CountryCode" = $tenantInfo.address.countryCode
"PhoneNumber" = $tenantInfo.address.phoneNumber
"FirstName" = $tenantInfo.address.firstName
"LastName" = $tenantInfo.address.lastName
}
New-Object psobject -Property $attributes
}
}