Azure Windows VM-如何确定它所在的资源组? [英] Azure Windows VM - how can I find out what Resource Group it is in?

查看:44
本文介绍了Azure Windows VM-如何确定它所在的资源组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure Windows VM上运行了C#代码.有没有办法让我找出此VM所在的资源组?

I have C# code running on an Azure Windows VM. Is there a way for me to find out what Resource Group this VM is in?

VM已使用Azure资源管理API部署(新的,不是经典的)

VM has been deployed with Azure Resource Management API (new, not classic)

推荐答案

以下内容将保证能够在不同资源组中区分具有相同名称的vm:

The following will guarantee the ability to distinguish between vm's with the same name across different resource groups:

从C#代码中找到vmId(涉及在以下链接中运行命令之一,或者可能使用Azure SDK:

From your C# code, find the vmId (involves running one of the commands at the following link or possibly using an Azure SDK: https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/). If using a Linux VM, be sure to take into account the different endian-ness, otherwise the vmId will not match.

一旦有了vmId,就可以使用CLI或Powershell(或可能的Azure SDK)列出订阅中的所有VM,然后在列表中进行搜索以查找哪个VM具有从计算机中获得的vmId..然后,您应该能够从该json的json的"id"字段中解析出该资源组的名称(正如Gaurav所提到的,它是其中包含资源组的字符串).例如,请尝试以下操作:

Once you have the vmId, you can use either CLI or Powershell (or potentially an Azure SDK) to list all of the VM's in the subscription, then search through the list to find which VM has the vmId you got from the machine. Then you should be able to parse out the resource group name from the "id" field of the json for that VM (which, as Gaurav mentioned, is a string with the resource group in it). For an example, try the following:

azure vm list --json -vv

此命令将向您显示用于发出请求的URL和响应正文.在此正文中,您将找到"vmId"和"id"字段.例如,它发送的请求之一是:

This command will show you the url's it is using to make the requests and the response body. In this body you will find the "vmId" and "id" field. For instance, one of the requests it sends is:

https://management.azure.com/subscriptions/{my-subscription-id}/providers/Microsoft.Compute/virtualMachines?api-version=2015-06-15

,响应主体是带有相关条目的json.希望这可以帮助!:)

and the response body for this is the json with the relevant entries. Hope this helps! :)

这篇关于Azure Windows VM-如何确定它所在的资源组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆