如何知道虚拟机是否在 Azure 中停止
How to Know if Virtual Machine is stopped in Azure
我正在使用 Azure Rest API。如何检查虚拟机是 运行 还是已停止?一开始我想使用配置状态,但它没有提供有用的信息
您可以在门户网站上查看 VM 的状态。如果你想使用 powershell- Get-azurevm -servicename "svcname" -vmname "vmname"
也会给你虚拟机的状态。
试试下面的代码。 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 资源管理,请参考 https://msdn.microsoft.com/en-us/Library/azure/mt163682.aspx。
在 REST API 对 Get information about the instance view of a virtual machine
的响应中,您可以找到 json 属性 [中第二个元素的属性 displayStatus
"statuses"
数组在参考页的底部,见下图:
使用 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");
状态将为:"VM deallocating"、"VM deallocated"、"VM stopping"、"VM stopped"、"VM starting" 或 "VM running"
Virtual machines lifecycle and states
我正在使用 Azure Rest API。如何检查虚拟机是 运行 还是已停止?一开始我想使用配置状态,但它没有提供有用的信息
您可以在门户网站上查看 VM 的状态。如果你想使用 powershell- Get-azurevm -servicename "svcname" -vmname "vmname"
也会给你虚拟机的状态。
试试下面的代码。 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 资源管理,请参考 https://msdn.microsoft.com/en-us/Library/azure/mt163682.aspx。
在 REST API 对 Get information about the instance view of a virtual machine
的响应中,您可以找到 json 属性 [中第二个元素的属性 displayStatus
"statuses"
数组在参考页的底部,见下图:
使用 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");
状态将为:"VM deallocating"、"VM deallocated"、"VM stopping"、"VM stopped"、"VM starting" 或 "VM running" Virtual machines lifecycle and states