You can simplify your code significantly by using Select-Object
to extract and annotate the Subnets
values:
Get-AzVirtualNetwork |Select-Object -Property @{Name='Vnet';E=
Name'} -ExpandProperty Subnets |Select-Object Vnet,@{Name='Subnet';Expression={$_.Name}}
If you insist on using loops, you'll need a nested loop (since the individual subnets are nested inside each vnet):
$vNets = Get-AzVirtualNetwork | Select-Object Name, Subnets
$ResourceList=@()
foreach($vNet in $vNets){
foreach($subnet in $vNet.Subnets){
$VMObject = New-Object PSObject
$VMObject | Add-Member -MemberType NoteProperty -Name "Vnet" -Value $vNet.Name
$VMObject | Add-Member -MemberType NoteProperty -Name "Subnet" -Value $subnet.Name
$ResourceList += $VMObject
}
}
$ResourceList
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…