Forum Discussion

Christian Forjahn's avatar
Christian Forjahn
Brass Contributor
Mar 25, 2017

PowerShell: Find VNet of an ARM VM

Hi everyone,

 

it took me quite some time to find a way to get the VNet an ARM VM belongs to by using PowerShell. I don't think this is the perfect solution, but I would like to share it in order someone else needs to find out. I would be interested in better ways of getting attached networks. I will start the discussion with this code snippet

 

 

 $vnets= Get-AzureRmVirtualNetwork
$vm = Get-AzureRmVM -name $newVm.Name -ResourceGroupName $newRG
$vmIPId = $vm.NetworkProfile.NetworkInterfaces.Id
foreach ($vnet in $vnets){
$vnetIPId = $vnet.Subnets.IpConfigurations.Id
if ($vnetIPId.Startswith($vmIPId) ){
$vnetName = $vnet.Name
return
}
}

 

Cheers Christian

  • Hello!

     

    Funny, that it isnt straight forward to get the vNet!

     

    I tried to do it using another method, but in the end I am not sure it is better than yours. :)

     

    $RG = "HybridWorkers"
     $VMName = "AA10"
     
     Login-AzureRmAccount
     $VM = Get-AzureRMVM -ResourceGroupName $RG -Name $VMName 
     $VMNicName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[-1] 
     $VMNic = Get-AzureRmNetworkInterface -ResourceGroupName $RG -Name 
     $VMvNetName = $VMNic.IpConfigurations.Subnet.Id.Split("/")[8]
     $vNet = Get-AzureRmVirtualNetwork -ResourceGroupName $RG -Name $VMvNetName
    
     $vNet

    It think it is quite weird that the command outputs this "Id" but afaik the other commands cannot use that as input to get a resource. :)

     

     

    • Christian Forjahn's avatar
      Christian Forjahn
      Brass Contributor

      Nice. This was one of my approaches as well.

      What I wanted to automate was to get the vNet without knowing the ResourceGroup it belongs to.

      • carlintveld's avatar
        carlintveld
        Brass Contributor

        I have revised the script with the following guidance:

        1. No looping
        2. No dependencies on additionally required pieces of information, e.g. resource group
         

         

        $vm = get-azvm -name azpbagentdvm01 
        $nic = get-aznetworkinterface -resourceid $vm.NetworkProfile[0].NetworkInterfaces[0].Id
        $subnetresourceid = $nic.IpConfigurations[0].Subnet.id
        $split = $subnetresourceid.split("/")
        $vnetresourceid = [string]::Join("/", $split[0..($split.Count - 3)])

         

         
        Or you could just pick the resource group and vnet name with `$subnet.split("/")[4]` and `$subnet.split("/")[8]`.
         
        Obviously this solution assumes a particular structure of the subnet resourceid. I wonder if there is a utility function in the Az modules package somewhere that helps with that.
         
        /subscriptions/<guid>/resourceGroups/<resourcegroupname>/providers/Microsoft.Network/virtualNetworks/<vnetname>/subnets/<subnetname>
  • Jan Steenbeek's avatar
    Jan Steenbeek
    Copper Contributor

    Not really innovating, just bringing it down to a single line:

    $vnetName = Get-AzureRmVirtualNetwork | % {if($_.Subnets.IpConfigurations.Id.StartsWith($vm.NetworkProfile.NetworkInterfaces.Id)){return $_.Name}}
  • goblehp's avatar
    goblehp
    Copper Contributor

    Christian Forjahn 

    This seems simpler to me because I don't know how to program like you guys do.  Maybe it will help a sysadmin like myself.


    $SourceVmObject = Get-AzVM -name 'HPG-DA-2'
    $SourceVMNIC = $SourceVmObject.NetworkProfile.NetworkInterfaces[0].Id | Get-AzNetworkInterface
    $SourceSubscriptionVnets = Get-AzVirtualNetwork
    $SourceSubscriptionVnet = $SourceSubscriptionVnets | where {$_.subnets.ipconfigurations.id -eq $SourceVMNIC.IpConfigurations.id}
    $SourceSubscriptionVnet.Name

Resources