Completers/Microsoft.Az.Resources.ResourceType.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$ArgumentCompleter = @{
    ### Refresh this list of commands, using this command:
    ### (Get-Command -ParameterName ResourceType).Name.ForEach({ "'{0}'," -f $PSItem }) | Set-Clipboard
    CommandName = @(
        'Find-AzResource',
        '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 = 'ResourceType';
    ScriptBlock = {
        <#
        .SYNOPSIS
        Auto-complete the -ResourceType 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)

        ### Helper function to find an authoritative list of all Azure Resource Manager (ARM) Resource Types
        function Get-AllARMResourceTypes {
            [CmdletBinding()]
            param ( )
            $ResourceProviderList = Get-AzResourceProvider -ListAvailable -WarningAction Ignore;

            foreach ($ResourceProvider in $ResourceProviderList) {
                foreach ($ResourceType in $ResourceProvider.ResourceTypes) {
                    '{0}/{1}' -f $ResourceProvider.ProviderNamespace, $ResourceType.ResourceTypeName;
                }
            }
        }

        ### Create fresh completion results for Azure virtual machines
        $ItemList = Get-AllARMResourceTypes | Where-Object { $PSItem -match $wordToComplete } | ForEach-Object {
            $CompletionText = '{0}' -f $PSItem;
            $ToolTip = '{0}' -f $PSItem;
            $ListItemText = '{0}' -f $PSItem;
            $CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue;

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

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