I am trying to retrieve the resource id from within a foreach but nothing is being returned, when looking at the debug output, the value of $item is empty.
sample code:
$vnetPNames=@("Name1","Name2","Name3")
foreach ($item in $vnetPNames) {
$getId = az network vnet list --query "`"[?name =='$item'].id`"" -o tsv
az network vnet peering create ...
}
any pointers to the simple item i am missing here?
I have reproduced in my environment and i got expected results :
I have seen that
your code has extra " (quotations) .
The code that worked for me is :
$vnetPNames=@("Rith123","bojja123")
foreach ($item in $vnetPNames) {
$getId = az network vnet list --query "[?name =='$item'].id" -o tsv
}
Here Rith123 and bojja123 are Vnet Names.
Please try to edit your code accordingly and you will get item.