locked
Simple if -ne -or statement with multiple values won't work. RRS feed

  • Question

  • Hi,

    I have a simple question but i for the likes of me can't figure it out.

    I just want an Elseif statement being run whenever a value is not 1 or 2 or 3. so if it's anything else than 1, 2 or 3 it should run.

    So i made this test script (see bellow) Now whenever i run this script i get the elseif result. But i don't want it. i want the "else" result. Does anybody know how i need to fix this? it's probably really simple but it's Monday i guess? ;)

    $a = 2

    if($a -eq 1)
    {write-host "It is one"}

    elseif($a -ne 1 -or $a -ne 2 -or $a -ne 3)
    {
    write-host "it's something else than  1 2 or 3"
    write-host "It's:" $a
    }
    Else
    {
    write-host "it's 2 or 3"
    }


    Don't forget about Alt+Esc!

    Monday, April 23, 2018 9:30 AM

Answers

  • In the elseif part you have to change the "-or" to "-and". Otherwise will at least 2 cases always be the case.  ;-)

    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, April 23, 2018 9:41 AM
    • Proposed as answer by jrv Monday, April 23, 2018 9:49 AM
    • Marked as answer by Weslee db Monday, April 23, 2018 9:52 AM
    Monday, April 23, 2018 9:41 AM

All replies

  • In the elseif part you have to change the "-or" to "-and". Otherwise will at least 2 cases always be the case.  ;-)

    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, April 23, 2018 9:41 AM
    • Proposed as answer by jrv Monday, April 23, 2018 9:49 AM
    • Marked as answer by Weslee db Monday, April 23, 2018 9:52 AM
    Monday, April 23, 2018 9:41 AM
  • ah you are correct. i will delete this question now so the internet doesn't remember my stupidity ;)

    no i was thinking if i change it to "-and" the $a HAS to be both 1 and 2 and 3. But ofcourse that's just impossible...

    /me back to sleep now ;)


    Don't forget about Alt+Esc!

    Monday, April 23, 2018 9:52 AM
  •  i will delete this question now so the internet doesn't remember my stupidity ;)

    So you want to betrail me about my reward?  ;-) :-D

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Monday, April 23, 2018 9:56 AM