Results 1 to 4 of 4
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    Missing the Obvious?

    Hey Y'all,

    I can't seem to find the error of my ways in the following code:
    Code:
    Clear-Host
    
    $ans = Read-Host "`nType UAC Level 1-4 `nor 0 to Exit and press the Enter Key"
    
    If ($ans -ge 0 -and $ans -le 4) {
    
      If ($ans -gt 0) {
        
        Write-Host "`nUAC set to Level $ans `n"
    
      }   #End If ($ans -gt 0)
      Else {
             Write-Host "`nUser selected Exit"
      }
    }
    Else {
           Write-Host "`n$ans is an invalid entry!"
    }
    
     Sleep 5   #Give user 5 seconds to read message
    Why do negative numbers and multiple digit numbers NOT fail the test [If ($ans -ge 0 -and $ans -le 4)]?

    Sample Runs:
    Run 1:
    Type UAC Level 1-4
    or 0 to Exit and press the Enter Key: 10

    UAC set to Level 10 <-- This is WRONG!

    Run 2:
    Type UAC Level 1-4
    or 0 to Exit and press the Enter Key: 0

    User selected Exit

    Run 3:
    Type UAC Level 1-4
    or 0 to Exit and press the Enter Key: 1

    UAC set to Level 1

    Run 4:
    Type UAC Level 1-4
    or 0 to Exit and press the Enter Key: 4

    UAC set to Level 4

    Run 5:
    Type UAC Level 1-4
    or 0 to Exit and press the Enter Key: 5

    5 is an invalid entry!

    Run 6:
    Type UAC Level 1-4
    or 0 to Exit and press the Enter Key: -1

    UAC set to Level -1 <--- This is WRONG

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    WS Lounge VIP Browni's Avatar
    Join Date
    Dec 2009
    Location
    Rochdale, UK
    Posts
    1,650
    Thanks
    38
    Thanked 161 Times in 139 Posts
    Comparing strings to numerics?

    Would this work?

    If ($ans -ge "0" -and $ans -le "4")

  3. The Following User Says Thank You to Browni For This Useful Post:

    RetiredGeek (2015-05-18)

  4. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Browni,

    Duh! I just couldn't wrap my mind around it today. I've now written down in my notes in BIG letters Read-Host returns a string variable!

    However, I have chosen this path to the correction:
    Code:
    [int]$ans = Read-Host "`nType UAC Level 1-4 `nor 0 to Exit and press the Enter Key"
    
    If ($ans -ge 0 -and $ans -le 4) {
    It's less typing! Of course it cause other problems if the user doesn't enter a number.

    Thanks much!

    Update: I just had a thought "10" is less than "4" in string comparisons! Looks like I need to do some serious error checking here.
    Last edited by RetiredGeek; 2015-05-18 at 10:36.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hey Y'all,

    Here's a solution I came up with after a little googling!
    Code:
    Clear-Host
    
    do {
        try {
            $numOk = $true
            [int]$NewUACLevel = Read-host "`nType UAC Level 1-4 `nor 0 to Exit and press the Enter Key"
            } # end try
        catch {$numOK = $false}
        } # end do 
    until (($NewUACLevel -ge 0 -and $NewUACLevel -le 4) -and $numOK)
    
      If ($NewUACLevel -gt 0) {
        
        Write-Host "`nUAC set to Level $NewUACLevel `n"
    
      }   #End If ($NewUACLevel -gt 0)
      Else {
             Write-Host "`nUser Selected Exit"
      }
    
     Sleep 5   #Give user 5 seconds to read message
    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •