-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget-costofretiringresources.ps1
258 lines (214 loc) · 10.3 KB
/
get-costofretiringresources.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
# v1.1.3
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, HelpMessage = "Specify the Service Retirement CSV File")]
[string]$ResourceIdFile = $null,
[Parameter(Mandatory = $true, HelpMessage = "Billing period, example: 202404")]
[string]$billingPeriod = $null,
[Parameter(Mandatory = $false, HelpMessage = "End date with format: YYYY-MM-DD")]
[datetime]$endDate = [datetime]::MaxValue
)
#requires -version 7
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
<#
.SYNOPSIS
Get the cost of retiring resources in a billing period
.PARAMETER resourceId
The resource ID of the resource to get the cost for
.PARAMETER billingPeriodStart
The start of the billing period in UTC format
.PARAMETER billingPeriodEnd
The end of the billing period in UTC format
.PARAMETER token
The token to use for authentication
#>
function Get-ResourceCost($resourceId, $billingPeriodStart, $billingPeriodEnd, $token) {
$subscriptionId = $resourceId.Split("/")[2]
$resourceGroup = $resourceId.Split("/")[4]
$uri = "https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.CostManagement/query?api-version=2023-11-01"
$jsonPayload = @{
type = "ActualCost"
dataSet = @{
granularity = "Monthly"
aggregation = @{
totalCost = @{
name = "Cost"
function = "Sum"
}
}
sorting = @(@{
direction = "ascending"
name = "UsageDate"
})
filter = @{
Dimensions = @{
Name = "ResourceId"
Operator = "In"
Values = @("${resourceId}")
}
}
}
timeframe = "Custom"
timePeriod = @{
from = "${billingPeriodStart}"
to = "${billingPeriodEnd}"
}
}
$jsonPayload = $jsonPayload | ConvertTo-Json -Depth 10
$cost = 0
# send the request. Handle errors 429 (too many requests)
do {
$requestCompleted = $false
$statusCode = 0
$responseHeaders = $null
$response = Invoke-RestMethod -Uri $uri -Method Post -SkipHttpErrorCheck -StatusCodeVariable "statusCode" -ResponseHeadersVariable "responseHeaders" -Body $jsonPayload -Headers @{
'Authorization' = "Bearer $token"
'Content-Type' = 'application/json'
"Client-Type" = "GetCostOfRetiringResources"
}
if ($statusCode -eq 429) {
$qpuRetryAfter = [double]::Parse($responseHeaders['x-ms-ratelimit-microsoft.costmanagement-client-qpu-retry-after'] ?? 0)
$clientRetryAfter = [double]::Parse($responseHeaders['x-ms-ratelimit-microsoft.costmanagement-client-retry-after'] ?? 0)
$tenantRetryAfter = [double]::Parse($responseHeaders['x-ms-ratelimit-microsoft.costmanagement-tenant-retry-after'] ?? 0)
$entityRetryAfter = [double]::Parse($responseHeaders['x-ms-ratelimit-microsoft.costmanagement-entity-retry-after'] ?? 0)
$retryAfterSet = @($qpuRetryAfter, $clientRetryAfter, $tenantRetryAfter, $entityRetryAfter)
$retryAfter = $retryAfterSet | Sort-Object -Descending | Select-Object -First 1 # get the maximum value of the retry-after values
if ($retryAfter -eq 0) {
$retryAfter = 30 # if none of the above is set, assume a delay of 30 seconds
}
write-host -ForegroundColor DarkYellow "wait ${retryAfter}s... " -NoNewline
Start-Sleep -Seconds $retryAfter
}
elseif ($statusCode -ne 200) {
Write-host -ForegroundColor Red "Error: $statusCode"
$requestCompleted = $true
}
else {
if (($null -ne $response.properties.rows) -and ($response.properties.rows.Count -gt 0)) {
$cost = $response.properties.rows[0][0]
write-host -ForegroundColor Yellow ("{0:N5}" -f $cost)
}
else {
write-host -ForegroundColor Green "no data"
}
$requestCompleted = $true
}
} while (-not $requestCompleted)
return $cost
}
### main script ###
$currentDate = Get-Date
# Validate billing period
if ($billingPeriod -notmatch '^\d{6}$') {
Write-Error "Invalid billing period format '$billingPeriod'. Expected format: yyyyMM"
return
}
# convert billing period to a start and end date formatted as UTC strings
$billingPeriodStart = [datetime]::ParseExact($billingPeriod, "yyyyMM", $null).ToString("yyyy-MM-01T00:00:00+00:00")
$billingPeriodEnd = [datetime]::ParseExact($billingPeriod, "yyyyMM", $null).AddMonths(1).AddSeconds(-1).ToString("yyyy-MM-ddT23:59:59+00:00")
# Validate and import CSV file
if (-not (Test-Path $ResourceIdFile)) {
Write-Error "File $ResourceIdFile does not exist"
return
}
$resourceIds = Import-Csv -Path $ResourceIdFile -Delimiter ";"
if ($null -eq $resourceIds) {
Write-Error "Failed to import CSV file or file is empty"
return
}
# convert the retirement date to a datetime object
$resourceIds = $resourceIds | % { $_.'Retirement Date' = ( $_.'Retirement Date' -as [datetime] ) ; $_ }
# filter out resource whose retirement date is before today or after the end date (if specified, otherwise assume [datetime]::MaxValue)
$resourceIds = $resourceIds | Where-Object { $_.'Retirement Date' -ge $currentDate -and $_.'Retirement Date' -le $endDate }
if ($null -eq $resourceIds -or $resourceIds.Count -eq 0) {
if ([datetime]::MaxValue -ne $endDate) {
Write-Error "No resources found in file with future retirement date on or before ${endDate}."
}
else {
Write-Error "No resources found in file with future retirement date."
}
return
}
Write-Host -NoNewline ([datetime]::MaxValue -eq $endDate ? "Resources in file with future retirement date: " : "Resources in file with future retirement date on or before ${endDate}: ")
Write-Host -ForegroundColor Yellow $resourceIds.Count
# if we have ASE in the list, we also need to get the list of all app plans in the ASE
$aseResources = ($resourceIds | Where-Object { $_.'Type' -eq "microsoft.web/hostingenvironments" })
if ($null -ne $aseResources) {
Write-Host "You have $($aseResources.Count) ASE resource(s) in the list. Getting the impacted app service plans used by the ASEs..."
$retiringFeature = $aseResources[0].'Retiring Feature'
$retirementDate = $aseResources[0].'Retirement Date'
$action = $aseResources[0].'Action'
$aseResourcesIds = $aseResources.'Resource Name'
$appPlans = Search-AzGraph -Query @"
resources
| where type =~ 'microsoft.web/serverfarms' and properties.hostingEnvironmentProfile <> ''
| mv-expand ASE = properties.hostingEnvironmentProfile
| project name, ASEid = ASE.id, type, subscriptionId, resourceGroup, location, id, tags
| where ASEid <> ''
"@
$impactedAppPlans = $appPlans | Where-Object { $aseResourcesIds -contains $_.ASEid }
if ($null -eq $impactedAppPlans) {
Write-Host "No impacted app plans found."
}
else {
Write-Host "There are $($impactedAppPlans.Count) impacted app plans."
# add the impacted app plans to the list of resources because we need to get the cost for them as well
foreach ($appPlan in $impactedAppPlans) {
$newresource = [PSCustomObject]@{
'Subscription' = $appPlan.subscriptionId
'Type' = $appPlan.type
'Retiring Feature' = $retiringFeature
'Retirement Date' = $retirementDate
'Resource Group' = $appPlan.resourceGroup
'Location' = $appPlan.location
'Resource Name' = $appPlan.id
'Tags' = $appPlan.tags
'Action' = $action
}
Write-Host "Adding app plan $($appPlan.Name) to the list of resources to get cost for..."
$resourceIds += $newresource
}
}
}
# get a token
$token = (Get-AzAccessToken -ResourceUrl "https://management.azure.com/").Token
# sort by retirement date (soonest first) then by retiring feature
$resourceIds = $resourceIds | Sort-Object -Property @{Expression = "Retirement Date"; Ascending = $true }, @{Expression = "Retiring Feature"; Ascending = $true }
$resourceCount = 0
$totalCost = 0
# prepare a hashtable to store cumulative costs for each resource type
$totalCostByResourceType = @{}
# iterate over the resources in the list
Write-Host
foreach ($resourceLine in $resourceIds) {
$resourceCount++
$resourceId = $resourceLine.'Resource Name'
$resourceType = $resourceLine.'Type'
# extract the resource name from the resource ID - it's the last part of the resource ID from item #7 onwards. It can have one or more slashes in it.
$resourceName = $resourceid.Split("/")[8..($resourceid.Split("/").Count - 1)] -join "/"
$retirementDate = $resourceLine.'Retirement Date'
$retiringFeature = $resourceLine.'Retiring Feature'
$subscriptionId = $resourceId.Split("/")[2]
$subscriptionName = $(Get-Azsubscription -SubscriptionId $subscriptionId).Name
write-host -NoNewline "[${resourceCount}] "
write-host -NoNewline -ForegroundColor Cyan "${retirementDate} "
write-host -NoNewLine "${subscriptionName}: "
write-host -NoNewline "${resourceType}, "
write-host -NoNewline -ForegroundColor Yellow "${retiringFeature}: "
write-host -NoNewLine "${resourceName}: "
# get the cost for the resource in the billing period
$cost = Get-ResourceCost -resourceId $resourceId -billingPeriodStart $billingPeriodStart -billingPeriodEnd $billingPeriodEnd -token $token
$totalCost += $cost
$totalCostByResourceType["${resourceType}, ${retiringFeature}"] += $cost
}
write-host
write-host -NoNewline "Total cost for all resources in billing period ${billingPeriod}: "
write-host -ForegroundColor Yellow ("{0:N5}" -f $totalCost)
# write out the costs by resource type
write-host
write-host "Total costs by resource type:"
foreach ($resourceTypeAndRetiringFeature in $totalCostByResourceType.Keys) {
write-host -NoNewline "${resourceTypeAndRetiringFeature}: "
write-host -ForegroundColor Yellow ("{0:N5}" -f $totalCostByResourceType[$resourceTypeAndRetiringFeature])
}