Skip to content

Instantly share code, notes, and snippets.

@vukasinterzic
Created March 12, 2023 18:30
Show Gist options
  • Save vukasinterzic/4877a9b4d0027a2bd11f4ea67508acc1 to your computer and use it in GitHub Desktop.
Save vukasinterzic/4877a9b4d0027a2bd11f4ea67508acc1 to your computer and use it in GitHub Desktop.
PowerShell script to retrieve Resource Group cost.
function Get-ResourceGroupCost {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $false)]
[int]$DaysBack = 30
)
# Set the time range for the cost data
$startDate = (Get-Date).AddDays(-$DaysBack)
$endDate = (Get-Date)
# Retrieve the cost data for the resource group
$costs = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate -ResourceGroup $ResourceGroupName
# Display the cost data
Write-Host "Cost information for resource group '$ResourceGroupName':"
$TotalCost = @()
$sum = 0
$costs | % {$sum += $_.PretaxCost}
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Start Date" -Value $startDate.ToString("yyyy-MM-dd")
$obj | Add-Member -MemberType NoteProperty -Name "Resource Group Name" -Value $ResourceGroupName
$obj | Add-Member -MemberType NoteProperty -Name "Total Cost (pretax)" -Value $sum
$obj | Add-Member -MemberType NoteProperty -Name "Currency" -Value ($costs.Currency)[0]
$TotalCost += $obj
return $TotalCost
}
#Example:
#Get-ResourceGroupCost -ResourceGroupName "RG-AzureIsFun" -DaysBack "30"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment