Solved

Restart service if it is not running.

Posted on 2012-04-03
6
380 Views
Last Modified: 2012-04-08
Is there a batch or vbscript file that can check to see if a service is running.  If a service is running then the script will do nothing.  If the service isn't running then it would start the service.

Also, the same for a process.  If the process isn't running then it would start the process.

Thank you.
0
Comment
Question by:cbones
6 Comments
 
LVL 19

Expert Comment

by:n2fc
ID: 37804229
See: http://technet.microsoft.com/en-us/library/cc773035(v=ws.10).aspx

The command: NET START {service}  will start a service; if it is already running, it will just tell you so!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37804235
Hi, you can use this for a service:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strServiceName = "spooler"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
	If objItem.State <> "Running" Then objItm.StartService()
Next

Open in new window


and this for a process:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strProcessPath = "c:\temp\myprogram.exe"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & Mid(strProcessPath, InStrRev(strProcessPath, "\") + 1) & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
blnFound = False
For Each objItem In colItems
	blnFound = True
Next
If blnFound = False Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strProcessPath, 1, False
End If

Open in new window


Regards,

Rob.
0
 
LVL 2

Expert Comment

by:FostWare
ID: 37804236
If you're running Windows 2008 or 7, you can set an event log task to restart the service.

We use it because it doesn't require us to have an extra service constantly running the vbscript.

Event log tasks can be set by selecting a specific error log entry for your service, clicking "Attach a task to this log..." and setting the task to cmd /c "sc start MyService"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cbones
ID: 37806115
Rob,

I receive an error when running the first vbscript.  I attached the file.  The process file just runs with no action.

Thank you.
Windows-Script-Host-Error.JPG
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37808888
Sorry, missed the "e" in objItem.  Here is the correct "service" code.

Rob.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strServiceName = "spooler"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
	If objItem.State <> "Running" Then objItem.StartService()
Next

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37809547
Ooop, just saw a mistake in the process code as well.  I accidentally left in Win32_Service instead of Win32_Process

Regards,

Rob

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strProcessPath = "c:\temp\myprogram.exe"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name='" & Mid(strProcessPath, InStrRev(strProcessPath, "\") + 1) & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
blnFound = False
For Each objItem In colItems
	blnFound = True
Next
If blnFound = False Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strProcessPath, 1, False
End If

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now