completions/StorageAccountNameCompletion.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
function StorageAccountNameCompleter {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters)

    $params = @{};
    $resourceGroupName = $boundParameters["ResourceGroupName"]
    if ($resourceGroupName -ne $null) {
        $params.ResourceGroupName = $resourceGroupName;
    }
    $ItemList = Get-AzureRmStorageAccount @params | ForEach-Object { $_.StorageAccountName } | Sort-Object # Select-Object with -ExpandParameter didn't work here!
    $wordToCompleteWildcard = $wordToComplete.Trim("'") + "*"

    $results = $ItemList `
        | Where-Object { $PSItem -like $wordToCompleteWildcard} `
        | Foreach-Object {  
        if ($PSItem -ne $null -and $PSItem -ne "") {
            $CompletionResult = @{
                CompletionText       = $PSItem
                ToolTip              = $PSItem
                ListItemText         = $PSItem
                CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue
            }
            New-CompletionResult @CompletionResult
        }
    }
    return $results
}

# "'" + [string]::Join("', '", (Get-command -Module AzureRM.Storage -Name *Storage* -ParameterName Name -ParameterType string | Sort-Object -Property Name)) + "'" | clip

Register-ArgumentCompleter `
    -Command ( 'Get-AzureRmStorageAccount', 'Get-AzureRmStorageAccountKey', 'Get-AzureRmStorageAccountNameAvailability', 'Get-AzureStorageContainerAcl', 'New-AzureRmStorageAccount', 'New-AzureRmStorageAccountKey', 'Remove-AzureRmStorageAccount', 'Set-AzureRmCurrentStorageAccount', 'Set-AzureRmStorageAccount'    
) `
    -Parameter 'Name' `
    -ScriptBlock $function:StorageAccountNameCompleter