Question:
Given a script foo.ps1: param($x,$y) return $x/$y
Is it possible to enforce explicit parameter naming when calling it?
./foo.ps1 5 10 would generate an error
./foo.ps1 -x 5 -y 10 would be OK
Answer:
This code works but it uses something not documented (I could not find anything about negative positions):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function Test { param( [Parameter(Position=-1)] $x , [Parameter(Position=-1)] $y ) $x/$y } Test -x 1 -y 2 Test -y 2 -x 1 Test 1 2 |
Output:
1 2 3 4 5 |
0.5 0.5 Test : Cannot bind positional parameters because no names were given. At C:\TEMP\_110127_170853\q1.ps1:15 char:5 |