[英]How to get the virtual machine name that my app is running on in 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.