?
Solved

End a Windows service/process in Task Manager and start it back up

Posted on 2008-01-28
4
Medium Priority
?
836 Views
Last Modified: 2012-06-21
I have tried another script that supposedly works for this, but it's causing problems...in that it doesn't actually end or start the service back up. It was a generated script from a site, so I guess that's always room for flaws.

I want to kill a certain service from the Task Manager at a certain time (I guess this can be done via Scheduled Tasks instead of coding). After that process is killed at the said time, I want it to be started back up again after like 30 seconds or so. I do NOT want the service to be restarted as it still might cause problems. I want it to be completely killed off.

1. Is it possible to add a verification code so that the script makes sure it's terminated first before it waits 30 seconds and try starting it back up? The first generated script I tried didn't seem to have this error check.

2. I want everything to be logged into a separate notepad log file (when it was brought down, when it was back up and any other errors if any in between the process). Do this without any prompts from the script....I just want it to run at the scheduled time (I'll do it via Scheduled Tasks) and that's it. No clicking OK to say it was stopped and started.

VBS would be perfect/preferable for this or any other methods (programming wise even), feel free to let me know. Something that I can edit if need be. I'm not sure if the starting back the service (services.msc service that is) will require a second script to be put into Scheduled Tasks or not. If it can be done with one VBS script file and run the stop and start command (check to make sure it's stopped first before starting) that would be great.

Thanks.

PS: Sorry about the point value. Tried to give more but I'm left with only 275. I have around 90,000 but I guess EE limits inactive users (been away for a while).
0
Comment
Question by:greyknight17
  • 3
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 1100 total points
ID: 20773694
Hi, try this script:

If you have a file called computers.txt with a list of computer names in it (one per line), it will check the specified service on that machine, stop it, then restart it as a specified interval.

'=====================================
' Note: strService is case sensitive.
strService = "Print Spooler"
strInputFile = "computers.txt"
intWaitToRestart = 5 ' in seconds
strLogFile = "ServiceLog.txt"

strLog = ""

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
While Not objInputFile.AtEndOfStream
      strComputer = objInputFile.ReadLine
      If Ping(strComputer) = True Then
            strLog = strLog & VbCrLf & Now & ": Restarting the " & strService & " service on " & strComputer
            Set objWMIService = GetObject("winmgmts:" _
                  & "{impersonationLevel=impersonate}!\\" _
                  & strComputer & "\root\cimv2")
            Set colListOfServices = objWMIService.ExecQuery _
                  ("Select * from Win32_Service Where DisplayName='"& strService & "'")
            On Error Resume Next
            For Each objService In colListOfServices
                  If Err.Number = 0 Then
                        On Error GoTo 0
                        'strLog = strLog & VbCrLf & Now & ": State of " & strService & " service on " & strComputer & ": " & objService.State
                        strLog = strLog & VbCrLf
                        If objService.State <> "Running" Then
                              strLog = strLog & VbCrLf & Now & ": " & strService & " service on " & strComputer & " is not running."
                              strLog = strLog & VbCrLf & Now & ": Starting the service in " & intWaitToRestart & " seconds..."
                        Else
                              strLog = strLog & VbCrLf & Now & ": " & strService & " service on " & strComputer & " is running."
                              strLog = strLog & VbCrLf & Now & ": Stopping the service..."
                              intReturn = objService.StopService()
                              If intReturn <> 0 Then
                                    strLog = strLog & VbCrLf & Now & ": Error stopping " & strService & " service on " & strComputer & "."
                              Else
                                    strLog = strLog & VbCrLf & Now & ": " & strService & " service on " & strComputer & " has stopped."
                                    strLog = strLog & VbCrLf & Now & ": Starting the service in " & intWaitToRestart & " seconds..."
                              End If
                        End If
                        WScript.Sleep intWaitToRestart * 1000
                        intReturn = objService.StartService()
                        'WScript.Sleep 5000
                        If intReturn <> 0 Then
                              strLog = strLog & VbCrLf & Now & ": Error starting " & strService & " service on " & strComputer & "."
                        Else
                              strLog = strLog & VbCrLf & Now & ": " & strService & " was successfully started on " & strComputer
                        End If
                        strLog = strLog & VbCrLf
                  Else
                        Err.Clear
                        On Error GoTo 0
                        strLog = strLog & VbCrLf & Now & ": " & strService & " service was not found on " & strComputer
                  End If
            Next
      Else
            strLog = strLog & VbCrLf
            strLog = strLog & VbCrLf & Now & ": " & strComputer & " could not be pinged."
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile(strLogFile, True)
objOutputFile.Write strLog
objOutputFile.Close
Set objOutputFile = Nothing

MsgBox "Done. Please see " & strLogFile

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'=====================================

Regards,

Rob.
0
 
LVL 15

Author Comment

by:greyknight17
ID: 20826748
Thanks Rob. That seems to have done the trick.

Any way to make it display the real time it took for each step? Right now it shows everything happening at once.

2/5/2008 3:39:54 PM: Restarting the abc service on xxx
2/5/2008 3:39:54 PM: abc service on xxx is running.
2/5/2008 3:39:54 PM: Stopping the service...
2/5/2008 3:39:54 PM: abc service on xxx has stopped.
2/5/2008 3:39:54 PM: Starting the service in 15 seconds...
2/5/2008 3:40:09 PM: abc was successfully started on xxx

Nothing major as long as it did the job. If you can make it work with the actual time it went down and then started back up, that's perfect.

Solution accepted.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20827823
Yeah, i guess so.....if you put this at the top:

dteStart = Timer

Then above this line:
MsgBox "Done. Please see " & strLogFile

put this
dteEnd = Timer
WScript.Echo "Time taken: " & dteEnd - dteStart & " seconds."

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20827832
Thanks for the grade, by the way.

Regards,

Rob.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question