Dynamic switches before positional parameters may not work as expected
Votes from Connect: 4
Original Date Submitted: 8/31/2014 12:30:14 PM
Handle: Roman Kuzmin
Site Name: PowerShell
Feedback ID: 960194
Frequency: Always Happens
Regression: Yes, this happens in all previous versions
Here is the code that demonstrates the problem. The cmdlet Set-Location has the
dynamic switch ReadOnly if the provider is FileSystem.
# provider that does not have the dynamic ReadOnly
# 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:\
Normally position of a switch parameter in a command does not matter. This not
the case for a dynamic switch. The case 2 fails with the error:
Get-ChildItem : A parameter cannot be found that matches parameter name 'ReadOnly'.
I think that at the moment of dynamic parameters creation it is not yet known
that ReadOnly is the switch. Thus, PowerShell treats it as a regular parameter
with its argument C:\ and C:\ therefore is not treated as a positional
parameter. As a result, Get-ChildItem thinks that the location is not specified
and uses the current env:. The provider Environment does not provide the
dynamic switch ReadOnly, so that finally the command fails due to incorrect
syntax, even though it is somewhat correct (the same command works if the
current provider is FileSystem).
In the example above a command fails due to a dynamic switch before a
positional parameter. Yet another class of problem is "swallowed parameters",
i.e. a command does not fail due to invalid syntax but proceeds with incorrect
positional parameters where some are swallowed by preceding dynamic switches
and the others are shifted accordingly.
Hypothetical example: imagine in the code above the provider Environment also
provides the dynamic switch ReadOnly. The case 2 would not fail but proceed
with the incorrect location (C:\ is swallowed and the default current env:\ is
Real example: Invoke-Build issue #4
Product Studio item created by Connect Synchronizer due to creation of feedback ID 960194 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=960194).
Internal BugId: 8980