# define a function without argument completer functionStart-Software { param( [Parameter(Mandatory)] [string] $Path )
Start-Process-FilePath$Path }
# define the code used for completing application paths $code = {
}
# calculate the completion values once, and reuse the values later # store results in a script-global variable $script:applicationCompleter = & { # get registered applications from registry $key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\*", "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\*"
# add applications found by Get-Command [System.Collections.Generic.List[string]]$commands = Get-Command-CommandType Application | Select-Object-ExpandProperty Source $list.AddRange($commands)
# add descriptions and compose completionresult entries $list | # remove empty paths Where-Object { $_ } | # remove quotes and turn to lower case ForEach-Object { $_.Replace('"','').Trim().ToLower() } | # remove duplicate paths Sort-Object-Unique | ForEach-Object { # skip files that do not exist if ( (Test-Path-Path$_)) { # get file details $file = Get-Item-Path$_ # quote path if it has spaces $path = $_ if ($path-like'* *') { $path = "'$path'" } # make sure tooltip is not null $tooltip = [string]$file.VersionInfo.FileDescription if ([string]::IsNullOrEmpty($tooltip)) { $tooltip = $file.Name } # compose completion result [Management.Automation.CompletionResult]::new( # complete path $path, # show friendly text in IntelliSense menu ('{0} ({1})'-f$tooltip, $file.Name), # use file icon 'ProviderItem', # show file description $tooltip ) } } }
# instead of complex code, simply return the cached results when needed $code = { $script:applicationCompleter }
# tie the completer code to all applicable parameters of own or foreign commands Register-ArgumentCompleter-CommandNameStart-Software-ParameterName Path -ScriptBlock$code Register-ArgumentCompleter-CommandNameStart-Process-ParameterName FilePath -ScriptBlock$code
# add applications found by Get-Command [System.Collections.Generic.List[string]]$commands = Get-Command-CommandType Application | Select-Object-ExpandProperty Source $list.AddRange($commands)
# add descriptions and compose completionresult entries $list | # remove empty paths Where-Object { $_ } | # remove quotes and turn to lower case ForEach-Object { $_.Replace('"','').Trim().ToLower() } | # remove duplicate paths Sort-Object-Unique | ForEach-Object { # skip files that do not exist if ( (Test-Path-Path$_)) { # get file details $file = Get-Item-Path$_ # quote path if it has spaces $path = $_ if ($path-like'* *') { $path = "'$path'" } # make sure tooltip is not null $tooltip = [string]$file.VersionInfo.FileDescription if ([string]::IsNullOrEmpty($tooltip)) { $tooltip = $file.Name } # compose completion result [Management.Automation.CompletionResult]::new( # complete path $path, # show friendly text in IntelliSense menu ('{0} ({1})'-f$tooltip, $file.Name), # use file icon 'ProviderItem', # show file description $tooltip ) } } }
# tie the completer code to all applicable parameters of own or foreign commands Register-ArgumentCompleter-CommandNameStart-Software-ParameterName Path -ScriptBlock$code Register-ArgumentCompleter-CommandNameStart-Process-ParameterName FilePath -ScriptBlock$code
[Management.Automation.CompletionResult]::new("'OU=managers,DC=company,DC=local'", "Management", "ProviderItem", "OU where the Management lives") [Management.Automation.CompletionResult]::new("'OU=subtest,OU=test,DC=company,DC=local'", "Experimental", "DynamicKeyword", "Reserved") [Management.Automation.CompletionResult]::new("'OU=External,OU=IT,DC=company,DC=local'", "Help Desk", "ProviderItem", "OU where the Helpdesk people reside")
})] [string] $OU )
"Chosen path: $OU" }
完整代码基本上只创建三个新的CompletionResult对象。每个参数都有四个参数:
自动完成的文字
显示在 IntelliSense 菜单中的文字
IntelliSense 菜单的图标
IntelliSense 菜单的工具提示
您甚至可以控制 IntelliSense 菜单中显示的图标。这些是预定义的图标:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
PS> [Enum]::GetNames([System.Management.Automation.CompletionResultType]) Text History Command ProviderItem ProviderContainer Property Method ParameterName ParameterValue Variable Namespace Type Keyword DynamicKeyword
当您运行此代码然后调用 Get-OU 时,可以按 TAB 键完成 OU X500 路径,也可以按 CTRL + SPACE 打开 IntelliSense 菜单。在菜单内,您会看到所选的图标和友好的文本。选择项目后,将使用 X500 自动完成的文字。