How to get VirtualNetwork name from virtual machine which is attached in Load balancer Backend pool using Microsoft Azure Management Network Fluent?

Dnyaneshwar Surywanshi 156 Reputation points
2020-12-14T16:54:24.273+00:00

I want to get VirtualNetwork name from virtual machine which is attached in Load balancer Backend pool.48041-vnet.png

In above image i have marked Vnet name i want that name and this virtual machine is attached with backend pool of load balancer.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,124 questions
Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,141 questions
Azure Load Balancer
Azure Load Balancer
An Azure service that delivers high availability and network performance to applications.
402 questions
{count} votes

Accepted answer
  1. KalyanChanumolu-MSFT 8,316 Reputation points
    2020-12-18T08:08:05.11+00:00

    @Dnyaneshwar Surywanshi Apologies for the delayed response.

    I tried to work backwards to get NIC details from the VM, then the Subnet to which the NIC was bound to and then the VNet to which the subnet belonged.

    var vm = azure.VirtualMachines.GetById("/subscriptions/**-**-**-**/resourceGroups/Networking-Lab/providers/Microsoft.Compute/virtualMachines/VM1");  
    var primaryNetworkInterface = vm.GetPrimaryNetworkInterface();  
    var subnetId = azure.NetworkInterfaces.GetById(primaryNetworkInterface.Id).Inner.IpConfigurations.FirstOrDefault().Subnet.Id;  
       
    var vNet = azure.Networks.List().Where(n => n.Inner.Subnets.Select(sn => string.Equals(sn.Id, subnetId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()).FirstOrDefault();  
      
     Console.Write($" VirtualMachine: {vm.Name} \n NetworkInterface: {primaryNetworkInterface.Name} \n VirtualNetwork: {vNet.Name} \n Subnet:  {ResourceUtils.NameFromResourceId(subnetId)}");  
    

    Here is the output

    49417-image.png

    Please let me know if you face any issues.

    ----------

    If an answer is helpful, please "Accept answer" or "Up-Vote" for the same which might be beneficial to other community members reading this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful