Link to home
Start Free TrialLog in
Avatar of G_M
G_MFlag for Australia

asked on

VBScript - Start service remotely on multiple remote clients

G'day guys,

Just trying to get a quick solution to a problem I am having with this script I am writing. I am trying to start a service on multiple servers from a central server, but I need to query the service first. I have the following script written, but it is throwing an error.

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

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objShell = CreateObject("WScript.Shell") 

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

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

Set objFile = objFSO.OpenTextFile(strPCList, 1) 

Do While Not (objFile.atEndOfStream) 

        strHostName = objFile.ReadLine 
        
        If objFSO.FolderExists("\\" & strHostName & "\c$") Then 
                isServiceInstalled svcName, strHostName 
                
                If isServiceInstalled = True 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  =========================================================
'=======================================================================                    

'Determine if service is installed 
Function isServiceInstalled(svcName, strComputer) 
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    
    ' Obtain an instance of the the class 
    ' using a key property value. 
    
    isServiceInstalled = FALSE 
    
    svcQry = "SELECT * from Win32_Service" 
    Set objOutParams = objWMIService.ExecQuery(svcQry) 

    For Each objSvc in objOutParams 
    
        Select Case objSvc.Name 
            Case svcName 
                isServiceInstalled = TRUE 
        End Select 
        
    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


If I could get a revision on my code including a bit better error checking, I'd really appreciate the help.

Cheers,
G_M
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

can u post the error?
to make sure server is available, i would use ping:

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oPing = oWMI.Get("Win32_PingStatus.Address='"& strHostName & "'")

If oPing.StatusCode = 0 Then
   isServiceInstalled svcName, strHostName
Else
   WriteLog("Cannot contact " & strHostName)
End If
Avatar of G_M

ASKER

Sorry, missed your first post. Here is the error I'm getting.

User generated image
Avatar of G_M

ASKER

obviously this is from the setting isServiceInstalled = False at the start...

This is the error without the value:

User generated image
SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G_M

ASKER

Ok, I got it running and reading the services on the servers. but it won't match the string to the service name... am I looking at the wrong value in objSvc.Name to match a string to?

Here's what I'm working with now:

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

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objShell = CreateObject("WScript.Shell") 

strPCList = "C:\Users\a-matanig\Desktop\SCCM-Servers1.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 
        
        If objFSO.FolderExists("\\" & strHostName & "\c$") Then 
                isServiceInstalled strSvcName, strHostName 
                
                If blnSvcInst = True 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 ======================================================================================================================== 
'======================================================================================================================================== 

'Determine if service is installed 
Function isServiceInstalled(svcName, strComputer) 
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    
    ' Obtain an instance of the the class 
    ' using a key property value. 
    
    blnSvcInst = FALSE 
    
    svcQry = "SELECT * from Win32_Service" 
    Set objOutParams = objWMIService.ExecQuery(svcQry) 

    For Each objSvc in objOutParams 
    
        Select Case objSvc.Name 
            Case svcName 
                blnSvcInst = TRUE 
        End Select 
        
    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

So it's running with no errors?

Try printing out the name to ensure you're using the right variables...

    svcQry = "SELECT * from Win32_Service" 
    Set objOutParams = objWMIService.ExecQuery(svcQry) 

    For Each objSvc in objOutParams 
    WScript.echo ">>" & objSvc.Name & "<<"   'Print out the value

        Select Case objSvc.Name 
            Case svcName 
                blnSvcInst = TRUE 
        End Select

Open in new window

Avatar of G_M

ASKER

Sorry, went to sleep. I did what you suggested, but that was before I implemented you change to line 23 and 25.

Now the script runs fine on most servers, but stops on a couple and either stalls and hangs or throws this error:

User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No problem with me.  I forgot to put in the ping check....good addition.

Rob.
Avatar of G_M

ASKER

Had to split the points guys, you both got me over the line.

Thanks