Search code examples
azurepowershellazure-cliazure-cli2

Powershell count azure CLI result


I am trying to achieve that my output if it is one single line it gets written away in a variable

This is what i have so far

az group list --query '[].{name:name}' --output table
$filter = Read-Host -Prompt "Please filter to find the correct resource group"
az group list --query "[?contains(name, '$filter')].name" --output tsv

what this code does is that you can filter all the resource groups and you get to see the ouptut in a TSV

What i am trying to add is that it checks if it is the only line and then write that line away (if it is one line)


Solution

  • Since you are already using powershell, why not just use powershell?

    $filter = Read-Host -Prompt "Please filter to find the correct resource group"
    Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $filter }
    

    Thanks for the help with changing the code! The end result is now:

    Connect-AzureRmAccount
    (get-azurermresourcegroup).ResourceGroupName 
    $filter = Read-Host -Prompt "Please filter to find the correct resource group" 
    $RGName = get-azurermresourcegroup | Where-Object { $_.ResourceGroupName -match $filter } 
    $RGName.resourcegroupname