简体   繁体   English

Azure资源管理API没有显示虚拟机状态?

[英]Azure resource management API not showing virtual machine state?

So I've been poking around at read-only api access into azure with the resource management api. 因此,我一直在使用只读API访问资源管理API,使其成为Azure。 Right now I'm focusing on Virtual Machines. 现在,我专注于虚拟机。 I've been using this pre-release package with TokenCredentials: 我一直在使用此预发行包和TokenCredentials:

https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.1-prerelease https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.1-prerelease

I get a bunch of rich info about my vms but I'm missing a pretty criticle piece of data and that's whether the vm is on or off. 我得到了很多有关我的虚拟机的丰富信息,但是我错过了一些相当有争议的数据,那就是虚拟机是打开还是关闭。 I've found a couple of meta data properties like InstanceView and Plan to be null when I expected them to be populated. 当我希望填充一些元数据属性时,例如InstanceView和Plan,它们为null。 It may because of how I launched my vms, it may be a unfinished or buggy new package, I can't tell. 可能是因为我启动了vms的方式,它可能是一个未完成或有错误的新软件包,我无法确定。 I was thinking InstanceViews statues would tell me what state the vm is in. 我当时以为InstanceViews雕像会告诉我虚拟机处于什么状态。

https://msdn.microsoft.com/en-us/library/microsoft.azure.management.compute.models.virtualmachineinstanceview.aspx https://msdn.microsoft.com/zh-CN/library/microsoft.azure.management.compute.models.virtualmachineinstanceview.aspx

So I suppose I have to look elsewhere. 所以我想我必须去别处。 I did find this older stackoverflow question that may be what I'm looking for: 我确实找到了这个较旧的stackoverflow问题,可能是我要寻找的问题:

azure management libraries virtual machine state Azure管理库虚拟机状态

However I'm not sure what dll this GetAzureDeyployment is part of or if it's even TokenCredential compatible. 但是我不确定此GetAzureDeyployment是哪个dll的一部分,或者甚至与TokenCredential兼容。 Anyone know whats up? 有人知道发生了什么吗?

You can use the following c# code to get the power status of your VM. 您可以使用以下c#代码获取VM的电源状态。

using System;
using System.Security;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;

namespace GetVmARM
{
    class Program
    {

        private static String tenantID = "<your tenant id>";
        private static String loginEndpoint = "https://login.windows.net/";
        private static Uri redirectURI = new Uri("urn:ietf:wg:oauth:2.0:oob");
        private static String clientID = "1950a258-227b-4e31-a9cf-717495945fc2";
        private static String subscriptionID = "<your subscription id>";
        private static String resource = "https://management.core.windows.net/";



        static void Main(string[] args)
        {
            var token = GetTokenCloudCredentials();
            var credential = new TokenCredentials(token);
            var computeManagementClient = new ComputeManagementClient(credential);
            computeManagementClient.SubscriptionId = subscriptionID;

            InstanceViewTypes expand = new InstanceViewTypes();

            var vm = computeManagementClient.VirtualMachines.Get("<the resource group name>", "<the VM>", expand);

            System.Console.WriteLine(vm.InstanceView.Statuses[1].Code);
            System.Console.WriteLine("Press ENTER to continue");
            System.Console.ReadLine();
        }

        public static String GetTokenCloudCredentials(string username = null, SecureString password = null)
        {
            String authString = loginEndpoint + tenantID;

            AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);

            var promptBehaviour = PromptBehavior.Auto;

            var userIdentifierType = UserIdentifierType.RequiredDisplayableId;

            var userIdentifier = new UserIdentifier("<your azure account>", userIdentifierType);

            var authenticationResult = authenticationContext.AcquireToken(resource, clientID, redirectURI, promptBehaviour, userIdentifier);

            return authenticationResult.AccessToken;
        }

    }
}

As you can see in this piece of code, I am using InstanceViewTypes which is not available in the document. 如您在这段代码中所看到的,我正在使用InstanceViewTypes,该文档中没有该实例。 This is new in the 13.0.1 pre-release version. 这是13.0.1预发行版本中的新增功能。 But yes, if you adding this to your computeManagementClient.VirtualMachines.Get method, you will be able to get extra information for your VM. 但是可以,如果将其添加到computeManagementClient.VirtualMachines.Get方法中,则将能够获取有关VM的更多信息。

Furthermore, I am using vm.InstanceView.Statuses[1] because vm.InstanceView.Statuses[0] is the ProvisioningState. 此外,由于vm.InstanceView.Statuses [0]是ProvisioningState,所以我正在使用vm.InstanceView.Statuses [1]。 And, I am not sure if the order is always like this, so you may need to loop through the whole status list. 而且,我不确定顺序是否总是这样,因此您可能需要遍历整个状态列表。

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

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