HTA with vb script to restart specific windows services

Okay.. so i was going through this website to check if i could find something useful with regards to restarting windows services on remote computers and guess what.. i did find something pretty useful. The HTA script can be found @ https://www.experts-exchange.com/questions/26734859/HTA-with-VBScript-to-stop-start-windows-services.html#a34616597

Now, I will be honest I am not an expert with vbscript and know only a few basics of that. What is was looking for is if there is a way to query only some specific services rather than listing all of them as most of the services will be be touched. I am looking to include only services that are related to my application.

Also, it'd be great if I could have a dropdown list to enter the remote host name rather than a text box as it is quite difficult to remember the hostnames of around 60 servers :)

P.S :: This HTA would be run on a legacy Windows 2003 server (yeah i know what you are thinking unfortunately I am struck with these)
Syed AltamashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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, how many services are you wanting to list?  Do you want to list different services for different servers, because that could become a large list to maintain.

In the meantime, this would pull all of your AD servers into a drop down box instead.

Regards,

Rob.

<html>
<head>
<title>ABL Server Service Checker</title>
<HTA:APPLICATION
	ID = "ServiceCheckerApp"
	APPLICATIONNAME="Service Check"
	BORDER = "thin"
	CAPTION = "yes"
	RESIZE = "no"
	SHOWINTASKBAR = "yes"
	SINGLEINSTANCE = "no"
	SYSMENU = "Yes"
	WINDOWSTATE = "normal"
	SCROLL = "no"
	VERSION = "1.0"
	INNERBORDER = "no"
	SELECTION = "no"
	MAXIMIZEBUTTON = "no"
	MINIMIZEBUTTON = "yes"
	NAVIGABLE = "yes"
	CONTEXTMENU = "yes"
	BORDERSTYLE = "normal">
</HTA>

<script language="VBScript">

Dim objWMIService, group

Sub Window_OnLoad
	width = 425
	height = 700
	x = (window.screen.width - width) / 2
	y = (window.screen.height - height) / 2
	If x < 0 Then x = 0
	If y < 0 Then y = 0
	window.resizeTo width,height
	window.moveTo x,y
	'strComputer.value = CreateObject("WScript.Network").ComputerName
	serviceDiv.focus
	serviceDiv.style.display = "none"
	BuildServerList
End Sub

Sub getServices
	If lst_servers.value = "" Then
		Exit Sub
	End If
	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Set objStatus = objWMIService.Get("Win32_PingStatus.Address='" & lst_servers.value & "',BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute='',SourceRouteType=0,Timeout=1000,TimestampRoute=0,TimeToLive=128,TypeofService=128")
	If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
		Exit Sub
	End If
	serviceDiv.style.display = "inline"
	' get TBODY for the table
	If tableDiv.innerhtml <> "" Then
		tableDiv.innerhtml = ""
	End If
	Set oTable = document.createElement("<TABLE>")
	tableDiv.appendChild(oTable)
	Set oTBody = document.createElement("TBODY")
	oTable.appendChild(oTBody)

	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
	Set colServiceList = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
	For Each objServiceList In colServiceList
		' Check for start mode of the windows base service
 	 	' Don't show services with status "Boot", "System" or "Disabled"
 	 	If (objServiceList.StartMode = "Auto" Or objServiceList.StartMode = "Manual") Then
			' Check for current status of the object
			' Don't show services with status "Error", "Degraded", "Unknown", "Pred Fail", "Starting", "Stopping" or "Service"
	 	 	If objServiceList.Status = "OK" Then
				Set oRow = document.createElement("<TR>")
		 	 	oTBody.appendChild(oRow)
		 	 	Set oCell = document.createElement("<TD align='left' bgcolor='#f0f0f0'>")
		 	 	oCell.innerText = objServiceList.DisplayName
		 	 	oRow.appendChild(oCell)
				Select Case objServiceList.State
					Case "Running"
						strBGCOLOR = "#00cd00"
					Case "Stopped"
						strBGCOLOR = "#fd0000"
					'"Start Pending", "Stop Pending", "Continue Pending", "Pause Pending", "Paused", "Unknown"
					Case Else
						strBGCOLOR = "#ffff00"
				End Select
				Set oCell = document.createElement("<TD BGCOLOR=" & strBGCOLOR & " align=center>")
		 	 	oRow.appendChild(oCell)
		 	 	Set oElem = document.createElement("<SPAN>")
		 	 	oElem.innerText = objServiceList.State
		 	 	oCell.appendChild(oElem)
		 	 	' If service status is not "Running" or "Stopped" disable checkboxes
		 	 	If strBGCOLOR = "#FFFF00" Then
		 	 		Set oElem=document.createElement("<INPUT TYPE='checkbox' NAME='NIC' DISABLED>")
		 	 	Else
		 	 		Set oElem=document.createElement("<INPUT TYPE=checkbox NAME='NIC' VALUE='" & objServiceList.Name & "'>")
				End If
		 	 	oCell.appendChild(oElem)
			End If
		End If
	Next
End Sub

Sub startService
    Set group = document.getElementsByName("NIC")
	For i = 0 To group.length - 1
		If group(i).checked = True And group(i).parentNode.bgColor = "#fd0000" Then
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
			Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
			If RecursiveServiceStart(objService) = True Then
				group(i).parentNode.bgColor = "#00cd00"
				group(i).parentNode.childNodes(0).innertext = "Running"
			End If
			group(i).checked = False
		End If
	Next
End Sub

Sub stopService(strRestart)
    Set group = document.getElementsByName("NIC")
	For i = 0 To group.length - 1
		If group(i).checked = True And group(i).parentNode.bgColor = "#00cd00" Then
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
			Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
			If RecursiveServiceStop(objService) = True Then
				If strRestart = "false" Then
					group(i).checked = False
				End If
			End If
		End If
	Next
End Sub

Sub restartService
	stopService("true")
	startService
End Sub

Function RecursiveServiceStart(objSvc)
	If objSvc.State = "Stopped" Then
		If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") Then
			intRC = objSvc.StartService
			If intRC > 0 Then
				MsgBox " Error starting service: " & objSvc.DisplayName
				RecursiveServiceStart = False
				Exit Function
			Else
				MsgBox " Successfully started service: " & objSvc.DisplayName
				RecursiveServiceStart = True
			End If
		End If
	Else
		For Each objGroup In group
			If objSvc.Name = objGroup.value Then
				objGroup.parentNode.bgColor = "#00cd00"
				objGroup.parentNode.childNodes(0).innertext = "Running"
			End If
		Next	
	End If
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Dependent" )
	For each objS in colServices
		RecursiveServiceStart objS
	Next
End Function

Function RecursiveServiceStop(objSvc)
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Antecedent" )
	For Each objS In colServices
		RecursiveServiceStop objS
	Next
	If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") And objSvc.State = "Running" Then
		intRC = objSvc.StopService
		If intRC > 0 Then
			MsgBox " Error stopping service: " & objSvc.DisplayName
			RecursiveServiceStop = False
			Exit Function
		Else 
			MsgBox " Successfully stopped service: " & objSvc.DisplayName
			For Each objGroup In group
				If objSvc.Name = objGroup.value Then
					objGroup.parentNode.bgColor = "#fd0000"
					objGroup.parentNode.childNodes(0).innertext = "Stopped"	
				End If
			Next
			RecursiveServiceStop = True
		End If
	End If
End Function

Sub BuildServerList
	Const ADS_SCOPE_SUBTREE = 2
 
	Set objRootDSE = GetObject("LDAP://RootDSE")
	strDomain = objRootDSE.Get("defaultNamingContext")
	 
	strQuery = "SELECT name, operatingsystem, objectcategory FROM 'LDAP://" & strDomain & _
		"' WHERE operatingSystem = '*Server*' " & _
		"AND objectCategory = 'computer'"
	 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = ("ADsDSOObject")
	objConnection.Open "Active Directory Provider"
	 
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.CommandText = strQuery
	objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
	objCommand.Properties("Page Size") = 999
	objCommand.Properties("Sort On") = "name"
	 
	Set objRecordSet = objCommand.Execute
	 
	'WScript.Echo objRecordSet.RecordCount & " servers found." & vbCrLf
	 
	While Not objRecordSet.EOF
		Set objOption = Document.createElement("OPTION")
		objOption.Text = objRecordSet("name").Value
		objOption.Value = objRecordSet("name").Value
		lst_Servers.Add(objOption)
		objRecordSet.MoveNext
	Wend

End Sub

</script>

<style>
	body
	{
		background-color: #008B8B;
		font-family: Helvetica;
		font-size: 8pt;
		margin-top: 10px;
		margin-left: 20px;
		margin-right: 10px;
		margin-bottom: 10px;
	}
	TD
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
	LEGEND
	{
		font-family: Trebuchet MS;
		font-size: 10pt;
	}
	Select
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
		width:195px
	}
	INPUT
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
</style>

<body>
	<div>
		Computer name: <select id="lst_servers" name="lst_servers" size=1 style="width:150px">
			<option id="opt_select" value="opt_select"> --- Select a server --- </option>
		</select>&nbsp;&nbsp;&nbsp;
		<input type="button" value="Get Services" onclick="getServices" title="Click to Restart the Services">
		<br>
		<input type="button" value="Start" onclick="startService" title="Click to Start the Services">
		<input type="button" value="Stop" onclick="stopService('false')" title="Click to Stop the Service">
		<input type="button" value="Restart" onclick="restartService" title="Click to Restart the Services">
	</div>
	<div id="serviceDiv" style="position: absolute; left: 4%; top: 10%; width: 100%; height: 89%; overflow-y: scroll; scrollbar-arrow-color: blue; scrollbar-face-color: #e7e7e7; scrollbar-3dlight-color: #a0a0a0; scrollbar-darkshadow-color: #888888">
		<table border="0" cellspacing="0" CellSpacing="0">
		<tr>
			<td valign="top" colspan="3">
				<fieldset>
					<legend><b>Service List</b></legend>
					<div id = "tableDiv"></div>
				</fieldset>
			</td>
		</tr>
		</table>
	</div>
</body>
</html>

Open in new window

0
Syed AltamashAuthor Commented:
Rob, thanks for replying.

I have around 50 services that will need to be listed in the HTA . The services will be common for all the server (though not all services will be running on all servers). We can trim down the services if using 50 services make the code more complex.

When trying to run the HTA on my local machine,
The specified domain either does not exist or could not be contacted.
. When I try to run this on my production servers, the script just hangs presumably because there are going to be thousands of servers on the domain ("IUSER" in this case). Would it not be better if we hardcode the hostnames in the dropdown list? There aren't going to be more than 25 servers that I need to check/restart services on.

Alternatively, would it be possible to load the servers and services from an external file?
0
RobSampsonCommented:
Hi, not sure why it hung on you, but here's a version that will read a config file and build the server and services list from that.
<html>
<head>
<title>ABL Server Service Checker</title>
<HTA:APPLICATION
	ID = "ServiceCheckerApp"
	APPLICATIONNAME="Service Check"
	BORDER = "thin"
	CAPTION = "yes"
	RESIZE = "no"
	SHOWINTASKBAR = "yes"
	SINGLEINSTANCE = "no"
	SYSMENU = "Yes"
	WINDOWSTATE = "normal"
	SCROLL = "no"
	VERSION = "1.0"
	INNERBORDER = "no"
	SELECTION = "no"
	MAXIMIZEBUTTON = "no"
	MINIMIZEBUTTON = "yes"
	NAVIGABLE = "yes"
	CONTEXTMENU = "yes"
	BORDERSTYLE = "normal">
</HTA>

<script language="VBScript">

Dim objWMIService, group, arrServers, arrServices, strHTAPath, dctServers, dctServices

Sub Window_OnLoad
	Set dctServers = CreateObject("Scripting.Dictionary")
	dctServers.CompareMode = vbTextCompare
	Set dctServices = CreateObject("Scripting.Dictionary")
	dctServices.CompareMode = vbTextCompare
    If Mid(document.location, 6, 3) = "///" Then
    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
    Else
    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
    End If
	width = 425
	height = 700
	x = (window.screen.width - width) / 2
	y = (window.screen.height - height) / 2
	If x < 0 Then x = 0
	If y < 0 Then y = 0
	window.resizeTo width,height
	window.moveTo x,y
	'strComputer.value = CreateObject("WScript.Network").ComputerName
	serviceDiv.focus
	serviceDiv.style.display = "none"
	BuildServerList
End Sub

Sub getServices
	If lst_servers.value = "" Then
		Exit Sub
	End If
	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Set objStatus = objWMIService.Get("Win32_PingStatus.Address='" & lst_servers.value & "',BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute='',SourceRouteType=0,Timeout=1000,TimestampRoute=0,TimeToLive=128,TypeofService=128")
	If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
		MsgBox "Unable to communicate with " & lst_servers.value
		Exit Sub
	End If
	serviceDiv.style.display = "inline"
	' get TBODY for the table
	If tableDiv.innerhtml <> "" Then
		tableDiv.innerhtml = ""
	End If
	Set oTable = document.createElement("<TABLE>")
	tableDiv.appendChild(oTable)
	Set oTBody = document.createElement("TBODY")
	oTable.appendChild(oTBody)

	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
	strWhere = "Where"
	For Each strService In dctServices
		If strWhere <> "Where" Then strWhere = strWhere & " OR "
		strWhere = strWhere & " DisplayName='" & strService & "' OR Name='" & strService & "'"
	Next
	'MsgBox "SELECT * FROM Win32_Service " & strWhere
	Set colServiceList = objWMIService.ExecQuery("SELECT * FROM Win32_Service " & strWhere)
	For Each objServiceList In colServiceList
		' Check for start mode of the windows base service
 	 	' Don't show services with status "Boot", "System" or "Disabled"
 	 	If (objServiceList.StartMode = "Auto" Or objServiceList.StartMode = "Manual") Then
			' Check for current status of the object
			' Don't show services with status "Error", "Degraded", "Unknown", "Pred Fail", "Starting", "Stopping" or "Service"
	 	 	If objServiceList.Status = "OK" Then
				Set oRow = document.createElement("<TR>")
		 	 	oTBody.appendChild(oRow)
		 	 	Set oCell = document.createElement("<TD align='left' bgcolor='#f0f0f0'>")
		 	 	oCell.innerText = objServiceList.DisplayName
		 	 	oRow.appendChild(oCell)
				Select Case objServiceList.State
					Case "Running"
						strBGCOLOR = "#00cd00"
					Case "Stopped"
						strBGCOLOR = "#fd0000"
					'"Start Pending", "Stop Pending", "Continue Pending", "Pause Pending", "Paused", "Unknown"
					Case Else
						strBGCOLOR = "#ffff00"
				End Select
				Set oCell = document.createElement("<TD BGCOLOR=" & strBGCOLOR & " align=center>")
		 	 	oRow.appendChild(oCell)
		 	 	Set oElem = document.createElement("<SPAN>")
		 	 	oElem.innerText = objServiceList.State
		 	 	oCell.appendChild(oElem)
		 	 	' If service status is not "Running" or "Stopped" disable checkboxes
		 	 	If strBGCOLOR = "#FFFF00" Then
		 	 		Set oElem=document.createElement("<INPUT TYPE='checkbox' NAME='NIC' DISABLED>")
		 	 	Else
		 	 		Set oElem=document.createElement("<INPUT TYPE=checkbox NAME='NIC' VALUE='" & objServiceList.Name & "'>")
				End If
		 	 	oCell.appendChild(oElem)
			End If
		End If
	Next
End Sub

Sub startService
    Set group = document.getElementsByName("NIC")
	For i = 0 To group.length - 1
		If group(i).checked = True And group(i).parentNode.bgColor = "#fd0000" Then
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
			Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
			If RecursiveServiceStart(objService) = True Then
				group(i).parentNode.bgColor = "#00cd00"
				group(i).parentNode.childNodes(0).innertext = "Running"
			End If
			group(i).checked = False
		End If
	Next
End Sub

Sub stopService(strRestart)
    Set group = document.getElementsByName("NIC")
	For i = 0 To group.length - 1
		If group(i).checked = True And group(i).parentNode.bgColor = "#00cd00" Then
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
			Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
			If RecursiveServiceStop(objService) = True Then
				If strRestart = "false" Then
					group(i).checked = False
				End If
			End If
		End If
	Next
End Sub

Sub restartService
	stopService("true")
	startService
End Sub

Function RecursiveServiceStart(objSvc)
	If objSvc.State = "Stopped" Then
		If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") Then
			intRC = objSvc.StartService
			If intRC > 0 Then
				MsgBox " Error starting service: " & objSvc.DisplayName
				RecursiveServiceStart = False
				Exit Function
			Else
				MsgBox " Successfully started service: " & objSvc.DisplayName
				RecursiveServiceStart = True
			End If
		End If
	Else
		For Each objGroup In group
			If objSvc.Name = objGroup.value Then
				objGroup.parentNode.bgColor = "#00cd00"
				objGroup.parentNode.childNodes(0).innertext = "Running"
			End If
		Next	
	End If
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Dependent" )
	For each objS in colServices
		RecursiveServiceStart objS
	Next
End Function

Function RecursiveServiceStop(objSvc)
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Antecedent" )
	For Each objS In colServices
		RecursiveServiceStop objS
	Next
	If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") And objSvc.State = "Running" Then
		intRC = objSvc.StopService
		If intRC > 0 Then
			MsgBox " Error stopping service: " & objSvc.DisplayName
			RecursiveServiceStop = False
			Exit Function
		Else 
			MsgBox " Successfully stopped service: " & objSvc.DisplayName
			For Each objGroup In group
				If objSvc.Name = objGroup.value Then
					objGroup.parentNode.bgColor = "#fd0000"
					objGroup.parentNode.childNodes(0).innertext = "Stopped"	
				End If
			Next
			RecursiveServiceStop = True
		End If
	End If
End Function

Sub BuildServerList
	strConfigFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "ServiceControlSettings.cfg"
	arrSections = Array("servers", "services")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists(strConfigFile) = True Then
		Set objConfig = objFSO.OpenTextFile(strConfigFile, 1, False)
		strMode = ""
		While Not objConfig.AtEndOfStream
			strLine = Trim(objConfig.ReadLine)
			For Each strSection In arrSections
				If LCase(strLine) = "[" & LCase(strSection) & "]" Then strMode = strSection
			Next
			If strMode <> "" And strLine <> "" And Left(strLine, 1) <> "[" Then
				Execute "dct" & strMode & ".Add """ & strLine & """, 0"
			Else
				If Left(strLine, 1) <> "[" And strLine <> "" Then MsgBox "Unable to determine section in configuration file for " & strLine
			End If
		Wend
		objConfig.Close
	Else
		MsgBox "Unable to find configuration file" & vbCrLf & strConfigFile
	End If
	If dctServers.Count = 0 Or dctServices.Count = 0 Then
		If dctServers.Count = 0 Then
			MsgBox "No servers were found in the configuration file."
		Else
			MsgBox "No services were found in the configuration file."
		End If
	Else
		For Each strServer In dctServers.Keys
			Set objOption = Document.createElement("OPTION")
			objOption.Text = strServer
			objOption.Value = strServer
			lst_Servers.Add(objOption)
		Next
	End If
End Sub

</script>

<style>
	body
	{
		background-color: #008B8B;
		font-family: Helvetica;
		font-size: 8pt;
		margin-top: 10px;
		margin-left: 20px;
		margin-right: 10px;
		margin-bottom: 10px;
	}
	TD
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
	LEGEND
	{
		font-family: Trebuchet MS;
		font-size: 10pt;
	}
	Select
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
		width:195px
	}
	INPUT
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
</style>

<body>
	<div>
		Computer name: <select id="lst_servers" name="lst_servers" size=1 style="width:150px">
			<option id="opt_select" value="opt_select"> --- Select a server --- </option>
		</select>&nbsp;&nbsp;&nbsp;
		<input type="button" value="Get Services" onclick="getServices" title="Click to Restart the Services">
		<br>
		<input type="button" value="Start" onclick="startService" title="Click to Start the Services">
		<input type="button" value="Stop" onclick="stopService('false')" title="Click to Stop the Service">
		<input type="button" value="Restart" onclick="restartService" title="Click to Restart the Services">
	</div>
	<div id="serviceDiv" style="position: absolute; left: 4%; top: 10%; width: 100%; height: 89%; overflow-y: scroll; scrollbar-arrow-color: blue; scrollbar-face-color: #e7e7e7; scrollbar-3dlight-color: #a0a0a0; scrollbar-darkshadow-color: #888888">
		<table border="0" cellspacing="0" CellSpacing="0">
		<tr>
			<td valign="top" colspan="3">
				<fieldset>
					<legend><b>Service List</b></legend>
					<div id = "tableDiv"></div>
				</fieldset>
			</td>
		</tr>
		</table>
	</div>
</body>
</html>

Open in new window


There is a line that says
strConfigFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "ServiceControlSettings.cfg"

Open in new window


So with ServiceControlSettings.cfg in the same folder as the HTA file, it will pick that up and read it.

The format of the file is as follows:

[Servers]
Server1
Server2

[Services]
Print Spooler
Windows Update

So [Servers] and [Services] define the different sections for you to list your required items.

See how that goes for you.

Regards,

Rob.
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Syed AltamashAuthor Commented:
This work perfectly Rob. Thanks man.

There are a few things that I'd like to highlight (if it is not too much to ask).

1. The services do not stop/start in the order they are selected. The service start based on the way they are listed. This is needed (for me at least) because of the legacy nature of my application, there are a  few services that have a message server and then 10 gateways. So the process to restart if first stop the message server and then the gateways. So, is it possible to stop/start the services depending on the order in which they are checked?

2. While the services are stopping/starting, I'd think it would be nice if we have a prompt saying the service is starting/stopping. As things stand, there is no such prompt and the prompt is only received after the service has stopped/started.

3. Finally, if I select multiple services, then I get a prompt after each service is stopped/started. This become tedious specially if i have checked 20-30 services to be started/stopped.

These requests can be discarded by you if you feel they aren't worth the effort. In such case, I'll select the above reply as the best solution.

Thanks Rob :)

Regards.
0
RobSampsonCommented:
Hi, that's a challenge!  :-)

I've put something together, which appears to work in my limited testing.  Give this HTA a go and see what it does for you.

Regards,

Rob.

<html>
<head>
<title>ABL Server Service Checker</title>
<HTA:APPLICATION
	ID = "ServiceCheckerApp"
	APPLICATIONNAME="Service Check"
	BORDER = "thin"
	CAPTION = "yes"
	RESIZE = "no"
	SHOWINTASKBAR = "yes"
	SINGLEINSTANCE = "no"
	SYSMENU = "Yes"
	WINDOWSTATE = "normal"
	SCROLL = "no"
	VERSION = "1.0"
	INNERBORDER = "no"
	SELECTION = "no"
	MAXIMIZEBUTTON = "no"
	MINIMIZEBUTTON = "yes"
	NAVIGABLE = "yes"
	CONTEXTMENU = "yes"
	BORDERSTYLE = "normal">
</HTA>

<script language="VBScript">

Dim objWMIService, group, arrServers, arrServices, strHTAPath, dctServers, dctServices

Sub Window_OnLoad
	Set dctServers = CreateObject("Scripting.Dictionary")
	dctServers.CompareMode = vbTextCompare
	Set dctServices = CreateObject("Scripting.Dictionary")
	dctServices.CompareMode = vbTextCompare
    If Mid(document.location, 6, 3) = "///" Then
    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
    Else
    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
    End If
	width = 600
	height = 700
	x = (window.screen.width - width) / 2
	y = (window.screen.height - height) / 2
	If x < 0 Then x = 0
	If y < 0 Then y = 0
	window.resizeTo width,height
	window.moveTo x,y
	'strComputer.value = CreateObject("WScript.Network").ComputerName
	serviceDiv.focus
	serviceDiv.style.display = "none"
	BuildServerList
End Sub

Sub getServices
	progressDIV.innertext = ""
	ClearOrderList
	If lst_servers.value = "" Then
		Exit Sub
	End If
	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Set objStatus = objWMIService.Get("Win32_PingStatus.Address='" & lst_servers.value & "',BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute='',SourceRouteType=0,Timeout=1000,TimestampRoute=0,TimeToLive=128,TypeofService=128")
	If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
		MsgBox "Unable to communicate with " & lst_servers.value
		Exit Sub
	End If
	serviceDiv.style.display = "inline"
	' get TBODY for the table
	If tableDiv.innerhtml <> "" Then
		tableDiv.innerhtml = ""
	End If
	Set oTable = document.createElement("<TABLE>")
	tableDiv.appendChild(oTable)
	Set oTBody = document.createElement("TBODY")
	oTable.appendChild(oTBody)

	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
	strWhere = "Where"
	For Each strService In dctServices
		If strWhere <> "Where" Then strWhere = strWhere & " OR "
		strWhere = strWhere & " DisplayName='" & strService & "' OR Name='" & strService & "'"
	Next
	'MsgBox "SELECT * FROM Win32_Service " & strWhere
	Set colServiceList = objWMIService.ExecQuery("SELECT * FROM Win32_Service " & strWhere)
	For Each objServiceList In colServiceList
		' Check for start mode of the windows base service
 	 	' Don't show services with status "Boot", "System" or "Disabled"
 	 	If (objServiceList.StartMode = "Auto" Or objServiceList.StartMode = "Manual") Then
			' Check for current status of the object
			' Don't show services with status "Error", "Degraded", "Unknown", "Pred Fail", "Starting", "Stopping" or "Service"
	 	 	If objServiceList.Status = "OK" Then
				Set oRow = document.createElement("<TR>")
		 	 	oTBody.appendChild(oRow)
		 	 	Set oCell = document.createElement("<TD align='left' bgcolor='#f0f0f0'>")
		 	 	oCell.innerText = objServiceList.DisplayName
		 	 	oRow.appendChild(oCell)
				Select Case objServiceList.State
					Case "Running"
						strBGCOLOR = "#00cd00"
					Case "Stopped"
						strBGCOLOR = "#fd0000"
					'"Start Pending", "Stop Pending", "Continue Pending", "Pause Pending", "Paused", "Unknown"
					Case Else
						strBGCOLOR = "#ffff00"
				End Select
				Set oCell = document.createElement("<TD BGCOLOR=" & strBGCOLOR & " align=center>")
		 	 	oRow.appendChild(oCell)
		 	 	Set oElem = document.createElement("<SPAN>")
		 	 	oElem.innerText = objServiceList.State
		 	 	oCell.appendChild(oElem)
		 	 	' If service status is not "Running" or "Stopped" disable checkboxes
		 	 	If strBGCOLOR = "#FFFF00" Then
		 	 		Set oElem=document.createElement("<INPUT TYPE='checkbox' NAME='NIC' DISABLED>")
		 	 	Else
		 	 		Set oElem=document.createElement("<INPUT TYPE=checkbox NAME='NIC' VALUE='" & objServiceList.Name & "' onClick='OrderList(""" & objServiceList.Name & "|" & objServiceList.DisplayName & """)'>")
				End If
		 	 	oCell.appendChild(oElem)
			End If
		End If
	Next
End Sub

Sub OrderList(strValues)
	strName = Split(strValues, "|")(0)
	strDisplayName = Split(strValues, "|")(1)
	Set group = document.getElementsByName("NIC")
	For i = 0 To group.length - 1
		If group(i).value = strName And group(i).checked = True Then
			Set objOption = Document.createElement("OPTION")
			objOption.Text = strDisplayName
			objOption.Value = strName
			lst_order.Add(objOption)
		ElseIf group(i).value = strName And group(i).checked = False Then
			For Each objOption In lst_order.Options
				If objOption.Value = strName Then objOption.RemoveNode
			Next
		End If
	Next
End Sub

Sub ClearOrderList
	For Each objOption In lst_order.Options
		objOption.RemoveNode
	Next
End Sub

Sub startService
    Set group = document.getElementsByName("NIC")
	For Each objOption In lst_order.options
		For i = 0 To group.length - 1
			If group(i).value = objOption.value And group(i).checked = True And group(i).parentNode.bgColor = "#fd0000" Then
				Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
				Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
				If RecursiveServiceStart(objService) = True Then
					group(i).parentNode.bgColor = "#00cd00"
					group(i).parentNode.childNodes(0).innertext = "Running"
				End If
				group(i).checked = False
			End If
		Next
	Next
	'ClearOrderList
End Sub

Sub stopService(strRestart)
    Set group = document.getElementsByName("NIC")
    For Each objOption In lst_order.options
		For i = 0 To group.length - 1
			If group(i).value = objOption.value And group(i).checked = True And group(i).parentNode.bgColor = "#00cd00" Then
				Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
				Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
				If RecursiveServiceStop(objService) = True Then
					If strRestart = "false" Then
						group(i).checked = False
					End If
				End If
			End If
		Next
	Next
	If strRestart = "false" Then ClearOrderList
End Sub

Sub restartService
	stopService("true")
	startService
End Sub

Function RecursiveServiceStart(objSvc)
	If objSvc.State = "Stopped" Then
		If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") Then
			intRC = objSvc.StartService
			If intRC > 0 Then
				progressDIV.innertext = progressDIV.innertext & vbCrLf & " Error starting service: " & objSvc.DisplayName
				RecursiveServiceStart = False
				Exit Function
			Else
				progressDIV.innertext = progressDIV.innertext & vbCrLf & " Successfully started service: " & objSvc.DisplayName
				RecursiveServiceStart = True
			End If
		End If
	Else
		For Each objGroup In group
			If objSvc.Name = objGroup.value Then
				objGroup.parentNode.bgColor = "#00cd00"
				objGroup.parentNode.childNodes(0).innertext = "Running"
			End If
		Next	
	End If
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Dependent" )
	For each objS in colServices
		RecursiveServiceStart objS
	Next
End Function

Function RecursiveServiceStop(objSvc)
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Antecedent" )
	For Each objS In colServices
		RecursiveServiceStop objS
	Next
	If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") And objSvc.State = "Running" Then
		intRC = objSvc.StopService
		If intRC > 0 Then
			progressDIV.innertext = progressDIV.innertext & vbCrLf & " Error stopping service: " & objSvc.DisplayName & " - Error: " & intRC
			RecursiveServiceStop = False
			Exit Function
		Else 
			progressDIV.innertext = progressDIV.innertext & vbCrLf & " Successfully stopped service: " & objSvc.DisplayName
			For Each objGroup In group
				If objSvc.Name = objGroup.value Then
					objGroup.parentNode.bgColor = "#fd0000"
					objGroup.parentNode.childNodes(0).innertext = "Stopped"	
				End If
			Next
			RecursiveServiceStop = True
		End If
	End If
End Function

Sub BuildServerList
	strConfigFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "ServiceControlSettings.cfg"
	arrSections = Array("servers", "services")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists(strConfigFile) = True Then
		Set objConfig = objFSO.OpenTextFile(strConfigFile, 1, False)
		strMode = ""
		While Not objConfig.AtEndOfStream
			strLine = Trim(objConfig.ReadLine)
			For Each strSection In arrSections
				If LCase(strLine) = "[" & LCase(strSection) & "]" Then strMode = strSection
			Next
			If strMode <> "" And strLine <> "" And Left(strLine, 1) <> "[" Then
				Execute "dct" & strMode & ".Add """ & strLine & """, 0"
			Else
				If Left(strLine, 1) <> "[" And strLine <> "" Then MsgBox "Unable to determine section in configuration file for " & strLine
			End If
		Wend
		objConfig.Close
	Else
		MsgBox "Unable to find configuration file" & vbCrLf & strConfigFile
	End If
	If dctServers.Count = 0 Or dctServices.Count = 0 Then
		If dctServers.Count = 0 Then
			MsgBox "No servers were found in the configuration file."
		Else
			MsgBox "No services were found in the configuration file."
		End If
	Else
		For Each strServer In dctServers.Keys
			Set objOption = Document.createElement("OPTION")
			objOption.Text = strServer
			objOption.Value = strServer
			lst_Servers.Add(objOption)
		Next
	End If
End Sub

</script>

<style>
	body
	{
		background-color: #008B8B;
		font-family: Helvetica;
		font-size: 8pt;
		margin-top: 10px;
		margin-left: 20px;
		margin-right: 10px;
		margin-bottom: 10px;
	}
	TD
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
	LEGEND
	{
		font-family: Trebuchet MS;
		font-size: 10pt;
	}
	Select
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
		width:195px
	}
	INPUT
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
</style>

<body>
	<div>
		Computer name: <select id="lst_servers" name="lst_servers" size=1 style="width:150px">
			<option id="opt_select" value="opt_select"> --- Select a server --- </option>
		</select>&nbsp;&nbsp;&nbsp;
		<input type="button" value="Get Services" onclick="getServices" title="Click to Restart the Services">
		<br>
		<input type="button" value="Start" onclick="startService" title="Click to Start the Services">
		<input type="button" value="Stop" onclick="stopService('false')" title="Click to Stop the Service">
		<input type="button" value="Restart" onclick="restartService" title="Click to Restart the Services">
	</div>
	<div id="serviceDiv" style="position: absolute; left: 4%; top: 10%; width: 100%; height: 89%; overflow-y: scroll; scrollbar-arrow-color: blue; scrollbar-face-color: #e7e7e7; scrollbar-3dlight-color: #a0a0a0; scrollbar-darkshadow-color: #888888">
		<table border="0" cellspacing="0" CellSpacing="0">
		<tr>
			<td valign="top" colspan="3">
				<fieldset>
					<legend><b>Service List</b></legend>
					<div id = "tableDiv"></div>
				</fieldset>
			</td>
			<td>
				<fieldset>
					<legend><b>Operation Order</b></legend>
					<div id = "orderDiv">
						<table>
							<tr>
								<td>
									<select id="lst_order" name="lst_order" size=10 style="width:200px">
									</select>
								</td>
<!--
								<td>
									<input type="button" value="Move to top" onclick="MoveTop" title="Move to top" style="width:100px"><br><br>
									<input type="button" value="Move up" onclick="MoveUp" title="Move up" style="width:100px"><br><br>
									<input type="button" value="Move down" onclick="MoveDown" title="Move down" style="width:100px"><br><br>
									<input type="button" value="Move bottom" onclick="MoveBottom" title="Move to bottom" style="width:100px">
								</td>
-->
							</tr>
						</table>
					</div>
				</fieldset>				
			</td>
		</tr>
		<tr>
			<td valign="top" colspan="4">
				<br>
				<fieldset>
					<legend><b>Progress</b></legend>
					<div id = "progressDiv"></div>
				</fieldset>
			</td>
		<tr>
		</table>
	</div>
</body>
</html>

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
Syed AltamashAuthor Commented:
Thanks for this Rob.. Will test it later in the day and get back to you with the results
0
Syed AltamashAuthor Commented:
Thanks for this Rob. This works brilliantly and is almost what i needed so a big thanks for this.

But, not sure what happened to this HTA. Lately, i have been receiving an error when i try to open this application which reads This Key is already associated with an element of this collection which seems weird because it was working earlier absolutely fine and i have made no changes whatsoever to the code
0
RobSampsonCommented:
Hi, I'd say you have a computer or service listed twice in the config file. Have a look at that and see how you go.

Rob.
0
Syed AltamashAuthor Commented:
Rob,

You were right. A service was duplicated in there.

Gonna close the question now and mark the best answer.

Thanks for all the help :)

Cheers mate!!!
0
RobSampsonCommented:
No problem. I could put in error checking to ignore that and prompt you if you needed, instead of crashing.

Thanks for the grade.

Rob.
0
Syed AltamashAuthor Commented:
That's the least i could do :)

That'd be great if you could.. would help other later on :)
0
RobSampsonCommented:
OK sure, here it is with that check.

<html>
<head>
<title>ABL Server Service Checker</title>
<HTA:APPLICATION
	ID = "ServiceCheckerApp"
	APPLICATIONNAME="Service Check"
	BORDER = "thin"
	CAPTION = "yes"
	RESIZE = "no"
	SHOWINTASKBAR = "yes"
	SINGLEINSTANCE = "no"
	SYSMENU = "Yes"
	WINDOWSTATE = "normal"
	SCROLL = "no"
	VERSION = "1.0"
	INNERBORDER = "no"
	SELECTION = "no"
	MAXIMIZEBUTTON = "no"
	MINIMIZEBUTTON = "yes"
	NAVIGABLE = "yes"
	CONTEXTMENU = "yes"
	BORDERSTYLE = "normal">
</HTA>

<script language="VBScript">

Dim objWMIService, group, arrServers, arrServices, strHTAPath, dctServers, dctServices

Sub Window_OnLoad
	Set dctServers = CreateObject("Scripting.Dictionary")
	dctServers.CompareMode = vbTextCompare
	Set dctServices = CreateObject("Scripting.Dictionary")
	dctServices.CompareMode = vbTextCompare
    If Mid(document.location, 6, 3) = "///" Then
    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
    Else
    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
    End If
	width = 600
	height = 700
	x = (window.screen.width - width) / 2
	y = (window.screen.height - height) / 2
	If x < 0 Then x = 0
	If y < 0 Then y = 0
	window.resizeTo width,height
	window.moveTo x,y
	'strComputer.value = CreateObject("WScript.Network").ComputerName
	serviceDiv.focus
	serviceDiv.style.display = "none"
	BuildServerList
End Sub

Sub getServices
	progressDIV.innertext = ""
	ClearOrderList
	If lst_servers.value = "" Then
		Exit Sub
	End If
	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Set objStatus = objWMIService.Get("Win32_PingStatus.Address='" & lst_servers.value & "',BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute='',SourceRouteType=0,Timeout=1000,TimestampRoute=0,TimeToLive=128,TypeofService=128")
	If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
		MsgBox "Unable to communicate with " & lst_servers.value
		Exit Sub
	End If
	serviceDiv.style.display = "inline"
	' get TBODY for the table
	If tableDiv.innerhtml <> "" Then
		tableDiv.innerhtml = ""
	End If
	Set oTable = document.createElement("<TABLE>")
	tableDiv.appendChild(oTable)
	Set oTBody = document.createElement("TBODY")
	oTable.appendChild(oTBody)

	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
	strWhere = "Where"
	For Each strService In dctServices
		If strWhere <> "Where" Then strWhere = strWhere & " OR "
		strWhere = strWhere & " DisplayName='" & strService & "' OR Name='" & strService & "'"
	Next
	'MsgBox "SELECT * FROM Win32_Service " & strWhere
	Set colServiceList = objWMIService.ExecQuery("SELECT * FROM Win32_Service " & strWhere)
	For Each objServiceList In colServiceList
		' Check for start mode of the windows base service
 	 	' Don't show services with status "Boot", "System" or "Disabled"
 	 	If (objServiceList.StartMode = "Auto" Or objServiceList.StartMode = "Manual") Then
			' Check for current status of the object
			' Don't show services with status "Error", "Degraded", "Unknown", "Pred Fail", "Starting", "Stopping" or "Service"
	 	 	If objServiceList.Status = "OK" Then
				Set oRow = document.createElement("<TR>")
		 	 	oTBody.appendChild(oRow)
		 	 	Set oCell = document.createElement("<TD align='left' bgcolor='#f0f0f0'>")
		 	 	oCell.innerText = objServiceList.DisplayName
		 	 	oRow.appendChild(oCell)
				Select Case objServiceList.State
					Case "Running"
						strBGCOLOR = "#00cd00"
					Case "Stopped"
						strBGCOLOR = "#fd0000"
					'"Start Pending", "Stop Pending", "Continue Pending", "Pause Pending", "Paused", "Unknown"
					Case Else
						strBGCOLOR = "#ffff00"
				End Select
				Set oCell = document.createElement("<TD BGCOLOR=" & strBGCOLOR & " align=center>")
		 	 	oRow.appendChild(oCell)
		 	 	Set oElem = document.createElement("<SPAN>")
		 	 	oElem.innerText = objServiceList.State
		 	 	oCell.appendChild(oElem)
		 	 	' If service status is not "Running" or "Stopped" disable checkboxes
		 	 	If strBGCOLOR = "#FFFF00" Then
		 	 		Set oElem=document.createElement("<INPUT TYPE='checkbox' NAME='NIC' DISABLED>")
		 	 	Else
		 	 		Set oElem=document.createElement("<INPUT TYPE=checkbox NAME='NIC' VALUE='" & objServiceList.Name & "' onClick='OrderList(""" & objServiceList.Name & "|" & objServiceList.DisplayName & """)'>")
				End If
		 	 	oCell.appendChild(oElem)
			End If
		End If
	Next
End Sub

Sub OrderList(strValues)
	strName = Split(strValues, "|")(0)
	strDisplayName = Split(strValues, "|")(1)
	Set group = document.getElementsByName("NIC")
	For i = 0 To group.length - 1
		If group(i).value = strName And group(i).checked = True Then
			Set objOption = Document.createElement("OPTION")
			objOption.Text = strDisplayName
			objOption.Value = strName
			lst_order.Add(objOption)
		ElseIf group(i).value = strName And group(i).checked = False Then
			For Each objOption In lst_order.Options
				If objOption.Value = strName Then objOption.RemoveNode
			Next
		End If
	Next
End Sub

Sub ClearOrderList
	For Each objOption In lst_order.Options
		objOption.RemoveNode
	Next
End Sub

Sub startService
    Set group = document.getElementsByName("NIC")
	For Each objOption In lst_order.options
		For i = 0 To group.length - 1
			If group(i).value = objOption.value And group(i).checked = True And group(i).parentNode.bgColor = "#fd0000" Then
				Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
				Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
				If RecursiveServiceStart(objService) = True Then
					group(i).parentNode.bgColor = "#00cd00"
					group(i).parentNode.childNodes(0).innertext = "Running"
				End If
				group(i).checked = False
			End If
		Next
	Next
	'ClearOrderList
End Sub

Sub stopService(strRestart)
    Set group = document.getElementsByName("NIC")
    For Each objOption In lst_order.options
		For i = 0 To group.length - 1
			If group(i).value = objOption.value And group(i).checked = True And group(i).parentNode.bgColor = "#00cd00" Then
				Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & lst_servers.value & "\root\cimv2")
				Set objService = objWMIService.Get("Win32_Service.Name='" & group(i).value & "'")
				If RecursiveServiceStop(objService) = True Then
					If strRestart = "false" Then
						group(i).checked = False
					End If
				End If
			End If
		Next
	Next
	If strRestart = "false" Then ClearOrderList
End Sub

Sub restartService
	stopService("true")
	startService
End Sub

Function RecursiveServiceStart(objSvc)
	If objSvc.State = "Stopped" Then
		If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") Then
			intRC = objSvc.StartService
			If intRC > 0 Then
				progressDIV.innertext = progressDIV.innertext & vbCrLf & " Error starting service: " & objSvc.DisplayName
				RecursiveServiceStart = False
				Exit Function
			Else
				progressDIV.innertext = progressDIV.innertext & vbCrLf & " Successfully started service: " & objSvc.DisplayName
				RecursiveServiceStart = True
			End If
		End If
	Else
		For Each objGroup In group
			If objSvc.Name = objGroup.value Then
				objGroup.parentNode.bgColor = "#00cd00"
				objGroup.parentNode.childNodes(0).innertext = "Running"
			End If
		Next	
	End If
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Dependent" )
	For each objS in colServices
		RecursiveServiceStart objS
	Next
End Function

Function RecursiveServiceStop(objSvc)
	Set colServices = objWMIService.ExecQuery("Associators of " _
			& "{Win32_Service.Name='" & objSvc.Name & "'} Where " _
			& "AssocClass=Win32_DependentService Role=Antecedent" )
	For Each objS In colServices
		RecursiveServiceStop objS
	Next
	If (objSvc.StartMode = "Auto" Or objSvc.StartMode = "Manual") And objSvc.State = "Running" Then
		intRC = objSvc.StopService
		If intRC > 0 Then
			progressDIV.innertext = progressDIV.innertext & vbCrLf & " Error stopping service: " & objSvc.DisplayName & " - Error: " & intRC
			RecursiveServiceStop = False
			Exit Function
		Else 
			progressDIV.innertext = progressDIV.innertext & vbCrLf & " Successfully stopped service: " & objSvc.DisplayName
			For Each objGroup In group
				If objSvc.Name = objGroup.value Then
					objGroup.parentNode.bgColor = "#fd0000"
					objGroup.parentNode.childNodes(0).innertext = "Stopped"	
				End If
			Next
			RecursiveServiceStop = True
		End If
	End If
End Function

Sub BuildServerList
	strConfigFile = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "ServiceControlSettings.cfg"
	arrSections = Array("servers", "services")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists(strConfigFile) = True Then
		Set objConfig = objFSO.OpenTextFile(strConfigFile, 1, False)
		strMode = ""
		While Not objConfig.AtEndOfStream
			strLine = Trim(objConfig.ReadLine)
			For Each strSection In arrSections
				If LCase(strLine) = "[" & LCase(strSection) & "]" Then strMode = strSection
			Next
			If strMode <> "" And strLine <> "" And Left(strLine, 1) <> "[" Then
				Execute "If dct" & strMode & ".Exists(""" & strLine & """) = False Then dct" & strMode & ".Add """ & strLine & """, 0 Else MsgBox ""Duplicate of " & strLine & " found in the " & strMode & " section. Ignoring."""
			Else
				If Left(strLine, 1) <> "[" And strLine <> "" Then MsgBox "Unable to determine section in configuration file for " & strLine
			End If
		Wend
		objConfig.Close
	Else
		MsgBox "Unable to find configuration file" & vbCrLf & strConfigFile
	End If
	If dctServers.Count = 0 Or dctServices.Count = 0 Then
		If dctServers.Count = 0 Then
			MsgBox "No servers were found in the configuration file."
		Else
			MsgBox "No services were found in the configuration file."
		End If
	Else
		For Each strServer In dctServers.Keys
			Set objOption = Document.createElement("OPTION")
			objOption.Text = strServer
			objOption.Value = strServer
			lst_Servers.Add(objOption)
		Next
	End If
End Sub

</script>

<style>
	body
	{
		background-color: #008B8B;
		font-family: Helvetica;
		font-size: 8pt;
		margin-top: 10px;
		margin-left: 20px;
		margin-right: 10px;
		margin-bottom: 10px;
	}
	TD
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
	LEGEND
	{
		font-family: Trebuchet MS;
		font-size: 10pt;
	}
	Select
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
		width:195px
	}
	INPUT
	{
		font-family: Trebuchet MS;
		font-size: 8pt;
	}
</style>

<body>
	<div>
		Computer name: <select id="lst_servers" name="lst_servers" size=1 style="width:150px">
			<option id="opt_select" value="opt_select"> --- Select a server --- </option>
		</select>&nbsp;&nbsp;&nbsp;
		<input type="button" value="Get Services" onclick="getServices" title="Click to Restart the Services">
		<br>
		<input type="button" value="Start" onclick="startService" title="Click to Start the Services">
		<input type="button" value="Stop" onclick="stopService('false')" title="Click to Stop the Service">
		<input type="button" value="Restart" onclick="restartService" title="Click to Restart the Services">
	</div>
	<div id="serviceDiv" style="position: absolute; left: 4%; top: 10%; width: 100%; height: 89%; overflow-y: scroll; scrollbar-arrow-color: blue; scrollbar-face-color: #e7e7e7; scrollbar-3dlight-color: #a0a0a0; scrollbar-darkshadow-color: #888888">
		<table border="0" cellspacing="0" CellSpacing="0">
		<tr>
			<td valign="top" colspan="3">
				<fieldset>
					<legend><b>Service List</b></legend>
					<div id = "tableDiv"></div>
				</fieldset>
			</td>
			<td>
				<fieldset>
					<legend><b>Operation Order</b></legend>
					<div id = "orderDiv">
						<table>
							<tr>
								<td>
									<select id="lst_order" name="lst_order" size=10 style="width:200px">
									</select>
								</td>
<!--
								<td>
									<input type="button" value="Move to top" onclick="MoveTop" title="Move to top" style="width:100px"><br><br>
									<input type="button" value="Move up" onclick="MoveUp" title="Move up" style="width:100px"><br><br>
									<input type="button" value="Move down" onclick="MoveDown" title="Move down" style="width:100px"><br><br>
									<input type="button" value="Move bottom" onclick="MoveBottom" title="Move to bottom" style="width:100px">
								</td>
-->
							</tr>
						</table>
					</div>
				</fieldset>				
			</td>
		</tr>
		<tr>
			<td valign="top" colspan="4">
				<br>
				<fieldset>
					<legend><b>Progress</b></legend>
					<div id = "progressDiv"></div>
				</fieldset>
			</td>
		<tr>
		</table>
	</div>
</body>
</html>

Open in new window

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.