question

olegarr avatar image
0 Votes"
olegarr asked ·

Azuer VM cost via PowerShell

Hello:

I'd like to be able to find VM cost for some period via PowerShell script.
Is it possible?

I thought I should be able to get it via " Get-AzConsumptionUsageDetail", but it gives me number(s) not even close to what I see in portal (6x or even 10x times difference).

Here is my script:

$Consumption = Get-AzConsumptionUsageDetail -ResourceGroup $RGName -StartDate $StartDay -EndDate $EndDay -InstanceName $VMName
$Costs = $Consumption.UsageQuantity
foreach ($Cost in $Costs) { $CostTotal += $Cost}

$CostTotal is my final number.

What am I doing wrong?
Is it possible to get VM cost for some (past) period via PowerShell script?

Thank you!

azure-virtual-machines
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

msrini-MSFT avatar image
3 Votes"
msrini-MSFT answered ·

Hi,

Your PS script is perfect. But you made one mistake. You have calculated the UseageQunatity instead of the Pretax Cost.

here is the script which will give you the exact cost :

 $costTotal=0
 $RGName ="RGNAME" 
 $StartDay="2019-12-01"
 $EndDay="2019-12-06"
 $VMName="vm1"
 $Consumption = Get-AzConsumptionUsageDetail -ResourceGroup $RGName -StartDate 
 $StartDay -EndDate $EndDay -InstanceName $VMName 
    
 $Costs = $Consumption.PretaxCost
    
 foreach ($Cost in $Costs) { $CostTotal += $Cost}
    
 $CostTotal


· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@msrini-MSFT,

Thank you very very much!

Now it works as expected.

Thanks again, it really helped!!!

1 Vote 1 ·

@msrini-MSFT,


Can you clarify one more thing, please?


Do I understand correctly that $Consumption.PretaxCost (where $Consumption = Get-AzConsumptionUsageDetail -ResourceGroup $RGName -StartDate $StartDay -EndDate $EndDay -InstanceName $VMName ) is the ACTUAL cost for the VM (Compute - CPU/RAM) that Microsoft would charge and storage cost is NOT included? What if my company has a discount from MS?


Thank you, olegarr


0 Votes 0 ·