I am trying to make a variable equal to a range. My script includes user input and this input has to fall within a certain range. I will be using the range a couple of times in the script so I want it to be a variable. This is what I have tried:
$serverrange = "1..18"
and I've tried
$serverrange = 1..18
I have googled it but I can't find much about this. The problem is PowerShell doesn't see this as a range, it sees it as 1..18 literally. So if I do Write-Output $serverrange it display as "1..18". So my question is how do I get it to see it as a range or how do I change the script to incorporate the range? Here is the rest of the script:
#user input for how many servers will be tested
$numofservers = Read-Host -Prompt "How many servers are being tested?"
if($numofservers -eq $serverrange) {
"Proceed"
}
else {
"Server limit exceeded"
}
Edit: Didn't realize that $serverrange = 1..18 worked.
As you've since realized, an unquoted expression such as 1..18 works perfectly fine: it uses PowerShell's range operator (..) to create an array of contiguous integers from 1 to 18; i.e., it is the equivalent of the following array literal:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
By contrast, "1..18" is an (expandable) string whose content prints as-is (literal 1..18, in this case, because the string contains neither variable references nor subexpressions).
Important: While it technically works to use Invoke-Expression to evaluate a string as a PowerShell statement (e.g., Invoke-Expression "1..18"), this approach should be avoided: Invoke-Expression is rarely the right tool and presents a security risk, particularly with unsanitized user input - see the PowerShell-team blog post entitled "Invoke-Expression considered harmful".
Your challenge seems to be create such a range based on user input, which is by default a string, as returned by Read-Host.
The range operator is capable of dynamically converting either operand (range endpoint) to an integer, and happily accepts expressions as the range endpoints:
1..(Read-Host 'Enter the upper bound')
If you enter 3 (which, as stated, is returned as a string), PowerShell dynamically converts that string to its integer equivalent and creates array 1..3, whose default output looks like this:
1
2
3
That said, in order to (a) provide user-friendly prompting that (b) enforces a predefined range of inputs (numbers), more work is needed.
Therefore:
Ensure that what the user enters can be converted to (a) a number that (b) falls within the expected range; if not, prompt again.
Once valid input has been received, use PowerShell's range operator (..) to construct the array of indices, as desired.
# Define the implied lower bound and the maximum upper bound.
$lowerBound = 1
$maxUpperBound = 18
# Prompt the user for the upper bound of the range, with validation,
# until a valid value is entered.
do {
$userInput = Read-Host -Prompt "How many servers are being tested?"
if (
($upperBound = $userInput -as [int]) -and
($upperBound -ge $lowerBound -and $upperBound -le $maxUpperBound)
) {
break # valid value entered, exit loop.
} else {
# Invalid input: Warn, and prompt again.
Write-Warning "'$userInput' is either not a number or is outside the expected range of [$lowerBound,$maxUpperBound]."
}
} while ($true)
# Create the array of indices based on user input.
1..$upperBound
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With