Search code examples
azurepowershellazure-cli

Get only Nic name similar to a particular string in Azure CLI


This command gives all NIC names in the Resource group

az network nic list --resource-group "RG_TEST" --query "[0].name" 

I want to get only NIC names contains the below string

String: NIC_PROD_TEST


Solution

  • After my workaround around this, I was able to retrieve results for a particular string by using the below query.

    Eg: String: 'new'

    Use "query "[?contains(name, 'string')].name" as shown below:

    az network nic list --resource-group <resourcegroupName> --query "[?contains(name, 'new')].name"
    

    Output:

    enter image description here

    If you want to get nic name starts with a particular string, you can use

    "query "[?starts_with(name, 'string')].name"

    az network nic list --resource-group <resourcegroupName --query "[?starts_with(name, 'new')].name"
    

    Output:

    enter image description here

    Reference: MSDoc