Since you're using PowerShell, why not stick to writing the argument completer in PowerShell, too?
Does this help? about_functions_argument_completion
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I wrote the code below as a means of Time Zone lookup and conversion. One of the things I wanted was completion of names based on ID, System Name, Display Name, or Daylight Name. But while the code works, the argument lookup doesn't. What's wrong?
PowerShell
$mapCommonTimeZones =
@{
'UTC' = [TimeZoneInfo]::UTC;
'PST' = ([TimeZoneInfo]::FindSystemTimeZoneById('Pacific Standard Time') | Where-Object { $_.DisplayName -match '\(US \& Canada\)' })[0];
'MST' = ([TimeZoneInfo]::FindSystemTimeZoneById('Mountain Standard Time'))[0];
'CST' = ([TimeZoneInfo]::FindSystemTimeZoneById('Central Standard Time') | Where-Object { $_.DisplayName -match '\(US \& Canada\)' })[0];
'EST' = ([TimeZoneInfo]::FindSystemTimeZoneById('Mountain Standard Time'))[0];
};
class TimeZoneCompleter : System.Management.Automation.IArgumentCompleter
{
[System.Collections.Generic.IEnumerable[System.Management.Automation.CompletionResult]] CompleteArgument([string] $strCmdName, [string] $strParamName, [string] $strWordToComplete, [System.Management.Automation.Language.CommandAst] $cmdAst, [System.Collections.IDictionary]$mapFakeBoundParameters)
{
return (([TimeZoneInfo]::GetSystemTimeZones() + $global:mapCommonTimeZones.Values) | Where-Object { $_.Id -like "$strWordToComplete*" -or $_.DisplayName -like "$strWordToComplete*" -or $_.DaylightName -like "$strWordToComplete*" -or $_.StandardName -like "$strWordToComplete*" } | ForEach-Object { Write-Output (new System.Management.Automation.CompletionResult $_.Id) });
}
}
function Get-AllTimeZones
{
[OutputType([TimeZoneInfo[]])]
param
(
[Parameter(Position = 1, ValueFromRemainingArguments, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[ArgumentCompleter([TimeZoneCompleter])]
[string[]]
$astrPartialTimeZoneInfo
)
Write-Output ($PSBoundParameters.ContainsKey('astrPartialTimeZoneInfo') ? ($astrPartialTimeZoneInfo | ForEach-Object { $curInfo = $_; $mapCommonTimeZones.Contains($_) ? $mapCommonTimeZones[$_] : ([System.TimeZoneInfo]::GetSystemTimeZones() | Where-Object { $_.Id -like "$curInfo*" }).Id }) : [System.TimeZoneInfo]::GetSystemTimeZones());
}
function Convert-TimeZones
{
[OutputType([datetime])]
param
(
[Parameter(Mandatory, Position = 0)]
[DateTime]
$dateFrom,
[Parameter(Mandatory, Position = 1, ParameterSetName = 'TZ_TZ')]
[Parameter(Mandatory, Position = 1, ParameterSetName = 'TZ_STR')]
[ValidateNotNull()]
[System.TimeZoneInfo]
$timezoneFrom,
[Parameter(Mandatory, Position = 2, ParameterSetName = 'TZ_TZ')]
[Parameter(Mandatory, Position = 2, ParameterSetName = 'STR_TZ')]
[ValidateScript({ $_ -ne $null -and $_ -ne $timezoneFrom })]
[System.TimeZoneInfo]
$timezoneTo,
[Parameter(Mandatory, Position = 1, ParameterSetName = 'STR_STR')]
[Parameter(Mandatory, Position = 1, ParameterSetName = 'STR_TZ')]
[ValidateScript({ $_ -ne $null -and $_ -ne '' -and (Get-AllTimeZones $_).length -ge 0 })]
[ArgumentCompleter([TimeZoneCompleter])]
[string]
$strFromTimeZone,
[Parameter(Mandatory, Position = 2, ParameterSetName = 'STR_STR')]
[Parameter(Mandatory, Position = 2, ParameterSetName = 'TZ_STR')]
[ValidateScript({ $_ -ne $null -and $_ -ne '' -and (Get-AllTimeZones $_).length -ge 0 })]
[ArgumentCompleter([TimeZoneCompleter])]
[string]
$strToTimeZone
)
begin
{
$timezoneFrom = $PSBoundParameters['timezoneFrom'] ? $timezoneFrom : (Get-TimeZone $strFromTimeZone);
$timezoneTo = $PSBoundParameters['timezoneTo'] ? $timezoneTo : (Get-TimeZone $strToTimeZone);
}
process
{
Write-Output ([TimeZoneInfo]::ConvertTime($dateFrom, $timezoneFrom, $timezoneTo));
}
}
Since you're using PowerShell, why not stick to writing the argument completer in PowerShell, too?
Does this help? about_functions_argument_completion
Your script isn't parsing correctly for me. PS 5.1 on Win10.
@MotoX80 : He's using PowerShell 7. All those ternary operators (i.e., "?" and ":") make PS5 barf.
Thx Rich. You can handle this one. I don't have 7 installed anywhere.
Sign in to comment