azurepowershellstorage

How to Retrieve Amount of Storage Backed up (in GB) by Azure?


I am tasked with retrieving information about a given Azure Backup Vault, and whether the backup(s) are successful or unsuccessful. If successful, I must record how much data has been backed up through a PowerShell function.

I was able to find a command that gave me the backup date(s) and the progress of each backup; however, I was not able to find a command or a set of commands that told me how much data has been backed up for each.

This is the function I have so far, it only gives me the list of backup jobs:

Function Get-AzBackupInfo
{
  $nameList = (Get-AzRecoveryServicesVault).Name
  $resourceGroupList = (Get-AzRecoveryServicesVault).ResourceGroupName
  $hostname = hostname

  for ($i = 0; $i -lt $nameList.Length; $i++)
  {
    if ($resourceGroupList[$i] -match $hostname)
    {
     $var = Get-AzRecoveryServicesVault -Name $nameList[$i] -ResourceGroupName $resourceGroupList[$i]
     $jobList = Get-AzRecoveryServicesBackupJob -VaultId $var.ID

    }
  }
   $jobList
}

Are there any modifications / additions I can make to this function that will give me the amounts of data (in GB) that have been backed up?


Solution

  • How to Retrieve Amount of Storage Backed up (in GB) by Azure?

    You can use the below PowerShell script which fetches the usage of Azure recovery vault.

    Script:

    $subscriptionId = "xxxx"
    $resourceGroupName = "xx"
    $vaultName = "vxxxd"
    $apiVersion = "2024-10-01"
    
    
    $uri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.RecoveryServices/vaults/$vaultName/usages?api-version=$apiVersion"
    
    $accessToken = (Get-AzAccessToken -ResourceUrl "https://management.azure.com").Token
    
    # Call the API
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers @{ Authorization = "Bearer $accessToken" }
    
    # Convert Byte values to GB
    $convertedResponse = $response.value | ForEach-Object {
        if ($_.unit -eq "Bytes") {
            $_.currentValue = "{0:N2} GB" -f ($_.currentValue / 1GB)
        }
        $_
    }
    
    # Display the results
    $convertedResponse | Format-Table -AutoSize
    

    The above script retrieves the usage details of an Azure Recovery Services vault by calling the Azure REST API. It authenticates using Get-AzAccessToken, fetches the usage details via Invoke-RestMethod, and converts byte values to GB for readability before displaying them in a table.

    Output: enter image description here

    Reference: