PowerShell to get the cost of a VM per day

I wanted to get the cost of a VM each day. I came up with the following code. Just the VM cost, not the ingress (DataTrIn) or egress (DataTrOut). I hope it helps someone!

$results=Get-AzConsumptionUsageDetail -StartDate 2019-12-01 -EndDate 2019-12-10 -IncludeAdditionalProperties
$subscriptions="YourSubName"
foreach ($sub in $subscriptions){
$results | `
where {$_.ConsumedService -eq 'Microsoft.Compute'} | `
where {$_.SubscriptionName -eq $sub}| `
Select UsageStart, InstanceName, UsageQuantity,PreTaxCost,@{N="UsageType";E={(ConvertFrom-Json $($_.AdditionalInfo)).UsageType}} | `
where {$_.UsageType -eq 'ComputeHR'} | `
Sort-Object InstanceName,UsageStart | ft 
}


,

One Response to PowerShell to get the cost of a VM per day

  1. Ravi Kiran June 4, 2022 at 11:21 am #

    Thanks for the Script

Leave a Reply