Appx.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
69
70
71
72
73
74
75
76
77
78
79
80
# ARGUMENT COMPLETER FUNCTIONS #################################################

# APPX Package
function Appx_PackageNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Appx\Get-AppxPackage -Name "$wordToComplete*" |
        Sort-Object -Property Name |
        ForEach-Object {
            $ToolTip = "Name: {0} - Version: {1}" -f $_.Name,$_.Version
            New-CompletionResult -CompletionText $_.Name -ToolTip $ToolTip
        }
}

function Appx_PackagePublisherArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Appx\Get-AppxPackage -Publisher "$wordToComplete*" |
        Select-Object -Property Publisher -Unique |
        Sort-Object -Property Publisher |
        ForEach-Object {
            $ToolTip = "Publisher: {0}" -f $_.Publisher
            New-CompletionResult -CompletionText $_.Publisher -ToolTip $ToolTip
        }
}

function Appx_PathArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-CompletionWithExtension $lastWord ('.appx')
}

# APPX Package Manifest
function Appx_PackageManifestNameArgumentCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Appx\Get-AppxPackage |
        Where-Object {$_.PackageFullName -like "$wordToComplete*"} |
        Sort-Object -Property PackageFullName |
        ForEach-Object {
            $ToolTip = "Name: {0} - Version: {1} - Full Name: {2}" -f $_.Name,$_.Version,$_.PackageFullName
            New-CompletionResult -CompletionText $_.PackageFullName -ToolTip $ToolTip
        }
}

################################################################################

# APPX Package

Register-ArgumentCompleter `
    -Command ('Get-AppxPackage') `
    -Parameter 'Name' `
    -Description 'Complete Appx names, for example: Get-AppxPackage -Name <TAB>' `
    -ScriptBlock $function:Appx_PackageNameArgumentCompletion

Register-ArgumentCompleter `
    -Command ('Get-AppxPackage') `
    -Parameter 'Publisher' `
    -Description 'Complete Appx names, for example: Get-AppxPackage -Publisher <TAB>' `
    -ScriptBlock $function:Appx_PackagePublisherArgumentCompletion

Register-ArgumentCompleter `
    -Command ('Add-AppxPackage') `
    -Parameter 'Path' `
    -Description 'Complete Appx path names, for example: Add-AppxPackage -Path <TAB>' `
    -ScriptBlock $function:Appx_PathArgumentCompletion

# APPX Package Manifest

Register-ArgumentCompleter `
    -Command ('Get-AppxPackageManifest','Remove-AppxPackage') `
    -Parameter 'Package' `
    -Description 'Complete Appx Package names, for example: Get-AppxPackageManifest -Package <TAB>' `
    -ScriptBlock $function:Appx_PackageManifestNameArgumentCompletion