c# - Azure 资源管理 API 未显示虚拟机状态?

标签 c# api azure

所以我一直在研究使用资源管理 API 对 azure 进行只读 api 访问。现在我主要关注虚拟机。我一直在使用这个预发布包和 TokenCredentials:

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

我获得了关于我的虚拟机的大量丰富信息,但我错过了一个非常关键的数据,那就是虚拟机是否打开或关闭。我发现一些元数据属性(例如 InstanceView 和 Plan)在我期望填充它们时为空。这可能是因为我启动虚拟机的方式,它可能是一个未完成的或有缺陷的新软件包,我无法判断。我在想 InstanceViews 雕像会告诉我虚拟机处于什么状态。

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

所以我想我必须去别处看看。我确实发现了这个旧的 stackoverflow 问题,这可能就是我正在寻找的:

azure management libraries virtual machine state

但是我不确定这个 GetAzureDeyployment 是哪个 dll 的一部分,或者它是否与 TokenCredential 兼容。有谁知道怎么回事吗?

最佳答案

您可以使用以下 C# 代码来获取虚拟机的电源状态。

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;
        }

    }
}

正如您在这段代码中看到的,我使用的是文档中未提供的 InstanceViewTypes。这是 13.0.1 预发行版本中的新增功能。但是,是的,如果您将其添加到computeManagementClient.VirtualMachines.Get 方法中,您将能够获取虚拟机的额外信息。

此外,我使用 vm.InstanceView.Statuses[1],因为 vm.InstanceView.Statuses[0] 是 ProvisioningState。而且,我不确定顺序是否总是这样,因此您可能需要循环遍历整个状态列表。

关于c# - Azure 资源管理 API 未显示虚拟机状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37534790/

相关文章:

c# - 帮助 EnumMemberAttribute?

c# - 如何在 listView OnItemEditing 中获取 DataItem?

c# - .NET/C# 和 Java 的高性能数学库

android - 是否有任何 SMS API 可用于 android 从 android 应用程序发送 SMS?

web-services - REST 数组操作最佳实践

c# - Flickr 'Invalid auth token (98)' 从 Asp.net 应用程序上传视频

R - POST 的 JSON 结构

azure - 在 CI/CD 步骤中运行 python 脚本

azure - 巴西人如何代表他开设 Windows Azure 帐户?

azure - 创建重置 Azure AD 密码功能