Search code examples
azurepowershellazure-powershell

Getting Usage Data + Charges Export of Azure Costs and Storing Information Inside a Azure Container


I am trying to create an export of my Azure Usages and Charges into a CSV by setting the scope to my Billing Account ID in a PowerShell script. I've ran the code and it works, but when I do a cost generation in another PowerShell script it seems like the command isn't getting all of the charges. Checking the file sent to the Azure Container, I've found that it is roughly 641 MB in size and when I manually download the Usage + Charges.csv from the Azure Portal, the csv is 656 MB in size. That means that there is roughly 15 MB of data missing from the Billing Account Scope, and I was wondering if I have the right idea here by exporting the CSV via PowerShell.

My ultimate goal is to automate the csv export each month so I can email myself the costs in another PowerShell script. I have contacted Microsoft about the issue and will meet with them to discuss the problem, but I was wondering if anyone has done something similar in their own time and if they could automate the Usage + Charges.csv by themselves. The code is posted below and any tips or pointers would help out.

Export Code:

# Log into the Azure Subscription and authenticate the login
Az Login

# Set variables for the command. 
$subscriptionId = "XXXXXXXXXXXXXXXXXXXXXXXXX"
$storageAccountId = "/subscriptions/$subscriptionId/resourceGroups/XXXXXXXXXXXXXXXXXXXXXX/providers/Microsoft.Storage/storageAccounts/XXXXXXXXXXXXXXXXXXXXXXXXXXX"
$containerName = "publicstoragecontainer001"
$exportName = "Usage Data for XXXXXXXXXXXXXXXXX"
$timeFrame = "TheLastBillingMonth"
$type = "Usage"
$folderName = "Total Usage Cost Exports"
$scheduleStatus = "Active"
$reoccurance = "Monthly"

# Get the Usage from the Billing Scope. 
$scope = "/providers/Microsoft.Billing/billingAccounts/XXXXXXXXXXXXXXXXX"

# Time Variables
$current_time = Get-Date
$future_time = $current_time.AddHours(1)
$current_time_str = $current_time.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$future_time_str = $future_time.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")


# Export the usage data of each tenant to the storage account
az costmanagement export create --name $exportName --scope $scope --storage-account-id $storageAccountId --storage-container $containerName --timeframe $timeFrame --type $type --storage-directory $folderName --schedule-status $scheduleStatus --recurrence $reoccurance --recurrence-period from=$current_time_str to=$future_time_str

Solution

  • I found the issue.

    The -type parameter needs to be set to ActualCost instead of Usage. if you set it to ActualCost, then the command will acquire both the Usage + Charges for the export csv instead of just the usage data. Changing the Parameter gets you the results you need.

    Thank you to everyone who contributed!

    Log into the Azure Subscription and authenticate the login

    Az Login

    # Set variables for the command. 
    $subscriptionId = "XXXXXXXXXXXXXXXXXXXXXXXXX"
    $storageAccountId = "/subscriptions/$subscriptionId/resourceGroups/XXXXXXXXXXXXXXXXXXXXXX/providers/Microsoft.Storage/storageAccounts/XXXXXXXXXXXXXXXXXXXXXXXXXXX"
    $containerName = "publicstoragecontainer001"
    $exportName = "Usage Data for XXXXXXXXXXXXXXXXX"
    $timeFrame = "TheLastBillingMonth"
    $type = "ActualCost"
    $folderName = "Total Usage Cost Exports"
    $scheduleStatus = "Active"
    $reoccurance = "Monthly"
    
    # Get the Usage from the Billing Scope. 
    $scope = "/providers/Microsoft.Billing/billingAccounts/XXXXXXXXXXXXXXXXX"
    
    # Time Variables
    $current_time = Get-Date
    $future_time = $current_time.AddHours(1)
    $current_time_str = $current_time.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
    $future_time_str = $future_time.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
    
    
    # Export the usage data of each tenant to the storage account
    az costmanagement export create --name $exportName --scope $scope --storage-account-id $storageAccountId --storage-container $containerName --timeframe $timeFrame --type $type --storage-directory $folderName --schedule-status $scheduleStatus --recurrence $reoccurance --recurrence-period from=$current_time_str to=$future_time_str