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

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).
LVL 15
greyknight17Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
greyknight17Author Commented:
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
RobSampsonCommented:
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
RobSampsonCommented:
Thanks for the grade, by the way.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.