Start disabled service remotely on multiple remote clients

Hi,

Greetings.

I have a requirement where I need to start a windows service if it is stopped.
I did find my answer here and the code in this link below which works perfectly but I would also like to add a functionality that when the service is 'disabled', the status is changed to 'Automatic' and then the service is started.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_27780884.html

Right now, this script passes on to the log file that the service has been started when it's disabled but the service remains stopped and disabled.

Please assist.

Thanks in advance.

Appreciate the help

Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")

strPCList = "C:\Users\a-matanig\Desktop\SCCM-Servers.txt"
strLogLoc = "C:\Users\a-matanig\Desktop\ServiceStart.log"
strSvcName = "WDSServer"


'========================================================================================================================================
'==== Execution Code ====================================================================================================================
'========================================================================================================================================

Set objFile = objFSO.OpenTextFile(strPCList, 1)

Do While Not (objFile.atEndOfStream)

	strHostName = objFile.ReadLine
	Set objPing = objWMI.Get("Win32_PingStatus.Address='" & strHostName & "'")

	If objPing.StatusCode = 0 Then
		
		
		If isServiceInstalled(strSvcName, strHostName) Then
			objShell.Run "cmd /c sc \\" & strHostName & " start " & strSvcName & """"
			WriteLog("Service " & strSvcName & " started on " & strHostName)
		Else
			WriteLog(strSvcName & " not installed on " & strHostName)
		End If
	Else
		WriteLog("Cannot contact " & strHostName)
	End If
		
Loop

'========================================================================================================================================
'===== Functions ========================================================================================================================
'========================================================================================================================================

Function isServiceInstalled(svcName, strComputer) 
    
	blnSvcInst = False

    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    
	If Err.Number = 0 Then
	    svcQry = "SELECT * from Win32_Service Where Name='" & svcName & "'" 
	    Set objOutParams = objWMIService.ExecQuery(svcQry) 
	    For Each objSvc in objOutParams 
			blnSvcInst = True
			Exit For
	    Next 
	End If
	
	Err.Clear
	On Error GoTo 0
    isServiceInstalled = blnSvcInst
	
End Function 

'Logging of messages to local PC
Function WriteLog(strLogOut)
	On Error Resume Next
	strLogOut = Now() & "  " & strComputer & "  " & strLogOut
	objFSO.OpenTextFile(strLogLoc, ForAppending, True).WriteLine(strLogOut)
	
	On Error GoTo 0	
End Function

Open in new window

kumaran16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, try this.  Instead of using SC remotely, I've duplicated your WMI call, and change the StartMode of the service using WMI.

Regards,

Rob,

Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")

strPCList = "C:\Users\a-matanig\Desktop\SCCM-Servers.txt"
strLogLoc = "C:\Users\a-matanig\Desktop\ServiceStart.log"
strSvcName = "WDSServer"


'========================================================================================================================================
'==== Execution Code ====================================================================================================================
'========================================================================================================================================

Set objFile = objFSO.OpenTextFile(strPCList, 1)

Do While Not (objFile.atEndOfStream)

	strHostName = objFile.ReadLine
	Set objPing = objWMI.Get("Win32_PingStatus.Address='" & strHostName & "'")

	If objPing.StatusCode = 0 Then
		
		
		If isServiceInstalled(strSvcName, strHostName) Then
			'objShell.Run "cmd /c sc \\" & strHostName & " start " & strSvcName & """"
			StartService strSvcName, strHostaName
			WriteLog("Service " & strSvcName & " started on " & strHostName)
		Else
			WriteLog(strSvcName & " not installed on " & strHostName)
		End If
	Else
		WriteLog("Cannot contact " & strHostName)
	End If
		
Loop

'========================================================================================================================================
'===== Functions ========================================================================================================================
'========================================================================================================================================

Function isServiceInstalled(svcName, strComputer) 
    
	blnSvcInst = False

    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    
	If Err.Number = 0 Then
	    svcQry = "SELECT * from Win32_Service Where Name='" & svcName & "'" 
	    Set objOutParams = objWMIService.ExecQuery(svcQry) 
	    For Each objSvc in objOutParams 
			blnSvcInst = True
			Exit For
	    Next 
	End If
	
	Err.Clear
	On Error GoTo 0
    isServiceInstalled = blnSvcInst
	
End Function 

Function StartService(svcName, strComputer)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CimV2") 
Set colListOfServices = objWMIService.ExecQuery ("Select * From Win32_Service Where Name ='" & svcName & "'")
For Each objService in colListOfServices
	objService.ChangeStartMode("Automatic")
	Wscript.Sleep 5000
	errReturnCode = objService.StartService()
Next
End Function

'Logging of messages to local PC
Function WriteLog(strLogOut)
	On Error Resume Next
	strLogOut = Now() & "  " & strComputer & "  " & strLogOut
	objFSO.OpenTextFile(strLogLoc, ForAppending, True).WriteLine(strLogOut)
	
	On Error GoTo 0	
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kumaran16Author Commented:
Hi Rob,

Sorry about the delayed reply..

I get the below error..

error
0
RobSampsonCommented:
LOL!  Whoops!!  Change line 29 from this:
			StartService strSvcName, strHostaName

Open in new window


to this
			StartService strSvcName, strHostName

Open in new window


I fat-fingered the parameter.

Rob.
0
kumaran16Author Commented:
Voila! That worked..

Thank you Rob...
0
RobSampsonCommented:
Great. Thanks for the grade.

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.