Using Developer Tools to get the Payload to Create Azure Budget Alerts for Action Groups (New-AzConsumptionBudget)
Are you trying to create a budget with PowerShell using New-AzConsumptionBudget on a resource group? You might have tried the code reference on this page New-AzConsumptionBudget (Az.Billing) | Microsoft Learn.
# If you update the sample command and try to execute
New-AzConsumptionBudget -ResourceGroupName budgetThis -Amount 60 -Name PSBudgetRG -Category Cost -StartDate 2022-11-01 -EndDate 2024-11-01 -TimeGrain Monthly
If you’re not using a subscription in an EA, you’ll get this error New-AzConsumptionBudget: Operation returned an invalid status code 'BadRequest'
Running get-error doesn’t provide much in the way of help to resolve. After further digging you’ll find the PowerShell reference page, you’ll probably find that Microsoft docs states you have to be an “EA customer, you can create and edit budgets programmatically using the Azure PowerShell module. However, we recommend that you use REST APIs to create and edit budgets because CLI commands might not support the latest version of the APIs."
If you are using EA subscription it still doesn’t create the alerts and the PowerShell doco doesn’t seem easy to use to create these notifications either.
Hopefully, you found this script SetConsumptionBudget.ps1 Create a Consumption Budget for ResourceGroups in Azure (github.com). It is a complete script and is helpful. This is a good implementation of the content documented here. Budgets - Create Or Update - REST API (Azure Consumption) | Microsoft Learn.
What if you couldn’t find a pre-created script what would you do? Or if you wanted a simplified call rather than the long doc example, in this case, I wanted to leverage using an Action Group.
Let’s create a budget through the Azure portal called “CreatedWithUI” and use the browser developer tools (F12) to capture the traffic requests to the Azure API.
After you click “Create” you’ll see traffic and see the request payload
You can copy the properties section, you just need to add the properties name back in.
With some changes to the syntax object, we can create a script to create the budget alerting an Action Group.
# Converted Syntax for use in script
$bodyObject = @{
"properties"= @{
"timePeriod" = @{
"startDate"= "2022-10-01T00:00:00Z"
"endDate"= "2024-09-30T00:00:00Z"
}
"timeGrain" = "Monthly"
"amount"= 62
"category"= "Cost"
"notifications" =@{
"forecasted_GreaterThan_90_Percent"= @{
"enabled" = $true
"operator"= "GreaterThan"
"threshold"= 90
"contactGroups"= @("/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/cloudmodernization-rg/providers/microsoft.insights/actionGroups/BudgetAlerts")
"thresholdType"= "Forecasted"
}
}
}
}
The last piece of data we need is the Action Group, which still works in PowerShell
# Parameters
$ActionGroupRGName = "cloudmodernization-rg"
$ActionGroupEmails = @("admin@yourorg.com")
$ActionGroupName = "BudgetAlerts"
# Get existing action Group ID
$ActionGroupId = (Get-AzActionGroup -resourcegroup cloudmodernization-rg -Name BudgetAlerts).id
# Create New Action Group
$AlertEmail = New-AzActionGroupReceiver -EmailAddress $ActionGroupEmail -Name ForcastBudgetAlert
$ActionGroupId = (Set-AzActionGroup -ResourceGroupName ($cb.ResourceGroup) -Name $ActionGroupName -ShortName $ActionGroupName -Receiver $AlertEmail).Id
Finally, if you pull all this together calling Invoke-RestMethod it would like something like
# Parameters
$subscriptionId = "00000000-1111-2222-3333-444444444444"
$resourceGroupName = "cloudmodernization-rg"
$ActionGroupName = "BudgetAlerts"
$budgetName = "$resourceGroupName-cb"
# Get existing action Group ID
$ActionGroupId = (Get-AzActionGroup -resourcegroup $resourceGroupName -Name $ActionGroupName ).id
$bodyObject = @{
"properties"= @{
"timePeriod" = @{
"startDate"= "2022-10-01T00:00:00Z"
"endDate"= "2024-09-30T00:00:00Z"
}
"timeGrain" = "Monthly"
"amount"= 62
"category"= "Cost"
"notifications" = @{
"forecasted_GreaterThan_90_Percent"= @{
"enabled" = $true
"operator"= "GreaterThan"
"threshold"= 90
"contactGroups"= @($ActionGroupId)
"thresholdType"= "Forecasted"
}
}
}
"filter" =@{}
}
$bearerToken=(Get-AzAccessToken).token
$headers = @{'Authorization' = "Bearer $bearerToken"}
$RequestBody = $bodyObject | convertto-json -Depth 15
$URI = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Consumption/budgets/$budgetName"+"?api-version=2021-10-01"
Invoke-RestMethod -Method Put -ContentType "application/json; charset=utf-8" -Uri $URI -Headers $headers -Body $RequestBody
Here are the three budgets $60, $61, and $62 created through this article.
This was aimed at showing you how to get to the payload submitted to the Azure Portal to solve a problem using PowerShell and how to revert to using a direct Azure API call.
Topic Search
-
Securing TLS in WAC (Windows Admin Center) https://t.co/klDc7J7R4G
Posts by Date
- March 2025 1
- February 2025 1
- October 2024 1
- August 2024 1
- July 2024 1
- October 2023 1
- September 2023 1
- August 2023 3
- July 2023 1
- June 2023 2
- May 2023 1
- February 2023 3
- January 2023 1
- December 2022 1
- November 2022 3
- October 2022 7
- September 2022 2
- August 2022 4
- July 2022 1
- February 2022 2
- January 2022 1
- October 2021 1
- June 2021 2
- February 2021 1
- December 2020 2
- November 2020 2
- October 2020 1
- September 2020 1
- August 2020 1
- June 2020 1
- May 2020 2
- March 2020 1
- January 2020 2
- December 2019 2
- November 2019 1
- October 2019 7
- June 2019 2
- March 2019 2
- February 2019 1
- December 2018 3
- November 2018 1
- October 2018 4
- September 2018 6
- August 2018 1
- June 2018 1
- April 2018 2
- March 2018 1
- February 2018 3
- January 2018 2
- August 2017 5
- June 2017 2
- May 2017 3
- March 2017 4
- February 2017 4
- December 2016 1
- November 2016 3
- October 2016 3
- September 2016 5
- August 2016 11
- July 2016 13