繁体   English   中英

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

[英]How to Know if Virtual Machine is stopped in Azure

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

试试下面的代码。 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;

            }
        }

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

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

在此处输入图片说明

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

也会为您提供vm的状态。

使用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");

状态将是:“虚拟机正在分配”,“虚拟机已重新分配”,“虚拟机正在停止”,“虚拟机已停止”,“虚拟机正在启动”或“虚拟机正在运行” 虚拟机的生命周期和状态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM