Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Vbs Script to stop a process, the start and stop a service

Posted on 2009-07-13
2
Medium Priority
?
762 Views
Last Modified: 2012-06-27
i need avbs script to stop     thisprocess.exe, the stop a service  "updates" and then start the updates service
0
Comment
Question by:underground712
2 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24842123
Modified from http://www.computerperformance.co.uk/vbscript/wmi_services.htm
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill 
strProcess = "'thisprocess.exe'" 
strService = "'updates'"
intSleep = 10000 '10 second delay
 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcess )
 
For Each objProcess in colProcess
  objProcess.Terminate()
Next 
 
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = " & strService )
 
For Each objService in colListOfServices
  objService.StopService()
  WSCript.Sleep intSleep
  objService.StartService()
Next

Open in new window

0
 
LVL 3

Expert Comment

by:rmconard
ID: 24842154
Here's how to stop and start a service:

Note* You will need WSH to make this work. Download here:

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=47809025-d896-482e-a0d6-524e7e844d81

Option Explicit
Dim objWService, objItem, objService
Dim colList, strService, intSleep, strComp
 
'Leave as is for local machine.
strComp = "."
 
'Time to wait until restart
intSleep = 15000
 
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"
 
strService = " 'updates' "
 
Set objWService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComp & "\root\cimv2")
 
Set colList = objWService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")
 
For Each objService in colList
    objService.StopService()
    WSCript.Sleep intSleep
    objService.StartService()
Next
 
WScript.Echo "Your "& strService & " service has Started"
WScript.Quit

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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