0

Here's my script (test.ps1):

[CmdLetBinding()]

Param
(
    [parameter(Mandatory=$true)][string]$environment,
    [switch][bool]$continue=$true
)

Write-Host $environment
Write-Host $continue

Question:
If I invoke this script by giving an argument which is a substring of the parameter I specified in the script like this: PS> .\test.ps1 -envi:blah, PowerShell doesn't seem to check the argument name. I want PowerShell to enforce parameter spelling, i.e., it should only accept -environment which matches the parameter name in the script. For anything else, it should raise an exception. Is that doable? How do I do that?
Thanks.

2
  • PowerShell is checking the parameter names. It matches names to their function parameters as long as there is no ambiguity. Select -Ex could mean -ExpandProperty or -ExcludeProperty. -Exp and -Exc, while not very descriptive, only have one parameter they could match each. PowerShell is only trying to do you a favour but wont allow a mistake to occur. Select-Object : Parameter cannot be processed because the parameter name 'ex' is ambiguous Commented Jan 6, 2015 at 2:52
  • @Matt I never thought this actually is a PowerShell feature to make the input easier. Good to know. Commented Jan 6, 2015 at 4:31

1 Answer 1

1

It's not pretty, but it will keep you from using anything except -environment as a parameter name.

Param
(
    [parameter(Mandatory=$true)][string]$environment,
    [parameter()]
     [ValidateScript({throw "Invalid parameter. 'environment' required."})]
     [string]$environmen,
    [switch][bool]$continue=$true
)


Write-Host $environment
Write-Host $continue
}

Edit: As Matt noted in his comment the automatic disambiguation will force you to specify enough of the parameter name to find a unique substring match. What I'm doing here is basically giving it a parameter that satisfies all but the last character to prevent using any substring up to the last character (because it's ambiguous), and throwing an error to prevent you from using that.

And, FWIW, that could well be the ugliest parameter validation I've ever done but I don't have any better ideas right now.

Sign up to request clarification or add additional context in comments.

4 Comments

Might be worth it to explain about the parameter disambiguation that PowerShell does as a favour to the end user. You, in this case, are working against it to satisfy the needs of the op. At least it works.
@mjolinor This is not an elegant solution but it does the trick. Thanks.
@itnovice I know. I was hoping someone else would come up with a better solution than that. I think of a better solution I'll update the answer.
Output of Get-Command -Syntax and Get-Help will look strange with this extra parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.