1

How can I make $m switch accept one argument but $prebuild doesn't need any argument.

Param (
 [Parameter(Mandatory=$true)]
 [Switch]$m,
 [Switch]$prebuild
)

The script would be executed as below but I get the following error.

.\test.ps1 -m java
A positional parameter cannot be found that accepts argument 'java'

1 Answer 1

4

Change $m to a [string]:

param(
  [string]$m,
  [switch]$prebuild
)

To test whether an argument was passed by the caller, use the $PSBoundParameters automatic variable:

if($PSBoundParameters.ContainsKey('m')){
  # An argument was passed to `-m`
}

(This last part is unnecessary if you keep the [Parameter(Mandatory=$true)] attribute)

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

Comments

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.