简体   繁体   中英

Azure Cost Alert using Powershell

When i create a new budget using powershell and try to set alert using the same command, it sets but not displaying in cost alerts section and also the amount not seen in alert conditions.

Below is the command:

Set-AzConsumptionBudget -Amount $Amount -Name $BudgetName `
    -ResourceGroupName $RGName -Category Cost -StartDate $StartDate `
    -EndDate $EndDate -ContactEmail $EmailId -NotificationKey $Key `
    -NotificationThreshold 0.8 -NotificationEnabled -TimeGrain Monthly `
    -ContactGroup $ActionGroupId

Below is the image which shows the amount field is not populating:

在此处输入图像描述

I can see it in budget section but not in cost alert section:

在此处输入图像描述

Be aware:

  • A Budget must always start at the 1st of a Month
  • CMDlets are buggy. Better you are using the GraphAPI

Here some examples to use Powershell

$StartofMonth = (Get-Date -Format yyyy-MM).ToString()
$EndOfBudget = (Get-Date).AddMonths(+$MonthUntilNuke) | Get-Date -UFormat "%Y-%m-%d"

$RestBody = @{
    properties = @{
        "category"      = "Cost";
        "amount"        = $budgetAmount;
        "unit"          = "EUR";
        "timeGrain"     = "Annually";
        "timePeriod"    = @{
            "startDate" = "$($StartofMonth)-01T00:00:00Z";
            "endDate"   = "$($EndOfBudget)T00:00:00Z"
        };
        "notifications" = @{
            "AlertAt95Percent"  = @{
                "enabled"       = "true";
                "operator"      = "GreaterThan";
                "threshold"     = 95;
                "thresholdType" = "Actual";
                "contactEmails" = @(
                    "$UPN",
                    "$AdditionalContact"
                )
            };
            "AlertAt100Percent" = @{
                "enabled"       = "true";
                "operator"      = "EqualTo";
                "threshold"     = 100;
                "thresholdType" = "Actual";
                "contactGroups" = @(
                    "/subscriptions/xxxx79c5/resourceGroups/xxxxnt/providers/microsoft.insights/actiongroups/DisableSubscription"
                )
            }
        }
    }
}

$ApiVersion = "api-version=2019-10-01"
$restURI = "https://management.azure.com/subscriptions/$($Subscription.Id)/providers/Microsoft.Consumption/budgets/myBudget?$ApiVersion"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM