Mastering Azure Costs: Building Automated Weekly Reports with PowerShell

Introduction

After spending time developing cost analysis automation for Azure subscriptions, I’ve noticed some interesting behaviors with the Azure Cost Management API. In this post, I’ll share my experiences and solutions for building reliable cost reporting automation.
 
If you’ve ever tried to automate Azure cost reporting using Invoke-AzCostManagementQuery you know it’s not straightforward. The cmdlet’s complexity and sparse documentation can make what seems like a simple task into a challenging endeavor. After spending considerable time decoding its quirks and experimenting with various approaches, I’ve developed a PowerShell-based solution for automated cost reporting.
 

The Solution

I’ve created two complementary PowerShell scripts that work together to provide comprehensive cost analysis:
  1. Weekly Cost Reports: Generates HTML reports every Monday morning, comparing service costs between weeks
  2. Quarterly Analysis (Experimental): Provides a broader view with 13-week cost trends and detailed service breakdowns

Requirements

Azure subscription with Cost Management Reader right PowerShell Modules:

  • Az.CostManagement
  • Az.Accounts (Minimum 4.0.1 version)
  • Three subscription tags configured (Optional):
    • Environment
    • CostCenter
    • Owner

Why Weekly Reports?

The script runs on Mondays, analyzing the previous completed week versus the week before that. This approach ensures we’re always comparing full weeks of data (Monday-Sunday), making trends and anomalies easier to spot.

Azure Costs Management

Features

  • Week-over-week service cost comparisons
  • Daily cost breakdowns
  • Visual indicators for cost changes
  • Clean HTML output
  • Multi-subscription support
  • Customizable email notifications

Quarterly Analysis 

While weekly reports are great for immediate cost tracking, sometimes you need a broader view of your spending patterns. That’s why I developed a quarterly analysis script that provides a 13-week perspective of your Azure costs. However, during development, I discovered that querying larger time ranges with the Cost Management API can be tricky – sometimes the API returns empty results for weeks where costs definitely exist in the portal. This led me to implement various reliability improvements, making this feature experimental but increasingly robust.

Features

  • 13-week cost trend visualization
  • Service-level cost breakdown across the quarter
  • Interactive charts using Chart.js
  • Detailed HTML reports with cost metrics
  • Export options (CSV/JSON) for further analysis
  • Retry logic for improved reliability

Known Limitations

  • The Cost Management API may occasionally return empty results
  • Data freshness depends on Azure’s data processing pipeline
  • API rate limits may require adjusting query patterns
  • Retry logic adds processing time but improves reliability

Looking Forward

This is just the beginning. Future versions will integrate with Azure Automation for improved scheduling and management capabilities. The plan is to enhance the solution with:

  • Budget tracking and alerts
  • Enhanced multi-subscription support
  • Email notifications via Azure Communication Services
  • Extended cost analytics
  • Enhanced error reporting and diagnostics

Free Examples

To help you get started, I’ve provided working examples that demonstrate how to use Invoke-AzCostManagementQuery. These examples show exactly how to handle timeframes, filtering, and data aggregation with the Cost Management API:

Premium Package

The complete solution, including both weekly and quarterly analysis scripts, is available in the premium download package. This includes:

Conclusion

While the Azure Cost Management API has its challenges, with proper error handling and retry logic, you can build reliable cost reporting automation. The combination of weekly and quarterly analysis provides both immediate and long-term visibility into your Azure spending patterns.

Remember to validate critical reports against the Azure portal, and don’t hesitate to adjust retry parameters based on your specific needs.

Leave a Comment

Contact me

If you’re interested in learning about Mastering Azure Costs: Building Automated Weekly Reports with PowerShell. I can help you understand how this solution can benefit your organization and provide a customized solution tailored to your specific needs.

Table of Contents