Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through each item in a single output, using Powershell

currently I am trying to write a script that obtains a list of all services on my computer, then try to use each service name in that output to run "sc.exe qfailure [service]."

However, I am not sure why my output consistently runs a single service's name over and over again. Here is my code.

Also, I'm trying to make it that if any service is configured to run a program in it's recovery options, it should be able to print out the service name. I only know how to -match "RUN PROCESS" though. How can I match the service name?

Part of my code is below.

    $services = get-service | select -expand name;
    Write-Host $services
    $output = $services | ForEach {sc.exe qfailure $services in};

Will appreciate any help. Thanks so much!

like image 689
J. Lim Avatar asked Oct 28 '25 05:10

J. Lim


1 Answers

When you pipe an object into foreach, each object within that object can be signified by the $_ variable

$output = $services | ForEach {sc.exe qfailure $_ in};
like image 112
jbockle Avatar answered Oct 31 '25 03:10

jbockle