When IDynamicParameters.GetDynamicParameters() gets called via tab completion, the parameters of the cmdlet are not fully parsed, i.e. if a user specifies e.g. Parameter1via commandline as -Parameter1 'myparameter' using either single or double quotes, then the quotes are still present in the C# string. This does not happen at the point of execution. Is there a better workaround to replacing the quotes by calling some other internal method instead? Is this an issue with the TabExpansion2 method or PSReadLine or something else?
Steps to reproduce
First Compile the following in a classic .Net library project and then import the dll into PowerShell using Import-Module
using System;
using System.Management.Automation;
namespace ClassLibrary1
{
[Cmdlet("Test", "TabCompletion")]
public class TestCmdlet : Cmdlet, IDynamicParameters
{
[Parameter(Mandatory = true)]
public string Parameter1 { get; set; }
public object GetDynamicParameters()
{
if (!string.IsNullOrEmpty(Parameter1))
{
if (Parameter1.StartsWith("\"") || Parameter1.StartsWith("\'"))
{
throw new Exception("Bug: PowerShell has not fully parsed already given input parameter and left the quotes from the command line input");
}
}
// Here would be some logic to return a dynamic parameter based on the input of non-dynamic parameters
return null;
}
protected override void ProcessRecord()
{
WriteObject(Parameter1);
}
}
}
Import-Module $pathToCompiledDll
# Attach VS debugger to powershell process
Test-TabCompletion -Parameter1 "test" # do not execute this line but press 'tab' to invoke tab completion
Expected behavior
Parameter1 should have the value test (as observed in the VS debugger since GetDynamicParameters suppress any raised exceptions)
Actual behavior
Parameter1 has the value "test"
Notes: Similarly, single quotes remain as well. If one would have used an interpolated string like "test$number", then one would get the value "test$number", whereas "test$($number)"
would at least give "test1"
This bug also occurs in Windows PowerShell 5.1
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.0.0-beta.8
PSEdition Core
GitCommitId v6.0.0-beta.8
OS Microsoft Windows 6.3.9600
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
When
IDynamicParameters.GetDynamicParameters()gets called via tab completion, the parameters of the cmdlet are not fully parsed, i.e. if a user specifies e.g.Parameter1via commandline as-Parameter1 'myparameter'using either single or double quotes, then the quotes are still present in the C# string. This does not happen at the point of execution. Is there a better workaround to replacing the quotes by calling some other internal method instead? Is this an issue with theTabExpansion2method orPSReadLineor something else?Steps to reproduce
First Compile the following in a classic .Net library project and then import the dll into PowerShell using
Import-ModuleExpected behavior
Parameter1should have the valuetest(as observed in the VS debugger sinceGetDynamicParameterssuppress any raised exceptions)Actual behavior
Parameter1has the value"test"Notes: Similarly, single quotes remain as well. If one would have used an interpolated string like
"test$number", then one would get the value"test$number", whereas"test$($number)"would at least give
"test1"This bug also occurs in Windows PowerShell 5.1
Environment data