1

For my script I have some parameters like such:

[Alias("f")]
[string]$file

When the user enters this at command-line: "./script.ps1 -f"

It comes up with an error >"Missing an argument for parameter...." I would like to handle this error myself, the default error is technical, and messy.

I would like to make it user friendly and readable, anyway? Every suggestion I have read has said to make it Mandatory.

But it is NOT a mandatory command, what I am trying to accomplish is if someone provides the parameter tag (AKA "-file"), but fails to provide a value for it.

Dylan Holmes
  • 83
  • 1
  • 5
  • `./script.ps1` is the way to run it _without_ a value for `-f` (i.e. don't even specify the `-f`) – gvee Apr 19 '18 at 07:37
  • @gvee Yes, I know but a user/customer might not know that, and enter "-f" with no value, and in that case I want to provide a custom error message. – Dylan Holmes Apr 19 '18 at 09:00
  • I believe that error is coming from the PS parser seeing an issue, not your code seeing an issue, but I'm not positive. If its coming from your script, then maybe a trap would work. edit: tried it with a trap, but its coming from the parser before the trap gets loaded. – Sambardo Apr 19 '18 at 14:13
  • Yea, and if you put the trap above the Param() block, it doesn't error out due to the invalid parameter, but then it errors out because param isn't the first call in the file. – Dylan Holmes Apr 19 '18 at 17:42

2 Answers2

2

You can use the [AllowNull()] paremeter attribute then check and handle your self.

Advanced Fuction parameter validation

[Cmdletbinding()]
Param
(
    [Alias("f")]
    [AllowNull()]
    [string]$file
)
Begin
{
    If($PSBoundParameters.ContainsKey('file') -and -not $file)
    { 
       Write-error 'ouch my user friendly error'
    }
}
Process{}
End{}

Note: the begin, process, end blocks are not needed but are a good standard to follow. Read more about them here and here.

The downside is this still requires a $null or '' parameter value to be passed.

If you start to look into this there are tons of posts about people attempting a similar goal.

1 - passing-null-to-a-mandatory-parameter-to-a-function

2 - PS Git Issue - 4616

3 - PS Git Issue - 4208

4 - powershell-param-statement-error-trap

5 - alternative-to-throwing-param-exceptions-in-powershell

jkdba
  • 2,378
  • 3
  • 23
  • 33
0

What you want is using the [Switch] parameter. Switch parameters are parameters with no parameter value. They're effective only when they're used and have only one effect.

You can verify your script's needs with get-help. In your case it would show something like this:

get-help .\script.ps1
script.ps1 [[-file] <Object>]

But if you defining your parmeters like this:

Param
(
    [Alias("f")]
    [Switch]$file
)

get-help will now show that no object is needed:

get-help .\script.ps1
script.ps1 [-file]

A complete example would look something like this:

Param
(
    [Alias("f")]
    [Switch]$file
)
If($file) { 
    Write-Host '-f Switch enabled'
}
Else {
    Write-Host 'Missing -f Switch'
}

You can now call you script like .\script.ps1 -file or like .\script.ps1 -f and the output in the console would be '-f Switch enabled'

Magnus
  • 1