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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS WMI and Services Startup properties.

Status
Not open for further replies.

thomasmcgeown

Technical User
Feb 6, 2002
27
GB
Good morning.

I am currently working on a script that will check for a service, and check if its running.

If the service is present and not running it will attempt to start the service.

At this point i would like the script to set the startup type to automatic.

Im farily new to WMI so im quite sure the problem is my code, it is not permisions as I am running the script from a domain admin account.

Code i am using is below:

If intTester <> 1 Then
Set objWMIService = GetObject(&quot;winmgmts:&quot; & &quot;{impersonationLevel=impersonate}!\\&quot; & strNewString & &quot;\root\cimv2&quot;)
Set colRunningServices = objWMIService.ExecQuery (&quot;Select * from Win32_Service&quot;)
For Each objService in colRunningServices
If objService.DisplayName = &quot;VNC Server&quot; Then
If objService.State = &quot;Running&quot; Then
domystuff
End If
If objService.State= &quot;Stopped&quot; Then
errReturn = objService.StartService()
errReturn = objService.Change(, , , , ,&quot;Auto&quot;)
End If
End If
Next
End if

Please forgive any n00b mistakes or sloppy coding!

Thank you in anticipation!

Tom
 
i would go for an ADSI approach rather than WMI, you will find it more readable and prob quicker.

Set WshNetwork = WScript.CreateObject(&quot;WScript.Network&quot;)
Set objComputer = GetObject(&quot;WinNT://&quot; & WshNetwork.ComputerName & &quot;,computer&quot;)

Set objService = objComputer.GetObject(&quot;service&quot;, &quot;LegatoLauncher&quot;)
If objService.Status = 4 Then
objService.Stop
End If

If objService.StartType <> 4 Then
objService.StartType = 4
objService.SetInfo
End If
Set objService = Nothing
 
Hello Thomasmcgeon,

My quick advice would be to change &quot;auto&quot; to &quot;automatic&quot;, if I recall correctly.

Here is something perplexing, isn't it?

regards - tsuji
 
Hi MrMovie,

Thank you for you reply.
I am not familiar with ADSI, could you comment your code so i dont make any false assumptions about it that will later trip me up?

Sorry to be a nusiance

Agin thank you for your reply

Regards

Tom
 
Set WshNetwork = WScript.CreateObject(&quot;WScript.Network&quot;)
'get reference to ADSI computer object
Set objComputer = GetObject(&quot;WinNT://&quot; & WshNetwork.ComputerName & &quot;,computer&quot;)

'get ref to a specific service
Set objService = objComputer.GetObject(&quot;service&quot;, &quot;LegatoLauncher&quot;)

'if service.status = 4, ie if the service is started
'service.status codes
'1 stopped
'2 start pending
'3 stop pending
'4 running
'5 continue_pending
'6 pause_pending
'7 paused
'8 error
If objService.Status = 4 Then
objService.Stop
End If

'set starttype
'0 boot
'1 system
'2 automatic
'3 manual
'4 disabled
If objService.StartType <> 4 Then
objService.StartType = 4
objService.SetInfo
End If
Set objService = Nothing
 
ms's site should give you full listings of ADSI service methods/properties etc, look for

IADsService and IADsServiceOperations

hope this helps
regards
mrmovie
 
Hello all,

Just for the record, my advice above was meant to be serious, in case thomasmcgeown did not understand it.

It is to change:
Code:
errReturn = objService.Change(, , , , ,&quot;Auto&quot;)
to
Code:
errReturn = objService.Change(, , , , ,&quot;Automatic&quot;)
An alternative would be
Code:
errReturn = objService.ChangeStartMode(&quot;Automatic&quot;)
There is in certain sense an irrationality in the message design. If I may put it, say, a simple read-write property (which is not the case here), for an object a:
a.startmode = &quot;automatic&quot; (_fictitious, assigning)
msgbox a.startmode (_fictitious, showing &quot;auto&quot;)

regards - tsuji
 
Yes, thankyou tsuji, your solution was the one i used in the end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top