G_M
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.
If I could get a revision on my code including a bit better error checking, I'd really appreciate the help.
Cheers,
G_M
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
If I could get a revision on my code including a bit better error checking, I'd really appreciate the help.
Cheers,
G_M
can u post the error?
to make sure server is available, i would use ping:
Set oWMI = GetObject("winmgmts:\\.\ro ot\cimv2")
Set oPing = oWMI.Get("Win32_PingStatus .Address=' "& strHostName & "'")
If oPing.StatusCode = 0 Then
isServiceInstalled svcName, strHostName
Else
WriteLog("Cannot contact " & strHostName)
End If
Set oWMI = GetObject("winmgmts:\\.\ro
Set oPing = oWMI.Get("Win32_PingStatus
If oPing.StatusCode = 0 Then
isServiceInstalled svcName, strHostName
Else
WriteLog("Cannot contact " & strHostName)
End If
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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
So it's running with no errors?
Try printing out the name to ensure you're using the right variables...
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
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No problem with me. I forgot to put in the ping check....good addition.
Rob.
Rob.
ASKER
Had to split the points guys, you both got me over the line.
Thanks
Thanks