Microsoft.PowerShell.Management.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#
# .SYNOPSIS
#
# Complete the -Namespace argument to Wmi cmdlets (similiar to buil-in cim cmdlets namespace completion support)
#
function WmiNamespaceCompleter
{
   param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $nsParent='root'

    if ($wordToComplete)
    {
        [int]$delimiter = $wordToComplete.LastIndexOfAny([char[]]('\','/'))
        if($delimiter -ne -1)
        {
                $nsParent = $wordToComplete.Substring(0,$delimiter)
                $nsLeaf = $wordToComplete.Substring($delimiter+1)
        }
    }

   Microsoft.PowerShell.Management\Get-WmiObject -Class __NAMESPACE -Namespace $nsParent | Where-Object Name -like $nsLeaf* | Sort-Object Name | ForEach-Object {
         $namespace = '{0}/{1}' -f $nsParent.Replace('\','/'),$_.Name
         New-CompletionResult $namespace $namespace
   }
}


#
# .SYNOPSIS
#
# Complete the -Attributes argument to Get-ChildItem
#
function DirAttributesParameterNameCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    [System.IO.FileAttributes].GetFields('Public,Static').Name |
        Where-Object { $_ -like "$wordToComplete*" } |
        Sort-Object |
        ForEach-Object {
            # TODO - use xml docs for tooltip
            New-CompletionResult $_
        }
}

#
# .SYNOPSIS
#
# Complete the -ItemType argument to New-Item
#
function NewItemItemTypeCompletion
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    $pathProvided = $fakeBoundParameter['Path']
    if($pathProvided)
    {
        $resolvedPath = Resolve-Path -Path $pathProvided
    }
    else
    {
        $resolvedPath = $PWD
    }
    $completionSet = switch ($resolvedPath.Provider.Name) {
        FileSystem {
            Write-Output File, Directory
        }
        ActiveDirectory {
            Write-Output User, Group, OrganizationalUnit, Container, Computer
        }
        Default {
            # TODO - other providers, check if AD is complete (for useful stuff at least).
            $null
        }
    }

    $completionSet |
        Where-Object { $_ -like "$wordToComplete*" } |
        Sort-Object |
        ForEach-Object {
            New-CompletionResult $_ -ToolTip "Create $_"
        }
}


#
# .SYNOPSIS
#
# Completer for *-ControlPanelItem -Name parameter
#
function ControlPanelItemNameCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-ControlPanelItem -Name "*$wordToComplete*" | ForEach-Object {
        New-CompletionResult $_.Name $_.Description
    }
}


#
# .SYNOPSIS
#
# Completer for *-ControlPanelItem -CanonicalName parameter
#
function ControlPanelItemCanonicalNameCompleter
{
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

    Get-ControlPanelItem -CanonicalName "*$wordToComplete*" | ForEach-Object {
        New-CompletionResult $_.CanonicalName $_.Description
    }
}


Register-ArgumentCompleter `
    -Command ('Get-WmiObject','Invoke-WmiMethod','Register-WmiEvent','Remove-WmiObject','Set-WmiInstance') `
    -Parameter 'Namespace' `
    -Description 'Complete the -Namespace argument to Wmi cmdlets. For example: Get-WmiObject -Namespace <TAB>' `
    -ScriptBlock $function:WmiNamespaceCompleter


Register-ArgumentCompleter `
    -Command "Get-ChildItem" `
    -Parameter "Attributes" `
    -Description @"
Complete file attributes like Hidden or ReadOnly, for example:

    Get-ChildItem -Attributes <TAB>
"@
 `
    -ScriptBlock $function:DirAttributesParameterNameCompletion


Register-ArgumentCompleter `
    -Command 'New-Item' `
    -Parameter 'ItemType' `
    -Description @'
Complete item types (in FileSystem/ ActiveDirectory), for example:

    New-Item -ItemType <TAB>
'@
 `
    -ScriptBlock $function:NewItemItemTypeCompletion


Register-ArgumentCompleter `
    -Command ('Get-ControlPanelItem','Show-ControlPanelItem') `
    -Parameter 'Name' `
    -Description 'Complete the -Name argument to ControlPanelItem cmdlets. For example: Show-ControlPanelItem -Name <TAB>' `
    -ScriptBlock $function:ControlPanelItemNameCompleter


Register-ArgumentCompleter `
    -Command ('Get-ControlPanelItem','Show-ControlPanelItem') `
    -Parameter 'CanonicalName' `
    -Description 'Complete the -CanonicalName argument to ControlPanelItem cmdlets. For example: Show-ControlPanelItem -Name <TAB>' `
    -ScriptBlock $function:ControlPanelItemCanonicalNameCompleter