2

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?

Johnbosco
  • 65
  • 1
  • 9

1 Answers1

1

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

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • thank you so much, that was super helpful and I really appreciate. You just made my day. – Johnbosco Jan 11 '20 at 22:26
  • 1
    Hi @Johnbosco ,glad to know that your issue has been solved, so could you pls click on the check mark beside the answer to toggle it from greyed out to filled in to mark this answer so that it will help others who has similar issues and it will be an award for me : ) have a nice day – Stanley Gong Jan 12 '20 at 09:32