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 )
source share