Completers/Microsoft.Azure.Subscription.SubscriptionName.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
$ArgumentCompleter = @{
        CommandName = @(
            'Get-WAPackSubscription', 
            'Remove-WAPackSubscription', 
            'Select-WAPackSubscription', 
            'Set-WAPackSubscription', 
            'Get-AzureSubscription', 
            'New-AzureSqlDatabaseServerContext', 
            'Remove-AzureSubscription', 
            'Select-AzureSubscription', 
            'Set-AzureSubscription'
            );
        ParameterName = 'SubscriptionName';
        #Description = 'Complete the -SubscriptionName parameter value for Azure cmdlets: Select-AzureSubscription -SubscriptionName <TAB>';
        ScriptBlock = {
        <#
            .SYNOPSIS
            Auto-complete the -SubscriptionName parameter value for Azure PowerShell cmdlets.

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

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

        #Write-Verbose -Message ('Called Azure SubscriptionName completer at {0}' -f (Get-Date))

        ### Create fresh completion results for Azure subscriptions
        $ItemList = Get-AzureSubscription | Where-Object { $PSItem.SubscriptionName -match ${wordToComplete} } | ForEach-Object {
            $CompletionText = $PSItem.SubscriptionName
            $ToolTip = 'Azure subscription "{0}" with ID {1}.' -f $PSItem.SubscriptionName, $PSItem.SubscriptionId
            $ListItemText = '{0} ({1})' -f $PSItem.SubscriptionName, $PSItem.SubscriptionId
            $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;