why my script is not writing my condition

Welcome Forums General PowerShell Q&A why my script is not writing my condition

Viewing 5 reply threads
  • Author
    Posts
    • #198443
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      I have the following

      @("Running", "Stopped").ForEach({ if($result = Get-Service | where { $_.Status -like $_ }) { Write-Output -InputObject "$($result)" } })

      why it not writing my statuses???

    • #198446
      Participant
      Topics: 4
      Replies: 1906
      Points: 4,251
      Helping Hand
      Rank: Community Hero

      Wow … that’s a strange way to get services with the status stopped and running. Try it this way:

      @("Running", "Stopped").ForEach( { $Status = $_; if ($result = Get-Service | where { $_.Status -like "$Status*" }) { Write-Output -InputObject "$($result)" } })

      The moment you use the pipeline variable ($_) from your “outer” loop you already replaced it with your “inner” pipeline variable.

      The “default” Powershell way would be something like this:

      Get-Service |
          Where-Object {$_.Status -eq 'Running' -or $_.Status -eq 'Stopped'} |
              Select-Object -Property Name

      … much easier to read I think. 😉

      • This reply was modified 2 months, 1 week ago by Olaf.
      • This reply was modified 2 months, 1 week ago by Olaf.
    • #198452
      Participant
      Topics: 12
      Replies: 1488
      Points: 1,982
      Helping Hand
      Rank: Community Hero

      What exactly are you trying to accomplish? There are numerous things wrong, but the main issue is that you are trying to use the $_ context incorrectly, see this example:

      @("Running", "Stopped").ForEach{ 
          #$_ Context is in foreach
          $status = $_               #$_ Context is from the pipeline
          if($result = Get-Service | where { $_.Status -like $status }) { 
              Write-Output -InputObject "$($result)" 
          } 
      }
      

      Piping to foreach can get confusing, so it’s typically better to do an explicit for loop:

      ForEach ($status in @("Running", "Stopped")) { 
          if($result = Get-Service | where { $_.Status -like $status }) { 
              Write-Output -InputObject "$($result)" 
          } 
      }
      

      If you want to group by status, try Group-Object

      PS C:\Users\rasim> Get-Service | Group-Object -Property Status
      
      Count Name                      Group                                                                                                                                                                                                                            
      ----- ----                      -----                                                                                                                                                                                                                            
        156 Stopped                   {AarSvc_96a65, AJRouter, ALG, AppIDSvc...}                                                                                                                                                                                       
        136 Running                   {AESMService, Appinfo, AudioEndpointBuilder, Audiosrv...}                                                                                                                                                                        
      
      
    • #198464
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Wow … that’s a strange way to get services with the status stopped and running. Try it this way:

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 7.20125px; left: 45px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      @(“Running”, “Stopped”).ForEach( { $Status = $_; if ($result = Get-Service | where { $_.Status -like “$Status*” }) { Write-Output InputObject “$($result)” } })
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      The moment you use the pipeline variable ($_) from your “outer” loop you already replaced it with your “inner” pipeline variable.

      The “default” Powershell way would be something like this:

      PowerShell
      3 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 7.20125px; left: 45px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      Get-Service |
      Where-Object {$_.Status -eq ‘Running’ -or $_.Status -eq ‘Stopped’} |
      Select-Object Property Name
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      … much easier to read I think. 😉

      Thank you

      I code it that way so it could run in 1 line without using semicolon, so your first solution works like a charm.

    • #198497
      Participant
      Topics: 4
      Replies: 1906
      Points: 4,251
      Helping Hand
      Rank: Community Hero

      I code it that way so it could run in 1 line without using semicolon, ….

      And what’s the advantage of that?

    • #198575
      Participant
      Topics: 12
      Replies: 1488
      Points: 1,982
      Helping Hand
      Rank: Community Hero

      Typically, there are only two states (Running\Stopped), so it can be as simple as this:

      $results = Get-Service | Select Name
      
Viewing 5 reply threads
  • You must be logged in to reply to this topic.