Question regarding input check in Powershell

Welcome Forums General PowerShell Q&A Question regarding input check in Powershell

Viewing 5 reply threads
  • Author
    Posts
    • #256379
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      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. rn”

      $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!

       

    • #256424
      Participant
      Topics: 15
      Replies: 1765
      Points: 3,178
      Helping Hand
      Rank: Community Hero

      Look at these examples of comparison operators. Take a look at these comparison operators:

      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.

    • #256517
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      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.

       

       

    • #256526
      Participant
      Topics: 15
      Replies: 1765
      Points: 3,178
      Helping Hand
      Rank: Community Hero

      There are a couple of ways to write the loop, but here is a basic example”

      The loop exits when all conditions are True.

      • This reply was modified 1 week, 2 days ago by Rob Simmers.
    • #256538
      Participant
      Topics: 8
      Replies: 565
      Points: 2,158
      Helping Hand
      Rank: Community Hero

      First, your while loop contains or when it should be -or

      Second, you don’t need the extra parentheses, this is perfectly fine.

      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

    • #256760
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      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.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.