This PowerShell script is designed to calculate the cost of retiring resources in Azure.
You need to be at least a Reader in order to export the data and invoke the Cost Management API used to calculate cost of resources being retired.
- Go to Azure Advisor and open the workbook called Service Retirement (preview):
- Select view Impacted Services:
- Export to Excel:
- Open the file in Excel and export to CSV. Note: the script currently uses ';' as field separator
- Execute the script in a PowerShell 7 session, with the Az module installed and logged to Azure.
.\get-costofretiringresources.ps1 -billingPeriod <YYYYMM> -ResourceIdFile <exported file> [-endDate YYYY-MM-DD]
For example:
.\get-costofretiringresources.ps1 -billingPeriod 202405 -ResourceIdFile .\export_data.csv -endDate 2024-08-31
- The script imports the data, sort them by retirement date and retiring feature, leaving out resources that, in theory, have been already retired..
- For the remaining resources, to be retired in any future date from today (or: from today to -endDate, if specified), it uses the Azure Cost Management API to extract the cost that the resource had in the specified billing period.
- The total costs are then shown at the end of execution.
Cost Management API:
https://learn.microsoft.com/en-us/rest/api/cost-management/query/usage?view=rest-cost-management-2023-11-01&tabs=HTTP
https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.CostManagement/query?api-version=2023-11-01"