PSReadLine具有奇妙的功能,因为它的Ctrl+Space键绑定。

有什么方法可以让我的脚本用户(通常是我)从可能的值列表中选择相同的“控制台菜单”功能吗?我不希望有一个单独的网格视图(Out-GridView)来进行选择。
发布于 2020-02-18 17:28:35
使用高级函数是实现这一目标的常见方法之一,如果您希望实现动态路径完成(如您的注释所示),则可以使用Register-ArgumentCompleter添加此功能。
$scriptBlock = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Get-ChildItem -Path $pwd -Directory | Where-Object {
$_ -like "*$wordToComplete*"
} | ForEach-Object {
"'$_'"
}
}
#Register the above scriptblock to the foo function Path Parameter
Register-ArgumentCompleter -CommandName foo -ParameterName Path -ScriptBlock $scriptBlock
function foo {
param(
[ValidateSet("Tom","Dick","Jane")]
$Name,
[ValidateRange(21, 65)]
$Age,
[string]
$Path
)
Write-Host ($Name + $Age + $Path)
}更多信息可以在Get-Help about_Functions_Advanced中找到。
上面的部分目录名也适用于部分目录名,例如,如果您知道该目录在名称类型foo -path test中有"test“并按CTRL+Space键,您将得到一个筛选列表--非常酷,对吗?
试试看
https://stackoverflow.com/questions/60286239
复制相似问题