Why simple while loop doesn't work?

Welcome Forums General PowerShell Q&A Why simple while loop doesn't work?

This topic contains 2 replies, has 3 voices, and was last updated by

js
 
Participant
9 months, 2 weeks ago.

  • Author
    Posts
  • #92752

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,639
    Helping HandTeam Member
    Rank: Community Hero

    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
    Points: 224
    Helping Hand
    Rank: 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?

The topic ‘Why simple while loop doesn't work?’ is closed to new replies.