
How to start/stop a NT service...
Here is a sample I just wrote for one of my MS Press WSH books.
The sample requires ADSI installed.
'************************************************
' File: StartStopService.vbs (WSH-sample in VBScript)
' Author: (c) G. Born
'
' Demonstrates how to start and stop a service using
' ADSI. (A sample from my MS Press book Advanced
' Windows Script Host Programming)
'************************************************
Option Explicit
Const service = "cisvc" ' Indexing service
Const computer = "Rom"
Dim oService, oComp
Dim sState
sState = array ("", "stopped", "start pending", "stop pending", _
"running", "continue pending", "pause pending", _
"paused", "error")
' Bind to service object
Set oComp = GetObject("WinNT://" & computer & ",computer")
Set oService = oComp.GetObject ("Service", service)
' attempt to start service
If MsgBox ("Service '" & service & "' status is: " & _
sState(oService.Status), vbYesNo + vbQuestion, _
"Start service on Computer //" & computer& "?") _
= vbYes Then
oService.start ' start my service
WScript.Echo "Service started" ' delay ...
End if
' attempt to pause service
If MsgBox ("Service '" & service & "' status is: " & _
sState(oService.Status), vbYesNo + vbQuestion, _
"Pause service on Computer //" & computer& "?") _
= vbYes Then
oService.pause ' pause service
WScript.Echo "Service paused" ' delay ...
End if
' attempt to stop service
If MsgBox ("Service '" & service & "' status is: " & _
sState(oService.Status), vbYesNo + vbQuestion, _
"Stop service on Computer //" & computer& "?") _
= vbYes Then
oService.stop ' stop service
WScript.Echo "Service stopped" ' delay ...
End if
WScript.Echo "Service '" & service & " Status '" & _
sState(oService.Status) & "' on Computer //" & computer
Set oService = Nothing ' release object variable
WScript.Quit
'*** End
--
______________________________________________________
Check out the WSH Bazaar at www.borncity.de
Thomas Eiberle schrieb in Nachricht
Quote:
>Hi,
>I wonder if it is possible to start and stop a NT service via WSH?
>Has somebody experience with this topic?
>Thanks in advance for your help,
>Thomas Eiberle