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!

Services - Error trying to start a service

Status
Not open for further replies.

theboylatham

Programmer
Mar 10, 2006
6
GB
Morning,

For various reasons, I am trying to alter a service's start-up state via the registry and then start that service via using WMI.

The script successfully changes the 'Start' key for the service in registry from Disabled to Manual (the registry key updates and the service startup type in services) but then the service cannot be started via the script.

I have also changed the registry key via script (and manually) and attempted to start the service manually but this also fails (as below):

System error 1058 has occurred.

The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.

It looks as if changing the service start state via the registry is updating the key and the state in services.msc but still the service is disabled when attempting to start it.

A lot of words and probably not very well explained but I would be grateful for any help...



 
how about

Set objShell = Wscript.CreateObject("WScript.Shell")
On Error Resume Next
strComputer = InputBox("Start Service On Remote Computer")
'Check if service is Running
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colRunningServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = 'DcomLaunch'")
For Each objItem In colRunningServices
If objItem.State="Stopped" Then
wscript.echo "stopped"
Call SetManStart
Else
wscript.echo "started"
wscript.quit
End If
Next

Sub SetManStart
'Set service to Manual
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServicesState = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='DcomLaunch'")
For Each objService in colServicesState
objService.ChangeStartMode("AUTO")
wscript.sleep 5000
'Start service
If objService.State="Stopped" then
objService.StartService()
wscript.sleep 2000
End If
Next
End Sub

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
Thanks for replying.

Yep, this was the way I had originally done it.

But for complicated reasons I've had to change it so that the service start-up state is changed via the registry rather than WMI...

ie.

oReg.GetDWORDValue HKEY_LOCAL_MACHINE,KEY_PATH,KEY_NAME,sKey_Value
If (sKey_Value = 4) Then
oReg.SetDWORDValue HKEY_LOCAL_MACHINE, KEY_PATH, KEY_NAME, MANUAL
End If

Dim oCollection : Set oCollection = oWMI.ExecQuery ("select * from win32_service where name = '" & SERVICE & "'")
For Each oService In oCollection
err = oService.StartService()
MsgBox err
Next

This changes the registry key, as reflected in registry and services.msc but for some reason doesn't allow the service to be started. producing the error in the opening post if attempted manually (using net start for example) and error code 14 via the script.

If I change the startup type manually in services.msc it works fine, so it's clearly something to do with the registry key (ie. all visible pointers indicate that the type has changed but on trying to start the service it still acts as if it's disabled).

Any ideas?




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top