How to download or export the budget details from azure portal or using PowerShell which are created in root scope?

260 views Asked by At

I'm trying to download the budgets we have created in root scope in azure, but not able to.

I tried using Get-AzConsumptionBudget but it does not give result for root management group but only for subscription level and resource group level.

Get-AzManagementGroup -GroupId "groupID" | Get-AzConsumptionBudget

I'm getting error like

Get-AzConsumptionBudget : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the 
input and its properties do not match any of the parameters that take pipeline input.
2

There are 2 answers

3
Vasu Rastogi On

To download or export budget details created in the root scope of the Azure portal or using PowerShell, you can follow these steps:

Azure Portal:

Log in to the Azure portal. Navigate to the "Cost Management + Billing" section. In the left sidebar, select "Cost Management" and then "Budgets." Find and select the budget you want to export. Once on the budget details page, look for an "Export" or "Download" option. This may be in the form of a button or an option within the menu.

1
KKI On

First of all, if you want to see the root and the resources in it, you need to enable the "Access management for Azure resources"

  1. Open the Azure portal
  2. Search "Tenant properties"
  3. Turn on the "Access management for Azure resources"
  4. Relogin

Reference and help: Elevate access to manage all Azure subscriptions and management groups

"However, when management groups are enabled for the organization, all subscription costs are rolled-up to the billing account and to the root management group because they're both constrained to a single directory."

"Management groups - Hierarchical containers, used to organize Azure subscriptions. A management group tree can support up to six levels of depth. The limit doesn't include the Root level or the subscription level."

Reference: Understand and work with scopes

So I think that what you want in this context is not achievable.

But! You can use the Get-AzManagementGroup command to query all subscriptions within the root. You can then iterate through all the subscriptions (Set-AzContext) and then query the budgets (Get-AzConsumptionBudget).

You can of course store these in an object on the fly and save them to a csv (or whatever) file at the end.

Something like this:

#Make sure that you have Az module installed
#If you don't have enough permissions, you can just delete or comment out the lines with "#permission" at the end.

#install-module az
    #Or if you don't need all module just the necessary ones run the following commands
#install-module az.billing
#install-module az.accounts
#install-module az.resources
#install-module az.accounts

Connect-AzAccount

$allTenant = Get-AzManagementGroup #permission

$arrayToFile_AllDetails = @()
$arrayToFile_MinimalDetails = @()

foreach($actualTenant in $allTenant){ #permission
    $allSubInTheTenant = Get-AzSubscription -TenantId $actualTenant.TenantId #permission, just delete "-TenantId $actualTenant.TenantId"

    foreach($actualSub in $allSubInTheTenant){
        Set-AzContext -Subscription $actualSub.Id #| out-null #you can add the "| out-null" if you don't want to see when switch between subs
        $budget = Get-AzConsumptionBudget
    
        if($budget -ne $null){
            $minimalObject = [PSCustomObject]@{
                BudgetName = $budget.Name
                Amount = $budget.Amount
                Category = $budget.Category
                TimeGrain = $budget.TimeGrain
            }

            $arrayToFile_MinimalDetails += $minimalObject
            $arrayToFile_AllDetails += $budget
        }    
    }
} #permission

$today = (Get-Date).ToString("MM-dd-yyyy")
$arrayToFile_AllDetails | export-csv -Path "C:\temp\AllBudget-AllDetails-$today.csv"
$arrayToFile_MinimalDetails | export-csv -Path "C:\temp\AllBudget-MinimalDetails-$today.csv"

Side note: make sure that you have c:\temp or replace it with your path.

MinimalDetails csv file:

MinimalDetails csv file

If this is what you were looking for, please accept the answer. If you have any other questions, please feel free to ask.