Introduction
The Solution
- Weekly Cost Reports: Generates HTML reports every Monday morning, comparing service costs between weeks
- 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.
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.