如何知道Azure中是否停止了虚拟机

时间:2015-11-01 20:39:16

标签: java node.js rest azure azure-virtual-machine

我正在使用Azure Rest API。如何检查虚拟机是否正在运行或已停止?首先,我想使用配置状态,但它没有提供有用的信息

4 个答案:

答案 0 :(得分:2)

尝试以下代码。 PowerState是您需要检查的。

       using (ComputeManagementClient computeClient = new ComputeManagementClient(credentials))
        {
            HostedServiceListResponse services  = await computeClient.HostedServices.ListAsync();
            foreach(HostedServiceListResponse.HostedService service in services.HostedServices)
            {
                DeploymentGetResponse deployment = await computeClient.Deployments.GetBySlotAsync(service.ServiceName, DeploymentSlot.Production);

                var powerState = deployment.RoleInstances[0].PowerState;

            }
        }

答案 1 :(得分:1)

您可以使用虚拟机REST API Get information about a virtual machine进行Azure资源管理,请参阅https://msdn.microsoft.com/en-us/Library/azure/mt163682.aspx

在REST Get information about the instance view of a virtual machine的响应中,您可以在参考页面底部的json属性displayStatus数组中找到第二个元素的属性"statuses",请参阅下图:

enter image description here

答案 2 :(得分:0)

您可以在门户网站上查看VM的状态。如果您想使用powershell- Get-azurevm -servicename "svcname" -vmname "vmname"

也会给你vm的状态。

答案 3 :(得分:0)

使用Nodejs:

var msRestAzure = require('ms-rest-azure');
var azArmCompute = require('azure-arm-compute');
const clientId = "xxxx";
const appSecret = "xxxx"; 
const tenantId = "xxxx";
const subscriptionId = "xxxx";

let credential = await msRestAzure.loginWithServicePrincipalSecret(clientId, appSecret, tenantId);
computeClient = new azArmCompute.ComputeManagementClient(credential, subscriptionId);
var getVMStatus = async function(resourceGroup, vmName){
    try {
        await computeClient.virtualMachines.get(resourceGroup, vmName, {expand: 'instanceView'},function(err, result){
            context.log("VM Status:" + result.instanceView.statuses[1].displayStatus);
        });
    } catch (error) {
        context.log("Error has occurred while trying to get VM info");
        throw error;
    }
}

getVMStatus("Rg_xxxx","Vm_xxxx");

状态为:“虚拟机正在分配”,“虚拟机已重新分配”,“虚拟机正在停止”,“虚拟机已停止”,“虚拟机正在启动”或“虚拟机正在运行” Virtual machines lifecycle and states