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

1 year, 1 month ago.

  • Author
  • #92752

    Points: 1
    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" )

    $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

    Points: 1,811
    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

    Points: 743
    Helping Hand
    Rank: Major Contributor
    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.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort