我在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: azure.microsoft/zh-cn/blog/accessing-and-using-azure-vm-unique-id/).如果使用Linux VM,请确保考虑到不同的字节序,否则vmId将不匹配.
From your C# code, find the vmId (involves running one of the commands at the following link or possibly using an Azure SDK: azure.microsoft/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:
management.azure/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
发布评论