Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

start/stop services script?

Status
Not open for further replies.

lgtshadow

Technical User
Dec 31, 2003
5
US
Is it possible to start and stop window's services using a script? (or command line/dos command)
Currently I have a large batch file that is set to run every night and it's now become necessary to stop/start the database service so that I can minuplate the database files without them getting messed up/potentaly accessed.
 
You can start and stop services using the net stop and net start command via a batch file.

Denny
MCSA (2003) / MCDBA (SQL 2000)

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(My very old site)
 
yes, this can be done using a script with WMI.

There is an example that I wrote to stop and start the spooler service.

Code:
'==========================================================================
'
' NAME: StopStartSpooler.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 10/18/2004
'
' COMMENT: <comment>
'
'==========================================================================

On Error Resume Next

'start with getting the local computer name
strComputer = "."

'now stop the services
Set objWMI = getobject("winmgmts://" & strComputer)
StopService ("Spooler")
WScript.Sleep 3000
StartService ("Spooler")

'sub to stop the services
Sub StopService (ServiceName)
         queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
         set results = objWMI.execquery(queryString)
         for each service in results
            if service.state = "Running" then
             service.stopService
            end if
          next
End Sub

'sub to start the services
Sub StartService (ServiceName)
         queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
         set results = objWMI.execquery(queryString)
         for each service in results
            if service.state <> "Running" then
             service.startService
            end if
          next
End Sub

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Markdmac As added kwezzie here, where can I get tutorials on WMI scripting etc, need to learn some stuff ? I need to upgrade my cat skinners badge[smile]

[blue]Arguably the best cat skinner around ! [/blue]

Cheers
Scott
 
best place to start is at
You can also pick up a lot at
Best advice if you understand the basics of scripting (if you can follow my example above) is to get a copy of Microsoft's free SCRIPTOMATIC tool that will give you sample code. Often requires a lot of editing becuase it gives you TOO MUCH information, but it is a great place to start.

MS Free Tools I recommend:
ScriptOmatic
ADSI ScriptOmatic
TweakOmatic

Make sure you read all of the docs on these as they are hysterical. The scripting team at Microsoft have really great senses of humor. To paraphrase from the TweakOmatic "We thought about making a admOmatic (for custom GPO templates) but the Group Policy Team is really mean and we didn't want to make them mad."

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top