Solved

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

Posted on 2009-07-13
2
746 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 125 total points
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

10 Experts available now in Live!

Get 1:1 Help Now