-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMicrosoftPartnerCenterAPI.psm1
719 lines (702 loc) · 27.1 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<#
.Synopsis
Gets an Azure Active Directory authentication token using the Azure Directory Authentication Library.
.DESCRIPTION
Long description
.EXAMPLE
Get-MPCAzureADToken -ApplicationID f5e4f291-6e60-48c0-bc2e-e72e9a3a0464 -ResourceUri https://api.partnercenter.microsoft.com -Credential (Get-Credential) -Domain netgain.onmicrosoft.com
#>
function Get-MPCAzureADToken {
[CmdletBinding()]
Param(
# ID of application created in Azure Active Directory
[Parameter(Mandatory=$true)]
[string]$ApplicationID,
# Azure AD Domain
[Parameter(Mandatory=$true)]
[string]$Domain,
# Uri of the resource to request the token for
[Parameter(Mandatory=$true)]
[string]$ResourceUri,
[pscredential]$Credential,
$Secret,
[switch]$NullTokenCache,
$CustomTokenCache,
[switch]$FileCache,
[string]$RedirectUri = 'http://localhost'
)
Write-Verbose 'Getting Azure AD token'
$adalPath = Join-Path -Path $PSScriptRoot -ChildPath '\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'
$adalPlatformPath = Join-Path -Path $PSScriptRoot -ChildPath '\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll'
Add-Type -Path $adalPath
Add-Type -Path $adalPlatformPath
if ($Credential -or $Secret) {
$body = @{
resource = $ResourceUri
client_id = $ApplicationID
}
if ($Credential) {
$body.grant_type = 'password'
$body.username = $Credential.UserName
$body.password = $Credential.GetNetworkCredential().Password
}
elseif ($Secret) {
$body.grant_type = 'client_credentials'
$body.client_secret = $secret
}
$params = @{
Uri = "https://login.microsoftonline.com/$Domain/oauth2/token"
Method = 'Post'
Body = $body
}
Write-Output (Invoke-RestMethod @params)
}
else {
if ($NullTokenCache) {
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/$Domain/",$null)
}
elseif ($CustomTokenCache) {
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/$Domain/",$CustomTokenCache)
}
elseif ($FileCache) {
$assemblies = (
(Join-Path -Path $PSScriptRoot -ChildPath '\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'),
(Join-Path -Path $PSScriptRoot -ChildPath '\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll'),
"System.Runtime, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Security"
)
$source = @"
using Microsoft.IdentityModel.Clients;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.IO;
using System.Security.Cryptography;
namespace TodoListClient
{
// This is a simple persistent cache implementation for a desktop application.
// It uses DPAPI for storing tokens in a local file.
public class FileCache : TokenCache
{
public string CacheFilePath;
private static readonly object FileLock = new object();
// Initializes the cache against a local file.
// If the file is already rpesent, it loads its content in the ADAL cache
public FileCache(string filePath)
{
CacheFilePath = filePath;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
lock (FileLock)
{
this.Deserialize(File.Exists(CacheFilePath) ? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath), null, DataProtectionScope.CurrentUser) : null);
}
}
// Empties the persistent store.
public override void Clear()
{
base.Clear();
File.Delete(CacheFilePath);
}
// Triggered right before ADAL needs to access the cache.
// Reload the cache from the persistent store in case it changed since the last access.
void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
lock (FileLock)
{
this.Deserialize(File.Exists(CacheFilePath) ? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath),null,DataProtectionScope.CurrentUser) : null);
}
}
// Triggered right after ADAL accessed the cache.
void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if the access operation resulted in a cache update
if (this.HasStateChanged)
{
lock (FileLock)
{
// reflect changes in the persistent store
File.WriteAllBytes(CacheFilePath, ProtectedData.Protect(this.Serialize(),null,DataProtectionScope.CurrentUser));
// once the write operation took place, restore the HasStateChanged bit to false
this.HasStateChanged = false;
}
}
}
}
}
"@
try {
Add-Type -TypeDefinition $source -ReferencedAssemblies $assemblies
}
catch {}
$cache = New-Object TodoListClient.FileCache("c:\temp\$Domain.dat")
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/$Domain/",$cache)
}
else {
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/$Domain/")
}
$token = $authenticationContext.AcquireTokenSilentAsync($ResourceUri, $ApplicationID)
Start-Sleep -Seconds 1
if ($token.Exception) {
$platformParameters = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" -ArgumentList 'Auto'
$token = $authenticationContext.AcquireTokenAsync($ResourceUri, $ApplicationID, $RedirectUri, $platformParameters)
if ($token.Exception) {
throw $token.Exception
}
else {
$token.Wait()
Write-Output $token.Result
}
}
else {
$token.Wait()
Write-Output $token.Result
}
}
}
<#
.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,
# Partner domain prefix for onmicrosoft.com domain
[Parameter(Mandatory=$true)]
[string]$PartnerDomainPrefix,
$MpcAzureAdToken,
[pscredential]$Credential,
[switch]$NullTokenCache,
$CustomTokenCache,
[switch]$FileCache
)
if (!$MpcAzureAdToken) {
$getMPCAzureADTokenParams = @{
ApplicationID = $ApplicationID
Domain = "$PartnerDomainPrefix.onmicrosoft.com"
ResourceUri = 'https://api.partnercenter.microsoft.com'
ErrorAction = 'Stop'
}
if ($NullTokenCache) {
$getMPCAzureADTokenParams.NullTokenCache = $true
}
elseif ($CustomTokenCache) {
$getMPCAzureADTokenParams.CustomTokenCache = $CustomTokenCache
}
elseif ($FileCache) {
$getMPCAzureADTokenParams.FileCache = $true
}
if ($Credential) {
$getMPCAzureADTokenParams.Credential = $Credential
$MpcAzureAdToken = (Get-MPCAzureADToken @getMPCAzureADTokenParams).access_token
}
else {
$MpcAzureAdToken = (Get-MPCAzureADToken @getMPCAzureADTokenParams).AccessToken
}
}
$params = @{
Uri = 'https://api.partnercenter.microsoft.com/generatetoken'
Headers = @{Authorization = "Bearer $MPCAzureADToken"}
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
}
function Get-MPCAzureUsage {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Customer ID
[Parameter(Mandatory=$true)]
[string]$CustomerID,
# Subscription ID
[Parameter(Mandatory=$true)]
[pscustomobject]$SubscriptionID,
# Start time
[Parameter(Mandatory=$true)]
[datetime]$StartTime,
# End time
[Parameter(Mandatory=$true)]
[datetime]$EndTime,
# Defines the granularity of usage aggregations
[Parameter()]
[ValidateSet('daily','hourly')]
[string]$Granularity
)
$universalSortableStartTime = Get-Date -Date $StartTime -Format u
$universalSortableEndTime = Get-Date -Date $EndTime -Format u
$baseUri = 'https://api.partnercenter.microsoft.com/v1/'
$uri = $baseUri + "customers/$CustomerID/subscriptions/$SubscriptionID/utilizations/azure?" +
"start_time=$universalSortableStartTime&end_time=$universalSortableEndTime"
if ($Granularity) {
$uri += "&granularity=$Granularity"
}
$headers = @{Authorization = "Bearer $MPCToken"}
$params = @{
Uri = $uri
Headers = $headers
Method = 'Get'
ContentType = 'application/json'
}
$items = @()
$totalCount = 0
$result = (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
$items += $result.items
$totalCount += $result.totalCount
if ($result.links.next) {
do {
$params2 = $params.Clone()
$params2.Uri = $baseUri + $result.links.next.uri
$headers2 = $headers.Clone()
$headers2.Add($result.links.next.headers.key, $result.links.next.headers.value)
$params2.Headers = $headers2
$result = (Invoke-WebRequest @params2).Content.Substring(1) | ConvertFrom-Json
$items += $result.items
$totalCount += $result.totalCount
}
until ($null -eq $result.links.next)
}
$output = [pscustomobject]@{
totalCount = $totalCount
items = $items
links = $result.links
}
Write-Output $output
}
function Get-MPCCustomerServiceCostsSummary {
[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/servicecosts/mostrecent"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Output ($result = Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
function Get-MPCCustomerUsageSummary {
[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/usagesummary"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Output ($result = Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
function Get-MPCSubscriptionUsage {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken,
# Customer ID
[Parameter(Mandatory=$true)]
[string]$CustomerID,
# Subscription ID
[Parameter(Mandatory=$true)]
[pscustomobject]$SubscriptionID
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/customers/$CustomerID/subscriptions/$SubscriptionID/usagerecords/resources"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Output ($result = Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
function Get-MPCInvoices {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/invoices"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Output ($result = Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
function Get-MPCAzurePrices {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken
)
$params = @{
Uri = "https://api.partnercenter.microsoft.com/v1/ratecards/azure"
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Output ($result = Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}
function Get-MPCCustomers {
[CmdletBinding()]
Param(
# Microsoft Partner Center authentication token
[Parameter(Mandatory=$true)]
[string]$MPCToken
)
$Uri = 'https://api.partnercenter.microsoft.com/v1/customers'
$params = @{
Uri = $Uri
Headers = @{Authorization = "Bearer $MPCToken"}
Method = 'Get'
ContentType = 'application/json'
}
Write-Verbose 'Getting customers'
Write-Output (Invoke-WebRequest @params).Content.Substring(1) | ConvertFrom-Json
}