Here is my PowerShell test script:
param(
[Parameter(Mandatory=$true,
Position=0)]
[int]$val
)
Write-Output $val;
Now I want to call it like this:
Powershell -ExecutionPolicy Bypass -File ".\testscript.ps1" "-1"
The error I get is:
A parameter cannot be found that matches parameter name '1'.
Note that I have no control over the way the application calls my script (i.e. I can not make the application call my script with -val:-1
). So binding has to be positional. How can I make my script bind -1
to $val
with the param
block?
If you can't fix how the application calls the script the only other option I see is to drop the named parameters entirely and use the
$args
collection instead:By casting the argument to
[int]
type safety is still enforced: