Hello everyone,
I just started to learn Powershell and I’m currently learning the very basics.
I tried to read some user input via the Read-Host function which I wanted to check for numbers between 1-10.
The check kind of works but I always get an error on the console because of the [int] cast that I’m trying to do (I did this because else I’m not able to deny the use of negative numbers since I guess that the user input is always stored in a string if there is no variable cast?). The script will not stop, it will only display the cast error.
This is what I tried:
$TestVar = Read-Host -Prompt “Please enter a number between 1 and 10”
#“Error check”
while(([int]$TestVar -lt “1”) or ([int]$TestVar -gt “10”))
{
Write-Host “Error, please try again.r
n”
$TestVar = Read-Host -Prompt “Please enter a number between 1 and 10”
}
Now the problem with this code is that it seems to work but I always receive an error on the console because of the [int] cast in the while loop. I tried to google for a solution but I only found some more complicated functions that did not help me.
I found the option -isnot [int] but somehow I was not able to get it to work (if I tried to enter a letter in the prompt it did not jump in the -isnot condition of a test if block.
Did I not understand the -isnot parameter correctly or can I even use it in my case like if($TestVar -isnot [int]) {…}? This if is never triggered, it is triggered when I use double but it does not solve my problem since somehow it will always trigger all other conditions that I tried out then.
Thank you for any explanation and best regards!
Look at these examples of comparison operators. Take a look at these comparison operators:
PS C:\WINDOWS\system32> $var = 5
#Comparison operator similar to SQL IN
$var -in 1..10
#Contains is a array comparison, the array contains this var
1..10 -contains $var
True
True
PS C:\WINDOWS\system32> $var = R
#Comparison operator similar to SQL IN
$var -in 1..10
#Contains is a array comparison, the array contains this var
1..10 -contains $var
$var = 5
#Comparison operator similar to SQL IN
$var -in 1..10
#Contains is a array comparison, the array contains this var
1..10 -contains $var
False
False
PS C:\WINDOWS\system32> $var = 11
#Comparison operator similar to SQL IN
$var -in 1..10
#Contains is a array comparison, the array contains this var
1..10 -contains $var
False
False
PS C:\WINDOWS\system32>
As you stated, -IS and -ISNOT are not applicable. Basically, they are checking the type, so if you wanted to see if the input was a number you could use this operator. Using the above operator is generating a range and verifying the number is between it, which should work well for what you are doing.
Thank you for your reply,
I tried to get my code to work with the examples that you provided but it does not work for me.
I’m reading user input via Read-Host -Prompt and if I try to enter a letter the code will exit and tell me that the input cannot be converted to System.Int32
I’d like to check if the input is correct (number between 1-10) but the script should not terminate with an error if it is not correct. If it is false I want to ask for a new input.
Thank you for your help.
There are a couple of ways to write the loop, but here is a basic example"
do {
$var = Read-Host -Prompt 'Enter a number between 1 and 10'
if (1..10 -notcontains $var) {
'Nope, {0} is not a number between 1 and 10, try again.' -f $var
}
}
while (1..10 -notcontains $var)
'You entered {0}' -f $var
The loop exits when all conditions are True.
'You entered {0}' -f $var
Enter a number between 1 and 10: r
Nope, r is not a number between 1 and 10, try again.
Enter a number between 1 and 10: g
Nope, g is not a number between 1 and 10, try again.
Enter a number between 1 and 10: 6
You entered 6
First, your while loop contains or when it should be -or
Second, you don’t need the extra parentheses, this is perfectly fine.
while([int]$TestVar -lt "1" -or [int]$TestVar -gt "10")
Third, if you want it to not error when something other than an integer is entered, do NOT force the cast to int. Instead, you can use the -as operator instead. It will try to convert to int but will not error if it can’t. Please see the updated code
$TestVar = (Read-Host -Prompt "Please enter a number between 1 and 10") -as [int]
# "Error check"
while($TestVar -lt "1" -or $TestVar -gt "10")
{
Write-Host “Error, please try again. r n”
$TestVar = Read-Host -Prompt "Please enter a number between 1 and 10"
}
Thank you for all answers, they helped me a lot to understand some basics.
@Rob Simmers: Thank you for the examples, I was able to test them with my code and they worked as intended. I was also able to get your first examples to run, there was some kind of problem with my PS environment (I think I tested to much and therefore the variables have not been cleated correctly. It showed errors on my first client where I did all the testing but there were no errors with another client. After restarting the PS environment everything worked…). Thanks for introducing me to the contains/notcontains options, they will come in handy again.
@Doug Maurer: Thank you for pointing out that the code that I posted works too. It actually worked after restarting my PS environment. I think I did too much testing and changed the code too much? After a restart it all worked (shame on me). Sorry that I forgot the - before the or, I typed the code without copying it and forgot the - sign. Thank you for telling me about the -as operator. This will help me with many other parts/error checks that I’m trying out.
Thank you guys for your help, I really appreciate all of your explanations since I’m just starting to learn.