Description
- 'Get-AllowedEncoding' function relies on 'Out-File', but it could be overloaded - for example with WindowsOutFilePatch
- Scriptblock parameter of Register-ArgumentCompleter for 'Export-Ini' missed param block, so $wordToComplete is always empty string
Steps To Reproduce
In PowerShell console:
PS> Export-Ini -Encoding a
press Tab
Result:
PS> Export-Ini -Encoding unknown
Expected behaviors
In PowerShell console:
PS> Export-Ini -Encoding a
press Tab
Result:
PS> Export-Ini -Encoding ascii
Your Environment
PsIni 4.0.1
PSVersion 5.1.14409.1029
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1029
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Possible Solution
function Get-AllowedEncoding {
# targeting built-in Cmdlet
$command = Get-Command -Name Microsoft.PowerShell.Utility\Out-File
if ($PSVersionTable.PSVersion.Major -ge 6) {
(
$command.Parameters['Encoding'].Attributes |
Where-Object { $_ -is [ArgumentCompletions] }
)[0].CompleteArgument('Out-File', 'Encoding', '*', $null, @{ }).CompletionText
}
else {
(
$command.Parameters['Encoding'].Attributes |
Where-Object { $_.TypeId -eq [ValidateSet] }
)[0].ValidValues
}
}
$EncodingCompleter = {
# missed param block
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
Get-AllowedEncoding |
Where-Object { $_ -like "$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_,
$_,
[System.Management.Automation.CompletionResultType]::ParameterValue,
$_
)
}
}
# Using the same completer for both functions
Register-ArgumentCompleter -CommandName Export-Ini -ParameterName Encoding -ScriptBlock $EncodingCompleter
Register-ArgumentCompleter -CommandName Import-Ini -ParameterName Encoding -ScriptBlock $EncodingCompleter
Description
Steps To Reproduce
In PowerShell console:
PS> Export-Ini -Encoding a
press Tab
Result:
PS> Export-Ini -Encoding unknown
Expected behaviors
In PowerShell console:
PS> Export-Ini -Encoding a
press Tab
Result:
PS> Export-Ini -Encoding ascii
Your Environment
PsIni 4.0.1
PSVersion 5.1.14409.1029
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1029
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Possible Solution