Solved Restart service on list of computers from txt file

September 4, 2013 at 04:17:58
Specs: Windows 7
I have a list of computers in a txt file that I want to be able to stop and start a running service on them. How can I accomplish that?

See More: Restart service on list of computers from txt file

Report •


#1
September 4, 2013 at 07:48:31
You'll probably end up using WMI to do it, but the SC command is also possible. Neither option will handle dependent services for you, so that would layer levels of complexity.

Assuming your service doesn't have any dependents, you could do something like this in PowerShell:

Get-Content somefile.txt | ForEach-Object {
Get-WmiObject Win32_Service -ComputerName $_ -Filter "Name='Spooler'" } | 
 ForEach-Object { 
  $stop = $_.StopService() ; Start-Sleep -Seconds 10 
  $start = $_.StartService()
  "{0}\{1}:`n`tStop: {2}`n`tStart: {3}" -f $_.PSComputerName, 
    $_.Name, $stop.ReturnValue, $start.ReturnValue 
}

How To Ask Questions The Smart Way


Report •

#2
September 4, 2013 at 07:52:19
I've been using SC for the time being which works fine because there are no dependencies but I've been inputting the computer name manually... how would I do it using SC with it reading from the text file?

Report •

#3
September 7, 2013 at 09:30:09
✔ Best Answer
Hello,
If you are looking for an easy solution for WinXP/2003 through Win8/2012 that has not external dependencies, like enabling PowerShell script execution or installing/copy program files, then using the CMD.EXE prompt's FOR command is the best bet.

:: A one line batch file 
for /F %%i in (computerlist.txt) do ( @sc \\%%i stop "SERVICE NAME" )

:: Same one liner but direct at command prompt
for /F %i in (computerlist.txt) do ( @sc \\%i stop "SERVICE NAME" ) 


Report •

Related Solutions

#4
September 7, 2013 at 09:31:25
:: A one line batch file 
for /F %%i in (computerlist.txt) do ( @sc \\%%i stop "SERVICE NAME" )

:: Same one liner but direct at command prompt
for /F %i in (computerlist.txt) do ( @sc \\%i stop "SERVICE NAME" )


Report •

#5
September 7, 2013 at 10:54:43
My powershell script avoids the script execution policy, except on the local machine. Also, you're forgetting to restart the service.

How To Ask Questions The Smart Way


Report •


Ask Question