MacAddress parameter validation requires Try-Catch on calling script #1

Closed
opened 2021-09-05 18:47:31 -06:00 by asif · 1 comment
Owner

Currently, MacAddress is checked using PowerShell integrated parameter validation against a RegEx. This requires any calling script to wrap calls to this function in Try...Catch blocks in case an invalid MAC address is passed.

DETAIL: Parameter validation is run by the command processor before Get-Process invocation. Validation failures throw a ParmeterBindingValidationException. This cannot be handled by -ErrorAction and does not register in -ErrorVariable because the process (Get-Process) does not even start! As such, only Try...Catch can handle this error. There is nothing wrong with this but it is not intuitive and makes calling the function awkward. Hence, this is an enchancement, not really a bug.

SOLUTION: Move validation before MAC address processing within the script itself and out of the param block. Register errors with Write-Error and skip further processing. This will allow the calling script to handle errors as desired via -ErrorAction and will register with -ErrorVariable.

Currently, `MacAddress` is checked using PowerShell integrated parameter validation against a RegEx. This requires any calling script to wrap calls to this function in Try...Catch blocks in case an invalid MAC address is passed. **DETAIL**: Parameter validation is run by the command processor *before* Get-Process invocation. Validation failures throw a *ParmeterBindingValidationException*. This **cannot** be handled by `-ErrorAction` and does not register in `-ErrorVariable` because the process (Get-Process) does not even start! As such, only Try...Catch can handle this error. There is nothing wrong with this but it is not intuitive and makes calling the function awkward. Hence, this is an enchancement, not really a bug. **SOLUTION**: Move validation before MAC address processing within the script itself and out of the param block. Register errors with Write-Error and skip further processing. This will allow the calling script to handle errors as desired via `-ErrorAction` and *will* register with `-ErrorVariable`.
asif added this to the Version 2.0 milestone 2021-09-05 18:47:31 -06:00
asif added the
enhancement
label 2021-09-05 18:47:31 -06:00
asif self-assigned this 2021-09-05 18:47:31 -06:00
Author
Owner

Closed by d5c3c071cd

Closed by d5c3c071cd
asif closed this issue 2021-09-05 18:51:35 -06:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: asif/ps-cmdlet-wol#1
No description provided.