azureazure-virtual-machineazure-billing-api

I need a powershell script/CLI/API that can calculate the running (upkeep) time of all the VMs (Windows & Linux) running on my Azure subscription


So, I have about six VMs (4 Linux & 2 Windows) running in an Azure subscription. I need to know how long the VMs have been running. How do I achieve this using a PowerShell/CLI/API?


Solution

  • Use Get-UsageAggregates in Az.billing Powershell module to get the running time of your VMs in a period of time:

    Connect-AzAccount
    
    $vmsUsage = (Get-UsageAggregates -ReportedStartTime "<start time>" -ReportedEndTime "<endtime>" -ShowDetails $true).UsageAggregations | Where-Object {$_.Properties.MeterCategory -eq  'Virtual Machines'}  
    
    foreach($usage in $vmsUsage){
      echo $usage.Properties
    } 
    

    Result : enter image description here