ArgumentCompleters/ArgumentCompleterResourceGroup.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function ArgumentCompleterResourceGroup($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'parameterName')]

    $subscriptionParameters = @()
    if ($fakeBoundParameters.Subscription)
    {
        $subscriptionParameters = @('--subscription', "`"$($fakeBoundParameters.Subscription)`"")
    }
    $resourceGroups = az group list --query '[].name' @subscriptionParameters | ConvertFrom-Json
    if (-not $resourceGroups -or $resourceGroups.Length -eq 0)
    {
        return $null
    }
    $results = foreach ($resourceGroup in $resourceGroups)
    {
        if ($resourceGroup -like "${wordToComplete}*")
        {
            $description = "Resource Group '${resourceGroup}'."
            [System.Management.Automation.CompletionResult]::new($resourceGroup, $resourceGroup, "ParameterValue", $description)
        }
    }
    if ($results.Length -eq 0)
    {
        return $null
    }
    return $results
}

Register-ArgumentCompleter -CommandName Invoke-AzCli -ParameterName ResourceGroup -ScriptBlock $function:ArgumentCompleterResourceGroup