Cost Anomaly Detection setup through powershell script

Guha, Satyaki 20 Reputation points
2025-08-14T13:50:10.2+00:00

Hi I am trying to setup cost anomaly alerts for all subscriptions under my management group through a powershell script. When I tested for one subscription, I got a status 200 code as the response, but when I checked from the console the cost alert rule was still not created.

Can anyone please help me whats going on since there are a lot of subscriptions, I want to automate this through a script. I can provide the script if needed.

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
{count} votes

Accepted answer
  1. Rahul Jorrigala 4,320 Reputation points Microsoft External Staff Moderator
    2025-08-14T19:46:20.64+00:00

    Hello Guha, Satyaki,

    The issue likely due to incorrect URL construction or handling of the api-version parameter. The api-version parameter must appear after the question mark (?) in the URL as a query string. If it's incorrectly placed or omitted, as shown below, it results in a 400 Bad Request error.

    User's image

    User's image

    I tested the scheduled action creation and update using Postman, and it worked as expected.

    User's image

     https://management.azure.com/subscriptions/XXXXXXX/providers/Microsoft.CostManagement/scheduledActions/test-rule?api-version=2025-03-01
    
    
    {
      "properties": {
        "displayName": "Weekly anomaly by resource group",  
        "scope": "subscriptions/XXXXXXXXX",  
        "status": "Enabled",  
        "viewId": "/subscriptions/XXXXXXX/providers/Microsoft.CostManagement/views/ms:DailyAnomalyByResourceGroup",  
        "schedule": {
          "frequency": "Weekly",  
          "hourOfDay": 12,                // Hour of day the alert should run
          "daysOfWeek": [1],              // Set to Monday (1) to run weekly on Monday
          "weeksOfMonth": null,           // No specific weeks of the month
          "dayOfMonth": 0,                // Day of month, not needed for weekly
          "startDate": "2025-08-14T17:25:41.276Z",  // Start date for the scheduled action
          "endDate": "2026-08-14T17:25:41.276Z"     // End date for the scheduled action
        },
        "notification": {
          "subject": "Cost anomaly detected in subscription",  // Notification subject
          "message": "",  // Message body, if needed
          "to": ["******@gmail.com"],  // Email recipient
          "language": "en",  // Language for the notification
          "regionalFormat": "en"  // Regional format for the notification
        },
        "notificationEmail": "******@gmail.com"  // Notification email
      }
    }
    
    

    User's image

    Important: Ensure that the scope specified in the JSON payload matches the scope selected in the Azure Portal.

    Please let me know if you face any challenge here, I can help you to resolve this issue further

    Please do not forget to "Accept the answer” and “upvote it” wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.