确定Microsoft Azure中的虚拟机状态和活动 [英] Determine Virtual Machine status and activity in Microsoft Azure

查看:89
本文介绍了确定Microsoft Azure中的虚拟机状态和活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有Azure帐户,并且具有不同的资源组和不同的虚拟机的不同的订阅.我想知道如何确定哪些未使用.例如,使用Powershell脚本检查用户启动或使用虚拟机的最后日期.

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.

推荐答案

PowerShell方法

在PowerShell中,您可以使用以下命令查询VM API.

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}}

这将给出以下输出

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方法

在PowerShell之外,您可以使用几个API调用相对轻松地做到这一点:

Outside of PowerShell you can do this relatively easily using a couple of API calls:

使用VM API获取所有VM的列表

Use the VM API to get a listing of all of your VMs

https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/listall

然后调用Instance View API检索虚拟机的最新已知实例状态

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

这将为您提供与上述PowerShell方法中相同的状态集. InstanceViewStatus 包含计算机的最后状态的 Status Time .

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.

这是我的其中一个VM的返回响应的状态部分的示例JSON:

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"
    }
  ]

这篇关于确定Microsoft Azure中的虚拟机状态和活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆