How to start/stop services with vbscript? 
Author Message
 How to start/stop services with vbscript?

How can I start or stop a windows service from a VBScript?


Wed, 27 Apr 2005 22:05:16 GMT  
 How to start/stop services with vbscript?

Quote:
> How can I start or stop a windows service from a vbscript?

Jan, here's one way. Note the triple quotes to allow for service names
containing spaces. The 'Result' variable will contain the exit code from
the NET command, if successful the exit code will be zero.

  Set WshShell=WScript.CreateObject("WScript.Shell")
  ServiceName = """Windows Time"""
  Result = WshShell.Run("net stop " & ServiceName, 0, True)

--
Ritchie
Undo address for mail



Thu, 28 Apr 2005 02:37:41 GMT  
 How to start/stop services with vbscript?

Quote:

> How can I start or stop a windows service from a vbscript?

Ritchie showed you how to use net.exe from a vbscript, I can show you how to do
it with ADSI as well as WMI:

Using ADSI:

' Stop the messenger service:

Const ADS_SERVICE_RUNNING = 4
sServer = "."      ' "." for local machine

Set oComputer = GetObject("WinNT://" & sServer & ",computer")
Set oService = oComputer.GetObject("Service", "Messenger")

If oService.Status = ADS_SERVICE_RUNNING Then
  oService.Stop
End If

IADsService
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsservice.asp

IADsServiceOperations
http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsserviceoperat...

Using WMI:

sServer = "."     '"." for local machine
Set oWMI = GetObject("winmgmts://" & sServer)

' using the fax service as an example
sServiceName = "Fax"

sWQL = "Select state from Win32_Service " _
    & "Where displayname='" & sServiceName & "'"

Set oResults = oWMI.ExecQuery(sWQL)
For Each oService In oResults
  If LCase(oService.State) = LCase("Running") Then
    oService.StopService
  End If
Next

Win32_Service
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_service.asp

--
torgeir
Microsoft MVP Scripting and WMI
Porsgrunn Norway



Thu, 28 Apr 2005 02:41:38 GMT  
 How to start/stop services with vbscript?
Torgeir, Ritchie,

Thank you for your replies.
I 'll try all methods. As far as I see now, I prefer the ADSI way.

Jan



Quote:

> > How can I start or stop a windows service from a vbscript?

> Ritchie showed you how to use net.exe from a vbscript, I can show you how
to do
> it with ADSI as well as WMI:

> Using ADSI:

> ' Stop the messenger service:

> Const ADS_SERVICE_RUNNING = 4
> sServer = "."      ' "." for local machine

> Set oComputer = GetObject("WinNT://" & sServer & ",computer")
> Set oService = oComputer.GetObject("Service", "Messenger")

> If oService.Status = ADS_SERVICE_RUNNING Then
>   oService.Stop
> End If

> IADsService
> http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsservice.asp

> IADsServiceOperations

http://msdn.microsoft.com/library/en-us/netdir/adsi/iadsserviceoperat...
p

- Show quoted text -

Quote:

> Using WMI:

> sServer = "."     '"." for local machine
> Set oWMI = GetObject("winmgmts://" & sServer)

> ' using the fax service as an example
> sServiceName = "Fax"

> sWQL = "Select state from Win32_Service " _
>     & "Where displayname='" & sServiceName & "'"

> Set oResults = oWMI.ExecQuery(sWQL)
> For Each oService In oResults
>   If LCase(oService.State) = LCase("Running") Then
>     oService.StopService
>   End If
> Next

> Win32_Service
> http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_service.asp

> --
> torgeir
> Microsoft MVP Scripting and WMI
> Porsgrunn Norway



Fri, 29 Apr 2005 22:44:53 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to start/stop services with vbscript?

2. Starting and Stopping NT Services with ADSI,wsh, and VBScript

3. Starting, stopping and pauseing a service.

4. Problems with stopping/starting service

5. START/STOP/ displayed Services running on PC thu an ASP page

6. How to start/stop a NT service

7. start and stop services

8. login script to start/stop service

9. stop/start a service in VBS

10. Stop and Starting Services on NT

11. Start/Stop a NT Service using wsh/adsi

12. Starting and stopping Services from ASP or VBS

 

 
Powered by phpBB® Forum Software