PowerShell: Use parameter checking with no exception

I would call a PowerShell script as follows:
script.ps1 -path mypath\to\files\ -days 6 -hours 0

To test the command line arguments, I can either do this manually or rely on the param syntax:

 Param ( [Parameter(Mandatory=$true )] [string] $path, [Parameter(Mandatory=$false)] [int] $days, [Parameter(Mandatory=$false)] [int] $hours ) 

If I use the param syntax:

  • the parameter definition should be the first line in the script (excluding comments). Ok no problem for me

  • in case of incorrect parameters, I can not catch the error (for example, to display a custom error message)

I want to display my own error message when calling a script with incorrect parameters.
Is it possible and how to eliminate the exception in case of a parameter error?

+4
source share
5 answers

So well, you cannot use param AND to catch related exceptions.

+3
source

Examples for functions (simple and advanced), but the same idea should work for param scripts:

 # Simple function. # Everything not declared in `param` goes to $args. # If $args is not empty then there are "invalid" parameters or "unexpected" arguments function test { param ( [string]$path, [int]$days, [int]$hours ) # check $args and throw an error (in here we just write a warning) if ($args) { Write-Warning "Unknown arguments: $args" } } 

or

 # Advanced function. # For an advanced function we can use an extra argument $args # with an attribute `[Parameter(ValueFromRemainingArguments=$true)]` function test { param ( [Parameter(Mandatory=$true )] [string] $path, [Parameter(Mandatory=$false)] [int] $days, [Parameter(Mandatory=$false)] [int] $hours, [Parameter(ValueFromRemainingArguments=$true)] $args ) # check $args and throw an error (in this test we just write a warning) if ($args) { Write-Warning "Unknown arguments: $args" } } 

Next test:

 # invalid parameter test -path p -invalid -days 5 # too many arguments test -path p 5 5 extra 

in both cases produces the same conclusion:

 WARNING: Unknown arguments: -invalid WARNING: Unknown arguments: extra 
0
source

A possible workaround is to wrap your actual function in another. Something like a private / public relationship. Example:

 function Example-Private { [CmdletBinding()] Param ( [ValidateNotNullOrEmpty()] [string]$Arg1, [ValidateNotNullOrEmpty()] [string]$Arg2 ) # Do what you need } function Example-Public { [CmdletBinding()] Param ( [string]$Arg1, [string]$Arg2 ) try { Example-Private $Arg1 $Arg2 } catch { # Display a user-friendly message, save exception into a log file, etc. } } 

If you are working on a module, you can see here how to export your public functions and hide private ones: Export Powershell Functions

0
source

In the Begin block, you can always perform additional parameter checking, but if the parameter is incorrect, I think that you will not want to continue execution. That is, you want to throw the final error. Here is an example:

 param ( [Parameter(Mandatory=$true )] [string] $path, [Parameter(Mandatory=$false)] [int] $days, [Parameter(Mandatory=$false)] [int] $hours ) Begin { if ($hours -lt 0 -or $hours -gt 23) { throw "Hours parameter must be between 0 and 23" } } 

However, I'm not sure what is better than using the built-in PowerShell parameter validation function, for example:

 param ( [Parameter(Mandatory=$true )] [string] $path, [Parameter(Mandatory=$false)] [int] $days, [Parameter(Mandatory=$false)] [ValidateRange(0,23)] [int] $hours ) 
-1
source

you can do this using the param syntax if you add a dummy parameter of a string type with the ValueFromRemainingArguments property. Then you can check this dummy parameter in your script and take appropriate actions, for example:

 Param( [Parameter(Mandatory=$false)] [SWITCH]$myparam1, [Parameter(Mandatory=$false)] [SWITCH]$myparam2, [parameter(Mandatory=$false,ValueFromRemainingArguments=$true)] [STRING]$dummy ) if ($dummy -eq anythingYouDontLike) throwAMessageOrSomething. 
-1
source

Source: https://habr.com/ru/post/1434900/


All Articles