Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Starting Service using WSH

3 views
Skip to first unread message

Oleg Rakitskiy

unread,
Mar 20, 2003, 8:44:28 AM3/20/03
to
How can I start service using WSH.. Shell.Run("Net start..") doesnt' suit me
'cos it show cmd window. Can I start service without cmd showing??

Best Regards. Oleg.


Torgeir Bakken (MVP)

unread,
Mar 20, 2003, 8:55:55 AM3/20/03
to
Oleg Rakitskiy wrote:

> How can I start service using WSH.. Shell.Run("Net start..") doesnt' suit me
> 'cos it show cmd window. Can I start service without cmd showing??

Hi

Shell.Run("Net start..", 0, True) and Shell.Run("Net start..", 0, False) will
hide the command prompt.


ADSI or WMI can be used for this as well:


Using ADSI:

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.Start
End If


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

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


Using WMI:

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

' using the Messenger service as an example
sServiceName = "Messenger"

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

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


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


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


0 new messages