Reputation: 21
I am trying to generate a report of usage on a calendar and the missing metric is running/stop VS deallocated time.
Looking at the API documentation there does not seems to be a straight forward way to get this information from neither the vm api or the cost management.
Any suggestions on how to get this metric?
Upvotes: 2
Views: 2478
Reputation: 11008
Powershell. Look for the PowerState status.
Get-AzVM -Status
C#
ResourceId vmResourceId = ResourceId.FromString(vmResourceId);
var authenticated = Azure.Configure().Authenticate(Common.Credentials);
vmInner = await authenticated.WithSubscription(vmResourceId.SubscriptionId).VirtualMachines.Inner.GetAsync(vmResourceId.ResourceGroupName, vmResourceId.Name, InstanceViewTypes.InstanceView);
PowerState = vmInner.InstanceView.Statuses.Where(i => i.Code.Contains("PowerState")).FirstOrDefault()?.DisplayStatus;
API - https://learn.microsoft.com/en-us/rest/api/compute/virtualmachines/instanceview. Look for the statuses array where code = "PowerState/running".
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/instanceView?api-version=2019-07-01
Upvotes: 2