0

Lets say I have a cmdlet:

function Set-Something
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string] $SomeValue
    )
}

and some automation that calls my cmdlet:

Set-Something

This will make the powershell session halt and write this to screen:

cmdlet Set-Something at command pipeline position 1 Supply values for the following parameters: SomeValue:

This is downright annoying when doing automation: What we really want is for powershell to not Halt forever expecting user input that will never come, instead we simply want it to throw an exception "Missing parameter in call to Set-Something".

Is this possible?

2 Answers 2

2

While removing [Parameter(Mandatory)] as Avshalom suggests works, another solution which will retain the self-documenting benefit, may be to run PowerShell non-interactively.

Use -Noninteractive to launch PowerShell in non-interactive mode. You should then get an error which respects [Parameter(Mandatory)].

Set-Something : Cannot process command because of one or more missing mandatory parameters: SomeValue.
At line:1 char:1
+ Set-Something
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Something], ParameterBindingException
    + FullyQualifiedErrorId : MissingMandatoryParameter,Set-Something
Sign up to request clarification or add additional context in comments.

Comments

2

Just remove the [Parameter(Mandatory)] part and validate it inside the function:

function Set-Something
{
    [CmdletBinding()]
    param(
        [string] $SomeValue
    )

    if (!$SomeValue)
    {
    throw "Missing parameter in call to Set-Something"
    }
}

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.