While loop keeps running , but need to go to if statement

Hello

Small question:
My while loops keeps running. Even when I Typ 1 it start again.
Normally when I typ 1 it should go to my first IF statement but that doesn’t work.
Is there something I don’t see?
When I typ $Keuze the variable is assigned 1 but stil it doesn’t go to my IF statement …

$Keuze = Read-Host "Wat wilt u doen? `n 1.Wachtwoordreset `n 2.Aliassen aanpassen `n 3.Rechten aanpassen mailbox `n 4.Taal aanpassen mailbox `n 5.Licenties wijzigen `n Typ het cijfer"

while ($Keuze -ne "1" -or $Keuze -ne "2" -or $Keuze -ne "3" -or $Keuze -ne "4" -or $Keuze -ne "5") 
{
    Write-Host "Foute Keuze gemaakt"  -ForegroundColor DarkMagenta
    $Keuze = Read-Host "Wat wilt u doen? `n 1.Wachtwoordreset `n 2.Aliassen aanpassen `n 3.Rechten aanpassen mailbox `n 4.Taal aanpassen mailbox `n 5.Licenties wijzigen `n Typ het cijfer"
}


if ($Keuze -eq "1") 
{
    $UPN = Read-Host "Geef de UPN in voor wie u het wachtwoord wilt resetten"
    $Password = Set-MsolUserPassword -UserPrincipalName $UPN -ForceChangePassword $false
    Write-Host "`Test"
    

It is because you are using -or

hmm but in my script I have 5 options. What should I do then?

Not knowing exactly what you are trying to do as I dont have info on all the variables, if you want to break out of the loop when ANY one of the vars is -ne, use -and.

2 Likes

haha yes that was it, stupid of me, thank you!

The while loop runs while the result is True.

When evaluating multiple OR statements, the result is True when any one of the conditional statements is True.

If $Keuze = 1 then all your other conditional statements are True, and the while loop will continue to run.

As suggested by @tonyd, you can change the the logical operation to -and but with lots of a possible values, you might want to consider a different approach.

function showMenu {

    $Keuze = Read-Host "Wat wilt u doen? `n 1.Wachtwoordreset `n 2.Aliassen aanpassen `n 3.Rechten aanpassen mailbox `n 4.Taal aanpassen mailbox `n 5.Licenties wijzigen `n Typ het cijfer"

    switch ($Keuze) {

        '1' {Wachtwoordreset}

        '2' {Write-Host 'Aliassen aanpassen'}

        '3' {write-Host 'Rechten aanpassen mailbox'}

        '4' {Write-Host 'Taal aanpassen mailbox'}

        '5' {Write-Host 'Licenties wijzigen'}

        default {showMenu}

    }

}

function Wachtwoordreset {

    $UPN = Read-Host "Geef de UPN in voor wie u het wachtwoord wilt resetten"
    $Password = Set-MsolUserPassword -UserPrincipalName $UPN -ForceChangePassword $false
    Write-Host "`Test"

}

showMenu
2 Likes