Completers/Microsoft.AzureRm.Resources.ResourceId.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
61
62
63
64
65
66
67
68
$ScriptBlock = {
        <#
        .SYNOPSIS
        Auto-complete the -ResourceId parameter value for Azure Resource Manager (ARM) PowerShell cmdlets.

        .NOTES
        Created by Trevor Sullivan <trevor@trevorsullivan.net>
        http://trevorsullivan.net
        #>

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

        try {
            $ObjectList = Get-AzureRmResource -ErrorAction Stop -WarningAction Ignore;
        } catch {
            Write-Host -Object ('Error occurred retrieving Automation Accounts: {0}' -f $PSItem.Exception.Message);
        }
        $ItemList = $ObjectList | Where-Object { $PSItem.AutomationAccountName -match $wordToComplete } | ForEach-Object {
            $CompletionText = $PSItem.ResourceId;
            $ToolTip = 'Resource {0} in {1} region, in {2} Resource Group.' -f $PSItem.ResourceName, $PSItem.Location, $PSItem.ResourceGroupName;
            $ListItemText = '{0} ({1})' -f $PSItem.ResourceName, $PSItem.Location;
            $CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue;

            New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList @($CompletionText, $ListItemText, $CompletionResultType, $ToolTip);
        }

        return $ItemList
    }
$ArgumentCompleter = @{
        ### NOTE: Use this command to generate command list:
        ### (Get-Command -Module AzureRM* -ParameterName ResourceId).Name.ForEach({ "'{0}'," -f $PSItem }) | Set-Clipboard
        CommandName = @(
            'Get-AzureRmAlertHistory',
            'Get-AzureRmAutoscaleHistory',
            'Get-AzureRmDiagnosticSetting',
            'Get-AzureRmLog',
            'Get-AzureRmMetric',
            'Get-AzureRmMetricDefinition',
            'Get-AzureRmPolicyAssignment',
            'Get-AzureRmPolicyDefinition',
            'Get-AzureRmResource',
            'Get-AzureRmResourceGroup',
            'Get-AzureRmResourceGroupDeployment',
            'Get-AzureRmResourceLock',
            'Get-AzureRmUsage',
            'Invoke-AzureRmResourceAction',
            'Move-AzureRmResource',
            'New-AzureRmResource',
            'New-AzureRmResourceLock',
            'Remove-AzureRmPolicyAssignment',
            'Remove-AzureRmPolicyDefinition',
            'Remove-AzureRmResource',
            'Remove-AzureRmResourceGroup',
            'Remove-AzureRmResourceGroupDeployment',
            'Remove-AzureRmResourceLock',
            'Set-AzureRmDiagnosticSetting',
            'Set-AzureRmPolicyAssignment',
            'Set-AzureRmPolicyDefinition',
            'Set-AzureRmResource',
            'Set-AzureRmResourceGroup',
            'Set-AzureRmResourceLock',
            'Stop-AzureRmResourceGroupDeployment'
        );
        ParameterName = 'ResourceId';
        ScriptBlock = $ScriptBlock;
}

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