Alternative to Throwing Param Exceptions in PowerShell? -
bottom line front
i'm looking method validate powershell (v1) command line parameters without propagating exceptions command line.
details
i have powershell script uses param
in conjunction [validatenotnullorempty]
validate command line paramaters:
param( [string] [validatenotnullorempty()]$domain = $(throw "domain (-d) param required.") )
we're changing paradigm of error handling no longer want pass exceptions command line, rather provide custom error messages. since param
block can not wrapped in try catch block, i've resorted like following:
param( [string]$domain = $("") ) try{ if($domain -like $("")){ throw "domain (-d) param required." } ... }catch{ #output error message }
my concern we're bypassing of built-in validation available using param
. new technique reasonable solution? there better way validate command line params while encapsulating exceptions within script? i'm interested in see how powershell professionals handle situation.
any advice appreciated.
as mentioned in comment: more read description, more come conclusion should not worry "bypassing built-in validation". why? because that's exactly target. want bypass it's default behavior, if that's need , have - it. ;)
Comments
Post a Comment