How to create dynamic url of a Azure Virtual Machine

Vikas Neekhra 6 Reputation points
2021-11-05T06:16:36.093+00:00

I need to create a dynamic Azure virtual machine url in Azure programmatically... I have this info (Subscription, resource group and resource name) of a VM and using that I need to create a url of that specific VM in Azure portal, so that once a user clicks on that link will redirect the user to that VM page in Azure cloud console.

What I have seen in the below url is active directory which I don't have for that specific customer/subscription, so how I can find that programmatically, or is there any other way I can create the direct url of a Azure VM. Please suggest

https://portal.azure.com/#@**<active directory>.onmicrosoft.com/resource/subscriptions/xxxxxxxxxx-yyyyyyy-zzzzzzzzzzzzzzzz/resourceGroups/<resource-group-name>/providers/Microsoft.Compute/virtualMachines/<resource-name>**/overview

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,185 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,141 Reputation points Microsoft Employee
    2021-11-08T01:48:58.023+00:00

    Hello @Vikas Neekhra ,
    In general the URL on top of the Azure Portal it shows only after the user is authenticated under that subscription Tenant i.e. user should have subscription access.
    There is a Azure CLI command to get the active directory name of the logged-in subscription.

    When you do : az ad signed-in-user show
    It will display all the information, look out for the attribute "userPrincipalName"
    Or you can say: az ad signed-in-user show --query 'userPrincipalName'

    You can use the output of the previous command and add it to build that complete URL for accessing the Virtual Machine.

    Regards,
    Shiva.

    0 comments No comments