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.

E. Staal
  • 525
  • 5
  • 18
  • Could you please tell me what is " connected devices"? Azure VM? – Jim Xu Jan 13 '20 at 03:25
  • 1
    That could be anything that has a nic basically. For us most of the time that means a Nic, vm scaleset instance or a loadbalancer – E. Staal Jan 13 '20 at 07:13

1 Answers1

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

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • You're awesome, that works! I did try with ExpandProperty but could not get the 'subnets/ipConfigurations' correct... – E. Staal Jan 13 '20 at 08:40