This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
PartnerCenterUsage.psm1
298 lines (257 loc) · 12.1 KB
/
PartnerCenterUsage.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
Set-StrictMode -Version latest
<#
© 2018 Microsoft Corporation. All rights reserved. This sample code is not supported under any Microsoft standard support program or service.
This sample code is provided AS IS without warranty of any kind. Microsoft disclaims all implied warranties including, without limitation,
any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance
of the sample code and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business
profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the
sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
#>
# Load common code
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\commons.ps1"
<#
.SYNOPSIS
Retrieve a collection of usage records for the specified date range. Usage records are only available for the last 90 days.
.DESCRIPTION
The Get-PCUsage cmdlet returns the usage records specified by the start and end times.
.PARAMETER SaToken
Specifies a authentication token you have created with your Partner Center credentials.
.PARAMETER TenantId
Specifies the tenant used for scoping this cmdlet.
.PARAMETER SubscriptionId
Specifies a subscription if for which to return usage information.
.PARAMETER StartTime
Specifies the start time for which to retrieve usage information. Usage is only available for the last 90 days, therefore this value cannot be more than 90 days from the current date.
.PARAMETER EndTime
Specifies the end time for which to retrieve usage information.
.PARAMETER Granularity
Specifies the granularity of the data to return. Valid values are: daily or hourly. The default value is daily.
.PARAMETER ShowDetail
Default this is set to $true. If set to $true, the utilization records will be split by the resource instance levels. If set to false, the utilization records will be aggregated on the resource level.
.PARAMETER ResultSize
Specifies the maximum number of results to return. The default value is 1000.
.PARAMETER ContinuationLink
Specifies a variable to save the URL to retrieve additional results.
.EXAMPLE
Get-PCUsage -TenantId 2a14b164-f983-4048-92e1-4f9591b87445 -SubscriptionId b027a4b3-5487-413b-aa48-ec8733c874d6 -StartTime '06-12-2018 00:00:00' -EndTime '06-31-2018 23:59:59' -Granularity hourly -ResultSize 2000
Return up to 2000 hourly usage records for the specified date range.
.NOTES
#>
function Get-PCUsage {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][String]$SubscriptionId,
[Parameter(Mandatory = $true)][String]$StartTime,
[Parameter(Mandatory = $true)][String]$EndTime,
[Parameter(Mandatory = $false)][ValidateSet('daily', 'hourly')][String]$Granularity = 'daily',
[Parameter(Mandatory = $false)][bool]$ShowDetail = $true,
[Parameter(Mandatory = $false)][ValidateRange(1, 100000)][int]$ResultSize = 1000,
[Parameter(Mandatory = $false)][String]$TenantId = $GlobalCustomerId,
[Parameter(Mandatory = $false)][Parameter(Mandatory = $false, ParameterSetName = 'next')][string]$SaToken = $GlobalToken
)
_testTokenContext($SaToken)
_testTenantContext ($TenantId)
if ($ResultSize -ge 1000)
{
$retObject = Get-PCUsage_implementation -SubscriptionId $SubscriptionId -StartTime $startTime -EndTime $endTime -Granularity $granularity -ShowDetail $ShowDetail -ResultSize 1000 -TenantId $TenantId -SaToken $SaToken
$returnItems = $retObject.Items
if ($retObject.Count -ge 1000)
{
do
{
$r = Get-PCUsage_implementation -SaToken $SaToken -ContinuationLink $retObject.links
$retObject = $r
$returnItems += $retObject.Items
}
until(!($r.links.PsObject.Properties.Name -match 'next'))
}
}
else
{
$retObject = Get-PCUsage_implementation -SubscriptionId $SubscriptionId -StartTime $startTime -EndTime $endTime -Granularity $granularity -ShowDetail $ShowDetail -ResultSize $ResultSize -TenantId $TenantId -SaToken $SaToken
$returnItems = $retObject.Items
}
return $returnItems
}
function Get-PCUsage_implementation {
[CmdletBinding()]
param ( [String]$SubscriptionId,
[String]$StartTime,
[String]$EndTime,
[String]$Granularity,
[bool]$ShowDetail,
[int]$ResultSize,
[String]$TenantId,
[string]$SaToken,
$ContinuationLink)
$obj = @()
$urlParts = @("https://api.partnercenter.microsoft.com/v1/")
$headers = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
$headers.Add("Authorization", "Bearer $SaToken")
$headers.Add("MS-PartnerCenter-Application", $ApplicationName)
if ($null -eq $ContinuationLink) {
try {
$s_time = Get-Date $StartTime -Format s
}
catch {
Write-Error "Start time is not in a valid format. Use '01-06-2018 00:00:00' format"
}
try {
$e_time = Get-Date $EndTime -Format s
}
catch {
Write-Error "End time is not in a valid format. Use '01-06-2018 23:59:00' format"
}
$urlParts += "Customers/{0}/Subscriptions/{1}/Utilizations/azure?start_time={2}Z&end_time={3}Z&show_details={4}&granularity={5}&size={6}" -f $TenantId, $SubscriptionId, $s_time, $e_time, $ShowDetail, $granularity, $ResultSize
}
else {
if (Get-Member -InputObject $continuationLink -name "next" -MemberType Properties) {
$urlParts += $continuationLink.next.uri
foreach ($i in $continuationLink.next.headers) {
$headers.Add($i.Key, $i.Value)
}
}
else {
throw "Check the Count or Link properties before trying to retrieve the next set of records"
}
}
$url = -join $urlParts
$response = Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method "GET"
$obj += $response.Substring(1) | ConvertFrom-Json
$properties = @{
'Count' = $obj[0].totalCount;
'Items' = _formatResult -obj $obj -type "UtilizationRecord";
'Links' = $obj[0].Links;
}
$retObject = New-Object –TypeName PSObject –Prop $properties
return $retObject
}
<#
.SYNOPSIS
Returns usage records for the specified tenant.
.DESCRIPTION
The Get-PCSubscriptionMonthlyUsageRecord returns a usage record for the specified tenant
.PARAMETER SaToken
Specified an authentication token you have created with your Partner Center credentials.
.PARAMETER TenantId
Specifies the tenant id for which to return a usage record
.EXAMPLE
Get-PCSubscriptionMonthlyUsageRecord
.NOTES
#>
function Get-PCSubscriptionMonthlyUsageRecord {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)][String]$TenantId = $GlobalCustomerId,
[Parameter(Mandatory = $false)][string]$SaToken = $GlobalToken
)
_testTokenContext($SaToken)
_testTenantContext ($TenantId)
$obj = @()
$url = "https://api.partnercenter.microsoft.com/v1/customers/{0}/subscriptions/usagerecords" -f $TenantId
$headers = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
$headers.Add("Authorization", "Bearer $SaToken")
$headers.Add("MS-PartnerCenter-Application", $ApplicationName)
$response = Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method "GET" #-Debug -Verbose
$obj += $response.Substring(1) | ConvertFrom-Json
return (_formatResult -obj $obj -type "SubscriptionMonthlyUsageRecord")
}
<#
.SYNOPSIS
Returns monthly Azure usage for the specified tenant.
.DESCRIPTION
The Get-PCAzureResourceMonthlyUsageRecord cmdlet returns Azure usage for the specified tenant.
.PARAMETER SaToken
Specifies an authentication token with your Partner Center credentials.
.PARAMETER TenantId
Specifies the tenant used for scoping this cmdlet.
.PARAMETER SubscriptionId
Specifies the subscription id for which to return usage.
.EXAMPLE
Get-PCAzureResourceMonthlyUsageRecord
.NOTES
#>
function Get-PCAzureResourceMonthlyUsageRecord {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)][String]$TenantId = $GlobalCustomerId,
[string]$SubscriptionId,
[Parameter(Mandatory = $false)][string]$SaToken = $GlobalToken
)
_testTokenContext($SaToken)
_testTenantContext ($TenantId)
$obj = @()
$url = "https://api.partnercenter.microsoft.com/v1/customers/{0}/subscriptions/{1}/usagerecords/resources" -f $TenantId, $SubscriptionId
$headers = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
$headers.Add("Authorization", "Bearer $SaToken")
$headers.Add("MS-PartnerCenter-Application", $ApplicationName)
$response = Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method "GET" #-Debug -Verbose
$obj += $response.Substring(1) | ConvertFrom-Json
return (_formatResult -obj $obj -type "AzureResourceMonthlyUsageRecord")
}
<#
.SYNOPSIS
Returns a summary of usage for the specified tenant.
.DESCRIPTION
The Get-PCCustomerUsageSummary cmdlet returns a summary of usage for the specified tenant.
.PARAMETER SaToken
Specifies an authentication token created with your Partner Center credentials.
.PARAMETER TenantId
Specifies the tenant used for scoping this cmdlet.
.EXAMPLE
Get-PCCustomerUsageSummary -TenantId 45916f92-e9c3-4ed2-b8c2-d87aa129905f
Get the usage summary for all subscriptions for the specified tenant id.
.NOTES
#>
function Get-PCCustomerUsageSummary {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)][String]$TenantId = $GlobalCustomerId,
[Parameter(Mandatory = $false)][string]$SaToken = $GlobalToken
)
_testTokenContext($SaToken)
_testTenantContext ($TenantId)
$obj = @()
$url = "https://api.partnercenter.microsoft.com/v1/customers/{0}/usagesummary" -f $TenantId
$headers = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
$headers.Add("Authorization", "Bearer $SaToken")
$headers.Add("MS-PartnerCenter-Application", $ApplicationName)
$response = Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method "GET" #-Debug -Verbose
$obj += $response.Substring(1) | ConvertFrom-Json
return (_formatResult -obj $obj -type "CustomerUsageSummary")
}
<#
.SYNOPSIS
Returns a cost summary for the specified billing period.
.DESCRIPTION
The Get-PCCustomerServiceCostSummary returns a cost summary for the specified billing period
.PARAMETER SaToken
Specifies an authentication token created with your Partner Center credentials.
.PARAMETER TenantId
Specifies the tenant used for scoping this cmdlet.
.PARAMETER BillingPeriod
Specifies the billing period. The only valid value is MostRecent. Current and None may be added in the future.
.EXAMPLE
Get-PCCustomerServiceCostSummary
.NOTES
#>
function Get-PCCustomerServiceCostSummary {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][ValidateSet("MostRecent")][String]$BillingPeriod, #toAdd "Current","none" as soon as they're supported
[Parameter(Mandatory = $false)][String]$TenantId = $GlobalCustomerId,
[Parameter(Mandatory = $false)][string]$SaToken = $GlobalToken
)
_testTokenContext($SaToken)
_testTenantContext ($TenantId)
$obj = @()
$url = "https://api.partnercenter.microsoft.com/v1/customers/{0}/servicecosts/{1}" -f $TenantId, $BillingPeriod
$headers = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
$headers.Add("Authorization", "Bearer $SaToken")
$headers.Add("MS-PartnerCenter-Application", $ApplicationName)
$response = Invoke-RestMethod -Uri $url -Headers $headers -ContentType "application/json" -Method "GET" #-Debug -Verbose
$obj += $response.Substring(1) | ConvertFrom-Json
return (_formatResult -obj $obj -type "ServiceCostsSummary")
}