Completers/Microsoft.Az.Resource.Name.ArgumentCompleters.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$ArgumentCompleter = @{
    CommandName = @(
        'Get-AzResource'
        'Get-AzResourceLock'
        'Get-AzRoleAssignment'
        'Invoke-AzResourceAction'
        'New-AzResource'
        'New-AzResourceLock'
        'New-AzRoleAssignment'
        'Remove-AzResource'
        'Remove-AzResourceLock'
        'Remove-AzRoleAssignment'
        'Set-AzResource'
        'Set-AzResourceLock'
    );
    ParameterName = 'ResourceName';
    #Description = 'Complete the -ResourceName parameter value for Azure Resource Manager cmdlets: Get-AzureResource -ResourceName <TAB>';
    ScriptBlock = {
        <#
        .SYNOPSIS
        Auto-complete the -ResourceName parameter value for Azure Resource Manager (ARM) cmdlets.
    
        .NOTES
        Created by Trevor Sullivan <trevor@trevorsullivan.net>
        http://trevorsullivan.net
        http://twitter.com/pcgeek86
        #>

        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

        ### Create fresh completion results for Azure virtual machines
        $ItemList = Get-AzResource | Where-Object { $PSItem.ResourceName -match $wordToComplete } | ForEach-Object {
            $CompletionText = '{0} -ResourceType {1} -ResourceGroupName {2}' -f $PSItem.ResourceName, $PSItem.ResourceType, $PSItem.ResourceGroupName;
            $ToolTip = 'Resource {0} of type {1} in Resource Group {2}' -f $PSItem.ResourceName, $PSItem.ResourceType, $PSItem.ResourceGroupName;
            $ListItemText = '{0} ({1})' -f $PSItem.ResourceName, $PSItem.ResourceGroupName;
            $CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue;

            New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList @($CompletionText, $ListItemText, $CompletionResultType, $ToolTip);
        }
    
        ### Return the fresh completion results
        return $ItemList;
    }
}

Microsoft.PowerShell.Core\Register-ArgumentCompleter @ArgumentCompleter;