#1 21 Jun 2019 14:49

Cornbeetle
New Member
Registered: 05 Oct 2016
Posts: 3

How use a variable as the argument in the 'ValidateRange' attribute

For example, can I do this?

$i1 = 1
$i2 = 10

[ValidateRange($i1,$i2)]$number = Read-Host "Enter number 1-10"

Offline

#2 21 Jun 2019 19:25

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,031
Website

Re: How use a variable as the argument in the 'ValidateRange' attribute

You can do that with ValidateScript() rather than ValidateRange()

$i1 = 1
$i2 = 10

[int32][ValidateScript( {
($_ -ge $i1) -AND ($_ -le $i2)
}
)]$number = Read-Host "Enter number 1-10"

The script block just has to return TRUE or FALSE

Offline

Board footer

Powered by FluxBB