Process all objects in Foreach

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of TommyQuality TommyQuality 2 months, 3 weeks ago.

  • Author
    Posts
  • #69603
    Profile photo of TommyQuality
    TommyQuality
    Participant

    Hi,

    Is it possible to process all objects in a foreach loop without waiting for the previous object to complete?

    So for instance

    $Services = Get-Service
    Foreach ($Service in $Services)
              {
                 Stop-Service -Name $Service.
              }
    

    Is there a way to do all in one hit?

    Cheers

  • #69604
    Profile photo of Paul DeArment Jr
    Paul DeArment Jr
    Participant

    You could do it with a workflow

    workflow MyStopService
    {
         $services = get-service
         foreach -parallel ($service in $services)
         {
              stop-service -name $service
         }
    }
    

    Now this doesn't do anything about services that stop one service when stopping themselves – but this is at least one way you could accomplish what you were looking to do.

  • #69624
    Profile photo of TommyQuality
    TommyQuality
    Participant

    Thanks Paul, the workflow seems to have done the trick.

    Cheers

You must be logged in to reply to this topic.