Solved

Batch file for status of service/s, then kill process and finally restart services

Posted on 2009-05-13
5
630 Views
Last Modified: 2013-11-10
Hello,

I'm implementing a Kaseya Server with Vmware snapshots. When snapshot take in place, service "kserver" didn't respond (it's not possible start, stop or restart service), workaround, seemed to kill process kaseya, then restart service.

Can anyone help me to make a batch file that, in first place check is service kaseya is running or not, and in case of not, kill the process and then restart the service?.  The idea is put this batch file in a scheduled task.

Would be interesting use command "sc", instead net start/stop...

Thank you
0
Comment
Question by:jpvhack
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
Tompa99 earned 250 total points
ID: 24379135
Hello

This script should work I think. You have to change the service to the correct one.

Regards Tompa
Dim strService

Dim strComputer

Dim objWMIService, objItem, objService

Dim colListOfServices
 

strComputer = "." ' Localhost

strService = "Alerter" ' Displayname of service
 

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

set colListOfServicesStatus = objWMIService.ExecQuery ("Select * from Win32_Service Where DisplayName ='" & strService & "'")

	

	for each objServiceState in colListOfServicesStatus

		ServiceState = objServiceState.State

		if ServiceState <> "Running"  then

			if ServiceState = "Stopped" Then 

				StartService strService,strComputer

			else 

				StopService strService,strComputer

				StartService strService,strComputer

			end if

		end if 

	next

wscript.Quit(99)
 

'========================== StopService(strService,strComputer)========================================

private function StopService(strService,strComputer)

	Dim objWMIService, objItem, objService

	Dim colListOfServices

	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where DisplayName ='" & strService & "'")

	For Each objService in colListOfServices

		objService.StopService()

		do while ServiceState <> "Stopped"

			set colListOfServicesStatus = objWMIService.ExecQuery ("Select * from Win32_Service Where DisplayName ='" & strService & "'")

				for each objServiceState in colListOfServicesStatus

					ServiceState = objServiceState.State

				next

		loop

	Next

end function
 

'========================== StartService(strService,strComputer)========================================

private function StartService(strService,strComputer)

	Dim objWMIService, objItem, objService

	Dim colListOfServices

	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where DisplayName ='" & strService & "'")

	For Each objService in colListOfServices

		objService.StartService()

		do while ServiceState <> "Running"

			set colListOfServicesStatus = objWMIService.ExecQuery ("Select * from Win32_Service Where DisplayName ='" & strService & "'")

				for each objServiceState in colListOfServicesStatus

					ServiceState = objServiceState.State

				next

		loop

	Next

end function

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24379220
Try this.
sc query "kserver" | find /i "state" | find /i "running"

if errorlevel 1 (

    taskkill /f /im kaseya.exe

    sc start "kserver"

)

Open in new window

0
 

Author Comment

by:jpvhack
ID: 24379721
AmazingTech:

How i could to avoid, that in case thar process kaseya.exe don't exist, this message: ERROR: The process "KServer.exe" not found.
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 250 total points
ID: 24379783
Sure no problem.

We'll use tasklist to find if it is a running process.
sc query "kserver" | find /i "state" | find /i "running"

if errorlevel 1 (

    tasklist /fi "imagename eq kaseya.exe" | find /i "kaseya.exe"

    if not errorlevel 1 taskkill /f /im kaseya.exe

    sc start "kserver"

)

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now