Home > Net >  Finding the last date of the vm using azure cli command
Finding the last date of the vm using azure cli command

Time:04-28

I have an account with Azure with different Resource Group and different virtual machine. I would like to know how I can determine which ones are unused. For example check the last date where the virtual machine was started or used by the user using azure cli command.

Please help me out with this...

CodePudding user response:

Easiest would probably to look at the powerstate of a vm. First list all the vm's and then run a query where you filter out those that are deallocated and belong to a specific resource group:

az vm list -d  --query '[?powerState == `VM deallocated` && resourceGroup==`resource_group`]'

For more information on the queries look up 'JMESPath-query' on the Microsoft docs page. Hopefully this helps.

CodePudding user response:

I would like to know how I can determine which ones are unused

Currently, there is no way to do that using cli.

check the last date where the virtual machine was started or used

We can get this information using PowerShell. which follows.

  1. Get data information of deallocated VM using Get-AzVM -VMName xxxx - RgName xxx -Status
# To retrieve the date of VM was Deallocated.
$vmDeallocatedDate = Get-AzVM -VMName <Your VM name> -ResourceGroupName <Your ResourceGroup Name> -Status
$vmDeallocatedDate.Statuses[0].Time
  1. List all the VM's and the timestamp of the action that triggered the Deallocation
Get-AzLog -Status Accepted -DetailedOutput | ?{$_.Authorization.Action -eq "Microsoft.Compute/virtualMachines/deallocate/action"} | fl ResourceId,EventTimestamp

References

  1. Get-AzLog to get VM Deallocate status
  2. Get-AzVM -status to get VM Deallocate status
  • Related