forked from corydwood/MicrosoftPartnerCenterAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoftPartnerCenterAPI.psm1
393 lines (384 loc) · 14.4 KB
/
MicrosoftPartnerCenterAPI.psm1
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
<#
.Synopsis
Gets an Azure Active Directory authentication token.
.DESCRIPTION
Long description
.EXAMPLE
Get-MPCAzureADToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential) -DomainPrefix netgain
#>
function Get-MPCAzureADToken {
[CmdletBinding()]
Param(
# ID of application created in Azure Active Directory
[Parameter(Mandatory=$true)]
[string]$ApplicationID,
# User credential to access Azure Active Directory
[Parameter(Mandatory=$true)]
[pscredential]$Credential,
# Partner domain prefix for onmicrosoft.com domain
[Parameter(Mandatory=$true)]
[string]$DomainPrefix
)
$username = $Credential.Username
$password = $Credential.GetNetworkCredential().Password
$body = "resource=https://api.partnercenter.microsoft.com&client_id=$ApplicationID&grant_type=password&username=$Username&password=$Password&scope=openid"
Write-Verbose 'Getting Azure AD token'
$mpcAzureAdTokenRequest = Invoke-WebRequest -Uri https://login.windows.net/$DomainPrefix.onmicrosoft.com/oauth2/token -Method Post -Body $body
Write-Output ($mpcAzureAdTokenRequest.Content | ConvertFrom-Json)
}
<#
.Synopsis
Gets a Microsoft Partner Center token when given an Azure Active Directory token.
.DESCRIPTION
Long description
.EXAMPLE
Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential) -PartnerDomainPrefix netgain
#>
function Get-MPCToken {
[CmdletBinding()]
Param(
# ID of application created in Azure Active Directory
[Parameter(Mandatory=$true)]
[string]$ApplicationID,
# User credential to access Azure Active Directory
[Parameter(Mandatory=$true)]
[pscredential]$Credential,
# Partner domain prefix for onmicrosoft.com domain
[Parameter(Mandatory=$true)]
[string]$PartnerDomainPrefix
)
$mpcAzureAdToken = Get-MPCAzureADToken -ApplicationID $ApplicationID -Credential $Credential -DomainPrefix $PartnerDomainPrefix -ErrorAction Stop
$params = @{
Uri = 'https://api.partnercenter.microsoft.com/generatetoken'
Headers = @{Authorization = "Bearer $($MPCAzureADToken.access_token)"}
Method = 'Post'
Body = 'grant_type=jwt_token'
}
Write-Verbose 'Getting Microsoft Partner Center token'
Write-Output ((Invoke-WebRequest @params).Content | ConvertFrom-Json)
}
<#
.Synopsis
Checks the domain availability for the given onmicrosoft.com domain prefix.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
Get-MPCDomainAvailability -CustomerDomainPrefix 'mydomain' -MPCToken $mpcToken.access_token
#>
function Get-MPCDomainAvailability {
[CmdletBinding()]
Param(
# Customer domain prefix for onmicrosoft.com domain
[Parameter(Mandatory=$true)]
[string]$CustomerDomainPrefix,
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/validations/checkdomainavailability/$CustomerDomainPrefix"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Verbose 'Checking domain availability'
$result = (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
if ($result -eq $true) {Write-Verbose "$CustomerDomainPrefix.onmicrosoft.com available."}
else {Write-Verbose "$($CustomerDomainPrefix.onmicrosoft.com) not available."}
$result
}
<#
.Synopsis
Creates a new customer in the Microsoft Partner Center.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
New-MPCCustomer -CustomerDomainPrefix mydomain -MPCToken $mpcToken.access_token -CompanyName 'My Company' -FirstName 'John' -LastName 'Doe' `
-Email 'John.Doe@MyAlternateDomain.com -PhoneNumber 5555555555 -AddressLine1 '1 Microsoft Way' -City Redmond -State WA -PostalCode 98052
#>
function New-MPCCustomer {
[CmdletBinding()]
Param(
# Customer domain prefix for onmicrosoft.com domain
[Parameter(Mandatory=$true)]
[string]$CustomerDomainPrefix,
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Name of company/organization
[Parameter(Mandatory=$true)]
[string]$CompanyName,
# The first name of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$FirstName,
# The last name of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$LastName,
# The email address of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$Email,
# The phone number of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$PhoneNumber,
# Address line 1 of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$AddressLine1,
# Address line 2 of a contact at the customer's company/organization
[string]$AddressLine2,
# Address line 3 of a contact at the customer's company/organization
[string]$AddressLine3,
# City of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$City,
# State of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$State,
# Postal/zip code of a contact at the customer's company/organization
[Parameter(Mandatory=$true)]
[string]$PostalCode,
# Country of a contact at the customer's company/organization
[string]$Country = 'US',
# The preferred culture for communication and currency, such as "en-us"
[string]$Culture = 'EN-US',
# The preferred language for communication
[string]$Language = 'En'
)
$mpcCustomerObject = [pscustomobject]@{
CompanyProfile = @{
Domain = "$CustomerDomainPrefix.onmicrosoft.com"
}
BillingProfile = @{
Email = $Email
Culture = $Culture
Language = $Language
CompanyName = $CompanyName
DefaultAddress = @{
Country = $Country
City = $City
State = $State
AddressLine1 = $AddressLine1
PostalCode = $PostalCode
FirstName = $FirstName
LastName = $LastName
PhoneNumber = $PhoneNumber
}
}
}
if ($AddressLine2) {$mpcCustomerObject.BillingProfile.DefaultAddress.AddressLine2 = $AddressLine2}
if ($AddressLine3) {$mpcCustomerObject.BillingProfile.DefaultAddress.AddressLine3 = $AddressLine3}
$mpcCustomerJson = $mpcCustomerObject | ConvertTo-Json
$params = @{
Uri = 'https://api.partnercenter.microsoft.com/v1/customers'
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Post'
Body = $mpcCustomerJson
ContentType = 'application/json'
}
Write-Verbose 'Creating customer'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
<#
.Synopsis
Gets a list of offers available in the Microsoft Partner Center
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
Get-MPCOffers -MPCToken $mpcToken.access_token
#>
function Get-MPCOffers {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Country where offer applies
[string]$Country = 'US'
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/offers?country=$Country"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Verbose 'Getting offers'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
<#
.Synopsis
Creates a new order in the Microsoft Partner Center.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
$mpcOffer = (Get-MPCOffers -MPCToken $mpcToken.access_token).items | select -First 1
New-MPCOrder -MPCToken $mpcToken.access_token -CustomerID e2dcbfa5-cc31-4062-a76f-34b4c2e92a72 -OfferID $mpcOffer.id -Quantity 5
#>
function New-MPCOrder {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Customer ID
[Parameter(Mandatory=$true)]
[string]$CustomerID,
# Offer ID
[Parameter(Mandatory=$true)]
[string]$OfferID,
# The number of licenses
[Parameter(Mandatory=$true)]
[int]$Quantity
)
$orderJson = [pscustomobject]@{
ReferenceCustomerId = $CustomerID
LineItems = @(
@{
LineItemNumber = 0
OfferId = $OfferID
Quantity = $Quantity
}
)
Attributes = @{
ObjectType = "Order"
}
} | ConvertTo-Json
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerID/orders"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Post'
ContentType = 'application/json'
Body = $orderJson
}
Write-Verbose 'Creating order'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
<#
.Synopsis
Removes a customer from the Microsoft Partner Center.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
Remove-MPCCustomer -MPCToken $mpcToken.access_token -CustomerID e2dcbfa5-cc31-4062-a76f-34b4c2e92a72
#>
function Remove-MPCCustomer {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Customer ID
[Parameter(Mandatory=$true)]
[string]$CustomerID
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerID"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Delete'
ContentType = 'application/json'
}
Write-Verbose 'Removing customer'
Write-Output (Invoke-WebRequest @params).Content | ConvertFrom-Json
}
<#
.Synopsis
Gets a customer from the Microsoft Partner Center.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
Get-MPCCustomer -MPCToken $mpcToken.access_token -Domain netgaintest1.onmicrosoft.com
#>
function Get-MPCCustomer {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Custom or onmicrosoft.com domain
[Parameter(Mandatory=$true)]
[string]$Domain
)
$Uri = @"
https://api.partnercenter.microsoft.com/v1/customers?size=0&filter={"Field":"Domain","Value":"$Domain","Operator":"starts_with"}
"@
$params = @{
Uri = $Uri
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Verbose 'Getting customer'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
<#
.Synopsis
Gets customer subscriptions from the Microsoft Partner Center.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
Get-MPCSubscriptions -MPCToken $mpcToken.access_token -CustomerID e2dcbfa5-cc31-4062-a76f-34b4c2e92a72
#>
function Get-MPCSubscriptions {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Customer ID
[Parameter(Mandatory=$true)]
[string]$CustomerID
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerID/subscriptions"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Verbose 'Getting subscriptions'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
<#
.Synopsis
Updates a customer's subscription in the Microsoft Partner Center.
.DESCRIPTION
Long description
.EXAMPLE
$mpcToken = Get-MPCToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -Credential (Get-Credential)
$mpcSubscription = (Get-MPCSubscriptions -MPCToken $mpcToken.access_token -CustomerID e2dcbfa5-cc31-4062-a76f-34b4c2e92a72).items | select -First 1
Update-MPCSubscription -MPCToken $mpcToken -CustomerID e2dcbfa5-cc31-4062-a76f-34b4c2e92a72 -Subscription $mpcSubscription -Quantity 5
#>
function Update-MPCSubscription {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Customer ID
[Parameter(Mandatory=$true)]
[string]$CustomerID,
# Object containing subscription information. Received from Get-MPCSubscriptions.
[Parameter(Mandatory=$true)]
[pscustomobject]$Subscription,
# The quantity of licenses to set the subscription to.
[Parameter(Mandatory=$true)]
[string]$Quantity
)
$Subscription.quantity = $Quantity
# Must be removed to prevent error
$Subscription.psobject.Properties.Remove('billingCycle')
$body = $Subscription | ConvertTo-Json
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerID/subscriptions/$($Subscription.id)"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Patch'
ContentType = 'application/json'
Body = $body
}
Write-Verbose 'Updating quantity on subscription'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}