问题
I have an account with Azure and different subscriptions 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 a powershell script.
回答1:
PowerShell Method
Within PowerShell you can use the following command to query the VM API.
You want to look at the Provisioning Status and Time, as well as the Running Status:
Connect-AzureRmAccount
Get-AzureRmVm | Get-AzureRmVm -Status | select ResourceGroupName, Name, @{n="Provisioned Time"; e={$_.Statuses[0].Time}}, @{n="Provisioned Status"; e={$_.Statuses[0].DisplayStatus}}, @{n="Running Status"; e={$_.Statuses[1].DisplayStatus}}
This will give the following output
ResourceGroupName : RG-Name
Name : VM-Name
Provisioned Time : 27/06/2018 19:06:39
Provisioned Status : Provisioning succeeded
Running Status : VM deallocated
ResourceGroupName : RG-Name1
Name : VM-Name1
Provisioned Time : 27/06/2018 19:06:39
Provisioned Status : Provisioning succeeded
Running Status : VM running
REST API Method
Outside of PowerShell you can do this relatively easily using a couple of API calls:
Use the VM API to get a listing of all of your VMs
https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/listall
Then call the Instance View API to retreive the last known Instance Status of the VM
https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/instanceview#instanceviewstatus
This will give you the same set of statuses as in the PowerShell method above. The InstanceViewStatus that contains the Status and the Time of the last state of the machine.
Here is the sample JSON of the status part of the returned response for one of my VMs:
"statuses": [
{
"code": "ProvisioningState/succeeded",
"level": "Info",
"displayStatus": "Provisioning succeeded",
"time": "2017-06-15T13:59:26.8578303+00:00"
},
{
"code": "PowerState/running",
"level": "Info",
"displayStatus": "VM running"
}
]
回答2:
As far as I know, you can check the VM status through the resource group one by one. Or all the VM status in one subscription. But not all the vm belongs to you. So I suggest you check through the resource group one by one which belongs to you. And the PowerShell script like below:
# connect to Azure with your account
Connect-AzureRmAccount
# get all the subscriptions of yours in this account
Get-AzureRmSubscription
# select which subscription you want to check
Select-AzureRmSubscription -SubscriptionId yourSubscriptionId
# get the power status of the vm in one resource group
Get-AzureRmVM -ResourceGroupName yourResourceGroupName -Status
The output like this:
For more details, see Get-AzureRmVM.
回答3:
Run query in Azure Resource Graph Explorer:
Resources
| project name, location,
PowerState=tostring(properties.extended.instanceView.powerState.code), type
| where type =~ 'Microsoft.Compute/virtualMachines'
| order by name desc
来源:https://stackoverflow.com/questions/52481983/determine-virtual-machine-status-and-activity-in-microsoft-azure