Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Validation works with _every_ reference to a $Var inside an “Advanced Function”

$
0
0

howdy y’all,

i was playing with a days-in-month function and needed to handle the wrap around for adding 1 to month number 12. the easiest way to do it seemed to be to set the $Month to 0 and then let it increment.

i got a nasty red error msg “The variable cannot be validated because the value 0 is not a valid value for the Month variable” while in the body of the function.

i had NO FREAKING IDEA that validation occurred anywhere other than in the parameter block.

here’s some example code …

function Test-ValidationInFunction { [CmdletBinding()] Param ( [Parameter( Mandatory, Position=0 )] [ValidateRange(1, 12)] [int] $TestNumber ) Begin {} Process { if ($TestNumber -eq 12) { $TestNumber = 0 } $TestNumber } End {} } foreach ($Number in 1..12) { Test-ValidationInFunction $Number } 

has that always been that way?

take care,
lee

submitted by /u/Lee_Dailey
[link] [comments]

The post Validation works with _every_ reference to a $Var inside an “Advanced Function” appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles