Search code examples
powershellazure-cli

Powershell foreach variable passing to az cli query missing


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?


Solution

  • I have reproduced in my environment and i got expected results :

    I have seen that

    enter image description here 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 
    }
    

    enter image description here

    Here Rith123 and bojja123 are Vnet Names.

    Please try to edit your code accordingly and you will get item.