Here is the code that demonstrates the problem. The Set-Location cmdlet has a ReadOnly dynamic switch if the provider is a FileSystem .
The normal position of the switch parameter in the command does not matter. This is not the case for the dynamic switch. Case 2 fails with an error:
Get-ChildItem : A parameter cannot be found that matches parameter name 'ReadOnly'.
What's happening? I think that at the time of creating the dynamic parameters, it is still unknown that ReadOnly is a switch. Thus, PowerShell treats this as a regular parameter with its argument C:\ and C:\ , therefore it is not considered as a positional parameter. As a result, Get-ChildItem considers that the location is not specified and uses the current env: The Environment provider does not provide a dynamic ReadOnly switch, so finally the command does not work due to incorrect syntax, although it is somewhat correct (the same command works if the current FileSystem provider).
Questions:
- Do I understand the problem correctly?
- Is this feature documented somewhere?
- Is there a workaround?
The last question is more about developing custom commands with dynamic parameters. The problem was originally noticed and described as Invoke-Build Issue # 4 . At the moment, this problem has just been documented. But I'm still interested in workarounds.
conclusions
- The described problem exists.
- It is not documented as such.
- Ways to work, each solves a problem:
- Specify dynamic switches after position parameter arguments
- Explicit dynamic switch arguments:
-ReadOnly:$true - Do not use positional parameters with dynamic switches, i.e. specify parameter names.
Opened error: 960194
source share