I want parameters ONLY through alias:
script.ps1 -d site1 -c eac34b2d
This shouldnt be allowed:
script.ps1 site1 eac34b2d
Param(
[parameter(Mandatory=$true)]
[alias("d")]
$DocRoot,
[alias("c")]
$Commit
)
Basically you want to disable positional parameter binding. I wrote about a built-in way in Powershell 3.0: use [CmdletBinding(PositionalBinding=$false)].
A workaround for v2 is available here:
Specify [CmdletBinding(PositionalBinding=$false)] before the param() block.
(This should work for functions as well as script params.)
The docs for Powershell 7.5 state:
In functions that have the
CmdletBindingattribute, unknown parameters and positional arguments that have no matching positional parameters cause parameter binding to fail.
When PositionalBinding is $false, function parameters are not positional by default. Unless the Position argument of the Parameter attribute is declared on the parameter, the parameter name (or an alias or abbreviation) must be included when the parameter is used in a function.
The Position argument of the Parameter attribute takes precedence over the PositionalBinding default value. You can use the Position argument to specify a position value for a parameter.