Azure vnet / subnet list connected devices
Asked Answered
P

1

6

I'm trying to write a PowerShell script that lists me all connected devices with there ips in my subnets in all VNets in all subscriptions.

UPDATE the below script works, scroll down for original problem.

$subs = Get-AzSubscription 
foreach ($Sub in $Subs) {
    Write-Host "***************************"
    Write-Host " "
    $Sub.Name 

    $SelectSub = Select-AzSubscription -SubscriptionName $Sub.Name

    $VNETs = Get-AzVirtualNetwork 
    foreach ($VNET in $VNETs) {
        Write-Host "--------------------------"
        Write-Host " "
        Write-Host "   vNet: " $VNET.Name 
        Write-Host "   AddressPrefixes: " ($VNET).AddressSpace.AddressPrefixes

        $vNetExpanded = Get-AzVirtualNetwork -Name $VNET.Name -ResourceGroupName $VNET.ResourceGroupName -ExpandResource 'subnets/ipConfigurations' 

        foreach($subnet in $vNetExpanded.Subnets)
        {
            Write-Host "       Subnet: " $subnet.Name
            Write-Host "          Connected devices " $subnet.IpConfigurations.Count
            foreach($ipConfig in $subnet.IpConfigurations)
            {
                Write-Host "            " $ipConfig.PrivateIpAddress
            }
        }

        Write-Host " " 
    } 
}

My original problem:

The problem however is that I cannot get the $subnet.IpConfigurations property to be filled. By that I mean that I do get a list but the child items only contain an Id, the rest of the properties like name, privateip, etc are null.

Pecoraro answered 10/1, 2020 at 19:35 Comment(2)
Could you please tell me what is " connected devices"? Azure VM?Decree
That could be anything that has a nic basically. For us most of the time that means a Nic, vm scaleset instance or a loadbalancerPecoraro
D
4

According to my research, if we want to get detailed information about subnet IpConfigurations, we need to specify the parameter ExpandResource with powershell command Get-AzVirtualNetwork. For example

$result=Get-AzVirtualNetwork -Name '<vnet name>'  -ResourceGroupName '<group nmae>' -ExpandResource 'subnets/ipConfigurations' 

$result.Subnets[0].IpConfigurations

enter image description here

Decree answered 13/1, 2020 at 8:16 Comment(1)
You're awesome, that works! I did try with ExpandProperty but could not get the 'subnets/ipConfigurations' correct...Pecoraro

© 2022 - 2024 — McMap. All rights reserved.