PowerShell: Find VNet of an ARM VM

%3CLINGO-SUB%20id%3D%22lingo-sub-56673%22%20slang%3D%22en-US%22%3EPowerShell%3A%20Find%20VNet%20of%20an%20ARM%20VM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-56673%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20everyone%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eit%20took%20me%20quite%20some%20time%20to%20find%20a%20way%20to%20get%20the%20VNet%20an%20ARM%20VM%20belongs%20to%20by%20using%20PowerShell.%20I%20don't%20think%20this%20is%20the%20perfect%20solution%2C%20but%20I%20would%20like%20to%20share%20it%20in%20order%20someone%20else%20needs%20to%20find%20out.%20I%20would%20be%20interested%20in%20better%20ways%20of%20getting%20attached%20networks.%20I%20will%20start%20the%20discussion%20with%20this%20code%20snippet%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%20%24vnets%3D%20Get-AzureRmVirtualNetwork%3CBR%20%2F%3E%20%24vm%20%3D%20Get-AzureRmVM%20-name%20%24newVm.Name%20-ResourceGroupName%20%24newRG%3CBR%20%2F%3E%20%24vmIPId%20%3D%20%24vm.NetworkProfile.NetworkInterfaces.Id%3CBR%20%2F%3E%20foreach%20(%24vnet%20in%20%24vnets)%7B%3CBR%20%2F%3E%20%20%24vnetIPId%20%3D%20%24vnet.Subnets.IpConfigurations.Id%3CBR%20%2F%3E%20%20if%20(%24vnetIPId.Startswith(%24vmIPId)%20)%7B%3CBR%20%2F%3E%20%20%20%24vnetName%20%3D%20%24vnet.Name%3CBR%20%2F%3E%20%20%20return%3CBR%20%2F%3E%20%20%7D%3CBR%20%2F%3E%20%7D%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECheers%20Christian%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-56673%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAutomatic%20%26amp%3B%20Control%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAutomation%20and%20DSC%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-117745%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%3A%20Find%20VNet%20of%20an%20ARM%20VM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-117745%22%20slang%3D%22en-US%22%3E%3CP%3ENot%20really%20innovating%2C%20just%20bringing%20it%20down%20to%20a%20single%20line%3A%3C%2FP%3E%3CPRE%3E%24vnetName%20%3D%20Get-AzureRmVirtualNetwork%20%7C%20%25%20%7Bif(%24_.Subnets.IpConfigurations.Id.StartsWith(%24vm.NetworkProfile.NetworkInterfaces.Id))%7Breturn%20%24_.Name%7D%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-58126%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%3A%20Find%20VNet%20of%20an%20ARM%20VM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-58126%22%20slang%3D%22en-US%22%3E%3CP%3ENice.%20This%20was%20one%20of%20my%20approaches%20as%20well.%3C%2FP%3E%3CP%3EWhat%20I%20wanted%20to%20automate%20was%20to%20get%20the%20vNet%20without%20knowing%20the%20ResourceGroup%20it%20belongs%20to.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-58002%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%3A%20Find%20VNet%20of%20an%20ARM%20VM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-58002%22%20slang%3D%22en-US%22%3E%3CP%3EHello!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFunny%2C%20that%20it%20isnt%20straight%20forward%20to%20get%20the%20vNet!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20to%20do%20it%20using%20another%20method%2C%20but%20in%20the%20end%20I%20am%20not%20sure%20it%20is%20better%20than%20yours.%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24RG%20%3D%20%22HybridWorkers%22%0A%20%24VMName%20%3D%20%22AA10%22%0A%20%0A%20Login-AzureRmAccount%0A%20%24VM%20%3D%20Get-AzureRMVM%20-ResourceGroupName%20%24RG%20-Name%20%24VMName%20%0A%20%24VMNicName%20%3D%20%24vm.NetworkProfile.NetworkInterfaces.Id.Split(%22%2F%22)%5B-1%5D%20%0A%20%24VMNic%20%3D%20Get-AzureRmNetworkInterface%20-ResourceGroupName%20%24RG%20-Name%20%0A%20%24VMvNetName%20%3D%20%24VMNic.IpConfigurations.Subnet.Id.Split(%22%2F%22)%5B8%5D%0A%20%24vNet%20%3D%20Get-AzureRmVirtualNetwork%20-ResourceGroupName%20%24RG%20-Name%20%24VMvNetName%0A%0A%20%24vNet%3C%2FPRE%3E%3CP%3EIt%20think%20it%20is%20quite%20weird%20that%20the%20command%20outputs%20this%20%22Id%22%20but%20afaik%20the%20other%20commands%20cannot%20use%20that%20as%20input%20to%20get%20a%20resource.%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-56769%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%3A%20Find%20VNet%20of%20an%20ARM%20VM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-56769%22%20slang%3D%22en-US%22%3EI%20think%20it%20should%20work%20without%20the%20for%20loop%20as%20well.%20I%20will%20have%20a%20try%20tomorrow.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1590467%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%3A%20Find%20VNet%20of%20an%20ARM%20VM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1590467%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20revised%20the%20script%20with%20the%20following%20guidance%3A%3C%2FP%3E%3COL%3E%3CLI%3E%3CSPAN%3ENo%20looping%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%3ENo%20dependencies%20on%20additionally%20required%20pieces%20of%20information%2C%20e.g.%20resource%20group%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FOL%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24vm%26nbsp%3B%3D%26nbsp%3Bget-azvm%26nbsp%3B-name%26nbsp%3Bazpbagentdvm01%26nbsp%3B%0A%24nic%26nbsp%3B%3D%26nbsp%3Bget-aznetworkinterface%26nbsp%3B-resourceid%26nbsp%3B%24vm.NetworkProfile%5B0%5D.NetworkInterfaces%5B0%5D.Id%0A%24subnetresourceid%26nbsp%3B%3D%26nbsp%3B%24nic.IpConfigurations%5B0%5D.Subnet.id%0A%24split%26nbsp%3B%3D%26nbsp%3B%24subnetresourceid.split(%22%2F%22)%0A%24vnetresourceid%26nbsp%3B%3D%26nbsp%3B%5Bstring%5D%3A%3AJoin(%22%2F%22%2C%26nbsp%3B%24split%5B0..(%24split.Count%26nbsp%3B-%26nbsp%3B3)%5D)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EOr%20you%20could%20just%20pick%20the%20resource%20group%20and%20vnet%20name%20with%20%60%24subnet.split(%22%2F%22)%5B4%5D%60%20and%20%60%24subnet.split(%22%2F%22)%5B8%5D%60.%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EObviously%20this%20solution%20assumes%20a%20particular%20structure%20of%20the%20subnet%20resourceid.%20I%20wonder%20if%20there%20is%20a%20utility%20function%20in%20the%20Az%20modules%20package%20somewhere%20that%20helps%20with%20that.%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%2Fsubscriptions%2F%3CGUID%3E%2FresourceGroups%2F%3CRESOURCEGROUPNAME%3E%2Fproviders%2FMicrosoft.Network%2FvirtualNetworks%2F%3CVNETNAME%3E%2Fsubnets%2F%3CSUBNETNAME%3E%3C%2FSUBNETNAME%3E%3C%2FVNETNAME%3E%3C%2FRESOURCEGROUPNAME%3E%3C%2FGUID%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

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

5 Replies
Highlighted
I think it should work without the for loop as well. I will have a try tomorrow.
Highlighted

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. :)

 

 

Highlighted

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.

Highlighted

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}}
Highlighted

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>