如何在.Net SDK中创建或列出Azure虚拟机的标记

如何在.Net SDK中创建或列出Azure虚拟机的标记,azure,azure-resource-manager,azure-sdk-.net,Azure,Azure Resource Manager,Azure Sdk .net,我想使用.NET SDK列出并更改Azure虚拟机的标记及其值 请告诉我这条路怎么走 谢谢。因为我手边没有虚拟机,所以我发布了更新资源组标记的代码 首先,请确保Azure AD应用程序已正确设置。您可能会发现此链接对该目的很有用: 接下来,我创建了一个简单的控制台应用程序。您需要做的是在应用程序中获取和Nuget包 在那之后,事情就很简单了。请参阅下面的代码: using System; using System.Linq; using Microsoft.IdentityModel.Clien

我想使用.NET SDK列出并更改Azure虚拟机的标记及其值

请告诉我这条路怎么走


谢谢。

因为我手边没有虚拟机,所以我发布了更新资源组标记的代码

首先,请确保Azure AD应用程序已正确设置。您可能会发现此链接对该目的很有用:

接下来,我创建了一个简单的控制台应用程序。您需要做的是在应用程序中获取和Nuget包

在那之后,事情就很简单了。请参阅下面的代码:

using System;
using System.Linq;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading;
using Microsoft.Azure.Management.Resources;
using Microsoft.Rest;

namespace AzureARMDemo
{
    class Program
    {
        private static string ClientId = "<your-application-client-id>";//This is the PowerShell Client Id
        private static string TenantId = "<tenant-id>";
        private static string LoginEndpoint = "https://login.microsoftonline.com/";
        private static string ServiceManagementApiEndpoint = "https://management.core.windows.net/";
        private static string RedirectUri = "urn:ietf:wg:oauth:2.0:oob";
        private static string SubscriptionId = "<your-azure-subscription-id>";
        private static string AzureResourceManagerEndpoint = "https://management.windows.net";
        private static string ResourceGroupNameToUpdate = "<resource-group-name-to-update>";
        static void Main(string[] args)
        {
            var token = GetAuthorizationHeader();
            var credentials = new TokenCredentials(token);
            var resourceManagerClient = new ResourceManagementClient(new Uri(AzureResourceManagerEndpoint), credentials)
            {
                SubscriptionId = SubscriptionId,
            };
            Console.WriteLine("Listing resource groups. Please wait....");
            Console.WriteLine("----------------------------------------");
            var resourceGroup = resourceManagerClient.ResourceGroups.List().FirstOrDefault(r => r.Name == ResourceGroupNameToUpdate);
            if (resourceGroup != null)
            {
                var tags = resourceGroup.Tags;
                if (!tags.ContainsKey("Key1"))
                {
                    tags.Add("Key1", "Value1");
                }
                else
                {
                    tags["Key1"] = tags["Key1"] + " Updated";
                }
                Console.WriteLine("Updating resource group. Please wait....");
                Console.WriteLine("----------------------------------------");
                resourceManagerClient.ResourceGroups.Patch(ResourceGroupNameToUpdate, resourceGroup);
                Console.WriteLine("Resource group updated.");
                Console.WriteLine("-----------------------");
            }
            //var resourceGroups = resourceManagerClient.ResourceGroups.List();
            //foreach (resourceGroup in resourceGroups)
            //{
            //    Console.WriteLine("Resource Group Name: " + resourceGroup.Name);
            //    Console.WriteLine("Resource Group Id: " + resourceGroup.Id);
            //    Console.WriteLine("Resource Group Location: " + resourceGroup.Location);
            //    Console.WriteLine("----------------------------------------");
            //}
            Console.WriteLine("Press any key to terminate the application");
            Console.ReadLine();
        }

        private static string GetAuthorizationHeader()
        {
            AuthenticationResult result = null;

            var context = new AuthenticationContext(LoginEndpoint + TenantId);

            var thread = new Thread(() =>
            {
                result = context.AcquireToken(
                  ServiceManagementApiEndpoint,
                  ClientId,
                  new Uri(RedirectUri));
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Name = "AquireTokenThread";
            thread.Start();
            thread.Join();

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }

            string token = result.AccessToken;
            return token;
        }
    }
}
使用系统;
使用System.Linq;
使用Microsoft.IdentityModel.Clients.ActiveDirectory;
使用系统线程;
使用Microsoft.Azure.Management.Resources;
使用微软.Rest;
名称空间AzureARMDemo
{
班级计划
{
私有静态字符串ClientId=“;//这是PowerShell客户端Id
私有静态字符串TenantId=“”;
专用静态字符串LoginEndpoint=”https://login.microsoftonline.com/";
专用静态字符串ServiceManagementApident=”https://management.core.windows.net/";
私有静态字符串重定向URI=“urn:ietf:wg:oauth:2.0:oob”;
私有静态字符串SubscriptionId=“”;
专用静态字符串AzureResourceManagerEndpoint=”https://management.windows.net";
私有静态字符串ResourceGroupNameToUpdate=“”;
静态void Main(字符串[]参数)
{
var token=GetAuthorizationHeader();
var凭证=新的令牌凭证(令牌);
var resourceManagerClient=new ResourceManagementClient(新Uri(AzureResourceManagerRendPoint)、凭据)
{
SubscriptionId=SubscriptionId,
};
Console.WriteLine(“正在列出资源组。请稍候…”);
Console.WriteLine(“---------------------------------------------------”;
var resourceGroup=resourceManagerClient.ResourceGroups.List().FirstOrDefault(r=>r.Name==ResourceGroupNameToUpdate);
if(resourceGroup!=null)
{
var tags=resourceGroup.tags;
如果(!tags.ContainsKey(“Key1”))
{
添加标签(“键1”、“值1”);
}
其他的
{
标记[“键1”]=标记[“键1”]+“已更新”;
}
Console.WriteLine(“正在更新资源组。请稍候…”);
Console.WriteLine(“---------------------------------------------------”;
resourceManagerClient.ResourceGroups.Patch(ResourceGroupNameToUpdate,resourceGroup);
控制台.WriteLine(“资源组已更新”);
Console.WriteLine(“--------------------------”);
}
//var resourceGroups=resourceManagerClient.resourceGroups.List();
//foreach(资源组中的资源组)
//{
//Console.WriteLine(“资源组名称:”+resourceGroup.Name);
//Console.WriteLine(“资源组Id:+resourceGroup.Id”);
//Console.WriteLine(“资源组位置:+resourceGroup.Location”);
//Console.WriteLine(“---------------------------------------------------”;
//}
Console.WriteLine(“按任意键终止应用程序”);
Console.ReadLine();
}
私有静态字符串GetAuthorizationHeader()
{
AuthenticationResult=null;
var context=新的AuthenticationContext(LoginEndpoint+TenantId);
变量线程=新线程(()=>
{
结果=context.AcquireToken(
ServiceManagementApident,
ClientId,
新Uri(重定向Uri));
});
SetApartmentState(ApartmentState.STA);
thread.Name=“AquireTokenThread”;
thread.Start();
thread.Join();
如果(结果==null)
{
抛出新的InvalidOperationException(“未能获取JWT令牌”);
}
字符串令牌=result.AccessToken;
返回令牌;
}
}
}

请告诉我们您到目前为止所做的工作以及面临的问题。嗨,高拉夫。谢谢你的回复。我刚刚开始研究dll Microsoft.Azure.Management.Resources以及MSDN中提到的它的类。但是我没有找到任何有用的文档来指导我验证和设置请求,以便为特定VM或资源组列出和创建标记。非常感谢您的帮助。@GauravMantri您能在这方面帮助我吗?如果我共享代码来更新资源组的标记,会有效吗?不幸的是,我现在还没有一个虚拟机,我可以在那里尝试代码。请让我知道。是的,当然。您是否使用过RESTAPI或.NET SDK?您好,Gaurav。谢谢你的回答,非常有用。但是,它会将标记更新到整个资源组,但我们如何才能实现将标记更新到特定虚拟机。是否有任何方法可以仅将标记更新到特定虚拟机?当然,您只能更新特定虚拟机的标记。我选择资源组作为示例,以演示如何使用资源管理器库更新标记,因为我没有虚拟机。我建议您使用该库更好地理解它,然后将其用于您的场景。