Why simple while loop doesn't work?

This topic contains 2 replies, has 3 voices, and was last updated by  js 3 months, 2 weeks ago.

  • Author
    Posts
  • #92752

    squigee
    Participant

    Sorry for this simply one, but can someone explain why when I enter any number between 1-5 the Get-Service doesn't execute.

    I also tired ($sel -eq "1","2","3","4","5" )

     
    
    Clear-host
    
    $sel = Read-Host -Prompt "Enter number 1-5"
    
    While ($sel -eq 1,2,3,4,5 ) {
    
    #below doesn't happen when enter 1-5 
    
    Get-Service t* | Where-Object {$_.Status -eq "Stopped"}
    
    } 
    
    
  • #92756

    Don Jones
    Keymaster

    1,2,3,4,5 is an array of five items. Any single item, like 1, is not equal to an array. You may want to use the -in operator instead.

  • #92826

    js
    Participant
    While (1,2,3,4,5 -eq $sel) {
    

    would actually work (I sort of learned this from the Iron Scripting). But do you want to keep repeating the body of the while loop over and over without asking for new input?

You must be logged in to reply to this topic.