Dynamic switches to positional parameters may not work as expected

Here is the code that demonstrates the problem. The Set-Location cmdlet has a ReadOnly dynamic switch if the provider is a FileSystem .

 # provider that does not have the dynamic -ReadOnly Set-Location env: # case 1: works because we explicitly specify FileSystem Get-ChildItem C:\ -ReadOnly # case 2: fails even though we explicitly specify FileSystem Get-ChildItem -ReadOnly C:\ 

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

+6
source share
1 answer

Your understanding is absolutely correct.

Parameter binding is not documented, as it is extremely complex. The language specification ( http://www.microsoft.com/en-us/download/details.aspx?id=36389 ) is probably the best documentation we have, but it’s incomplete and I don’t think it covers this situation.

The only workaround I can think of is to provide an argument to the switch parameter, e.g.

 Get-ChildItem -ReadOnly:$true C:\ 

Feel free to open the error. It will probably not be fixed, but at least gives the team the opportunity to discuss this.

I would suggest that the correction would be something like "if the parameter binding fails and there are dynamic parameters, go back and assume that the unknown parameters are switch parameters and try again." This can be done with one unknown parameter at a time, or all at once, in any case, parameter binding can be very slow if there are many unknown parameters.

+3
source

Source: https://habr.com/ru/post/974524/


All Articles