Link to home
Start Free TrialLog in
Avatar of chasmant
chasmant

asked on

HTA with VBScript to stop/start windows services

I saw this script on your site is there anyway to make this work on remote pc?
Avatar of dneamtu
dneamtu
Flag of Romania image

Yes, it can be possible as long as you have access on that PC.
Please note that if the example uses WMI it will be necessary to activate WMI on the remote machine(s).

Please submit the link that you are referring to.
Avatar of chasmant
chasmant

ASKER

I have access to pc  I attached the script can it be made to work on a remote pc
<html><head><title>ABL Server Service Checker</title>
<HTA:APPLICATION
ID = "ServiceCheckerApp"
APPLICATIONNAME="Service Check"
BORDER = "thin"
CAPTION = "yes"
RESIZE = "no"
ICON = "Msn-Messenger.ico"
SHOWINTASKBAR = "yes"
SINGLEINSTANCE = "no"
SYSMENU = "Yes"
WINDOWSTATE = "normal"
SCROLL = "yes"
SCROLLFLAT = "yes"
VERSION = "1.0"
INNERBORDER = "no"
SELECTION = "no"
MAXIMIZEBUTTON = "no"
MINIMIZEBUTTON = "yes"
NAVIGABLE = "yes"
CONTEXTMENU = "yes"
BORDERSTYLE = "normal">
</hta>
<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>
<script language="VBScript">
Const ForAppending = 8
oServer = "PC02205"
Sub Window_OnLoad
Dim width, height, x, y
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
End Sub
Sub startService
service = "????"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & oServer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & service & "'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='" & service & "'} Where AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
objService.StartService()
Next
End Sub
Sub restartService
Msgbox "Restart Me"
End Sub
Sub CloseForm
Window.Close
End Sub
</script>
</head><body>
<tr><td align="left" valign="bottom">
<input type="button" value="Start" onclick="StartService" title=" Click to Start the Services">
<input type="button" value="Stop" onclick="StopService" title=" Click to Stop the Service">
<input type="button" value="Restart" onclick="RestartService" title=" Click to Restart the Services">
<input type="button" value=" Exit " onclick="CloseForm" title=" Click to Exit Form ">
<table border="0" cellspacing="0" CellSpacing="0"><tr><td width="350">
<tr><td valign="top" colspan="3"><fieldset><legend><b>Service List</b></legend>
<table border="0" cellpadding="3" width="350">
<TR><TD align='left' bgcolor='#f0f0f0'>Alerter</TD><td bgcolor=#FD0000 align=center><i>Stopped</i></td><td><input type="checkbox" name="Alerter"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Apple Mobile Device</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Apple Mobile Device"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Windows Audio</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Windows Audio"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Bonjour Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Bonjour Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Computer Browser</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Computer Browser"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>LANDesk(R) Management Agent</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="LANDesk(R) Management Agent"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Citrix Diagnostic Facility COM Server</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Citrix Diagnostic Facility COM Server"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>VMware Capacity Planner Service</TD><td bgcolor=#FD0000 align=center><i>Stopped</i></td><td><input type="checkbox" name="VMware Capacity Planner Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Cryptographic Services</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Cryptographic Services"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>DCOM Server Process Launcher</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="DCOM Server Process Launcher"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>DHCP Client</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="DHCP Client"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Logical Disk Manager</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Logical Disk Manager"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>DNS Client</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="DNS Client"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Event Log</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Event Log"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Fax</TD><td bgcolor=#FD0000 align=center><i>Stopped</i></td><td><input type="checkbox" name="Fax"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>HP Network Devices Support</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="HP Network Devices Support"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>IBM Rapid Restore Ultra Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="IBM Rapid Restore Ultra Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>IBM User Verification Manager</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="IBM User Verification Manager"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Intel Alert Handler</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Intel Alert Handler"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Intel Local Scheduler Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Intel Local Scheduler Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Intel PDS</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Intel PDS"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>LANDesk Targeted Multicast</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="LANDesk Targeted Multicast"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>LANDesk Remote Control Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="LANDesk Remote Control Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Java Quick Starter</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Java Quick Starter"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>LANDesk Policy Invoker</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="LANDesk Policy Invoker"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Server</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Server"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Workstation</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Workstation"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>LANDesk(R) Extended device discovery service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="LANDesk(R) Extended device discovery service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>TCP/IP NetBIOS Helper</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="TCP/IP NetBIOS Helper"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Messenger</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Messenger"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Microsoft Exchange Management</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Microsoft Exchange Management"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Net Logon</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Net Logon"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Plug and Play</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Plug and Play"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>IPSEC Services</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="IPSEC Services"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Protected Storage</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Protected Storage"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Remote Registry</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Remote Registry"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Remote Procedure Call (RPC)</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Remote Procedure Call (RPC)"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Security Accounts Manager</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Security Accounts Manager"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Sophos Anti-Virus status reporter</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Sophos Anti-Virus status reporter"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Sophos Anti-Virus</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Sophos Anti-Virus"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Task Scheduler</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Task Scheduler"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>System Event Notification</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="System Event Notification"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Shell Hardware Detection</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Shell Hardware Detection"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>LANDesk(R) Software Monitoring Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="LANDesk(R) Software Monitoring Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Print Spooler</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Print Spooler"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Windows Image Acquisition (WIA)</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Windows Image Acquisition (WIA)"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Themes</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Themes"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>OfficeScan NT Firewall</TD><td bgcolor=#FD0000 align=center><i>Stopped</i></td><td><input type="checkbox" name="OfficeScan NT Firewall"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Distributed Link Tracking Client</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Distributed Link Tracking Client"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>VMware Converter Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="VMware Converter Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Veeam Backup and FastSCP Service</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Veeam Backup and FastSCP Service"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Protector Suite Virtual Token</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Protector Suite Virtual Token"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Windows Time</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Windows Time"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>Windows Management Instrumentation</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="Windows Management Instrumentation"></td>
<TR><TD align='left' bgcolor='#f0f0f0'>VNC Server</TD><td bgcolor=#00CD00 align=center><i>Running</i></td><td><input type="checkbox" name="VNC Server"></td>
</table><p></fieldset></table>
</td></tr></table></td>
<table border="0" cellspacing="0" cellpadding="0" width="350">
<tr><td align="left" valign="bottom">
<input type="button" value="Start" onclick="StartService" title=" Click to Start the Services">
<input type="button" value="Stop" onclick="StopService" title=" Click to Stop the Service">
<input type="button" value="Restart" onclick="RestartService" title=" Click to Restart the Services">
<input type="button" value=" Exit " onclick="CloseForm" title=" Click to Exit Form ">
</td></tr></table></body></html>

Open in new window

chasmant, let me start by saying that I am impressed by the GUI of your hta; very nice indeed.

Now,… there are a couple of issues that I want to share:
1. the hta has the list of services hard coded; you need to generate them by querying Win32_Service;
2. the code associated with the buttons is incomplete or incorrect;
3. suggestion: you can put the buttons on a div and the list on a div; this way when you scroll up or down you don’t loose the buttons and you don’t have to put buttons top and down.
4. finally, you are using WMI; See this article here: http://msdn.microsoft.com/en-us/library/aa389290(v=vs.85).aspx

I will supply you with a piece of code that does a basic operation using WMI on a remote computer; we will see if it works or if it returns an error. Please read the article prior to testing the script and make sure that you enabled DCOM and disabled the firewall on the remote machine.

Using your favorite text editor (i.e.: Notepad) create a new file; paste the code and save with vbs extension. Run from command line: cscript.exe file.vbs

Don’t forget to change the "." in strComputer with the name of the computer you are trying to connect to.
strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM Win32_Service")
For Each objSWbemObject In colSWbemObjectSet
    WScript.Echo "Display Name:  " & objSWbemObject.DisplayName & vbCrLf & _
                 "   State:      " & objSWbemObject.State       & vbCrLf & _
                 "   Start Mode: " & objSWbemObject.StartMode   & vbCrLf
Next

Open in new window

Avatar of johnb6767
It is a nice little piece of code... But I have to ask... This functionality is already included in Windows...

Start>run>services.msc>Rt Click "Local", and select "Connect to another computer"

Why recreate the wheel? This method gets all services on the remote PC, in case there are some that are NOT included in your HTA script......
I get calls that the clock is off by an hour and I find I can not remote to pc and need to use ip for some reason thought that this would be easier
but I hadn't tried using connect to remote computer using IP instead moght work
If you cannot access a computer by name it could be a DNS issue; you should investigate.

chasmant, did you got the chance to test my script ? Is it working ? How do you want to proceed ?
chasmant, I assume that the clock being off by an hour is the problem you are trying to fix by connecting remotely.  I suspect this is going to be a problem in itself, because if you cannot connect remotely, then WMI connections will most likely fail as well.  You might be better off providing a tool that the users themselves can run instead, provided they even have network access to access the tool.  But to further assist with that resolution, we need a better description of the actual problem you're trying to solve.

After that, perhaps we can make the HTA run against remote pcs.

Regards,

Rob.
dneamtu: Not sure where I put that code can you maybe paste it in my script and then I can test it
It is very simple: open notepad and paste the code. Save the file with the extension .vbs. Open a command prompt and type cscript.exe file_name.vbs
You need to tell me if it returns something or if it gives an error.
P.S.: I hope that you read all the notes that I posted and realize that your code is incomplete and that will require more work that initially specified.
dneamtu
Your script worked  with no errors
just not sure how to make this go to another pc
Modify strComputer with the name of your remote computer and then run the script. We do this test to see if we can use WMI on a remote machine. I will post a hta example to show you how to include this example.
tried it on remote pc and worked also
It looks to me like you want the same functionality as on your older post which you had a question on...
https://www.experts-exchange.com/questions/24494363/HTA-with-VBScript-to-stop-start-windows-services.html
Did my comments added Jan 14th meet your needs?
Great script rejoinder ! However I get this error message:
Line: 21 Error: Object doesn't support this property or method: 'View'
This is odd - the script wasn't changed that much to cause an error message like this.  Are you running the script on the same machine as you did when you first asked the other question?

As a matter of point - I'm running Windows 7 as an administrator when running the script.

On a side note... happy new year Rob!
I am not the author; I am just another colleague trying to help out. I run the HTA on Windows XP SP3
Hey rejoinder!  Happy New Year.  I hope you're keeping well.  To avoid too much confusion, I'll just watch this one.  If you need any assistance, let me know.

Regards,

Rob.
dneamtu,
Can you try the script on a machine that has MS Office installed?
On a computer with MS Office installed it works.
Ah - the HTA uses Visual Basic 6 controls that are bundled with Office and some other Windows applications.
Pardon my ignorance, but which code are you guys trying, and what's it got to do with MSOffice?

Rob.
I did up a similar start/stop HTA file under - https://www.experts-exchange.com/questions/24494363/HTA-with-VBScript-to-stop-start-windows-services.html

The HTA references a listview control to display the services with checkboxes beside the names of the services.  The listview control is bundled with Office.
OK well, for this one, you can use this as a template.  I've made it dynamically get the services from the computer name entered in the text box, it just needs the stop and restart code put in.

Regards,

Rob.
<html><head><title>ABL Server Service Checker</title>
<HTA:APPLICATION
ID = "ServiceCheckerApp"
APPLICATIONNAME="Service Check"
BORDER = "thin"
CAPTION = "yes"
RESIZE = "no"
ICON = "Msn-Messenger.ico"
SHOWINTASKBAR = "yes"
SINGLEINSTANCE = "no"
SYSMENU = "Yes"
WINDOWSTATE = "normal"
SCROLL = "yes"
SCROLLFLAT = "yes"
VERSION = "1.0"
INNERBORDER = "no"
SELECTION = "no"
MAXIMIZEBUTTON = "no"
MINIMIZEBUTTON = "yes"
NAVIGABLE = "yes"
CONTEXTMENU = "yes"
BORDERSTYLE = "normal">
</hta>
<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>
<script language="VBScript">
Const ForAppending = 8
Dim dictServices
Sub Window_OnLoad
	Set dictServices = CreateObject("Scripting.Dictionary")
	Dim width, height, x, y
	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
	Set objNetwork = CreateObject("WScript.Network")
	txt_computer.Value = objNetwork.ComputerName
End Sub
Sub PopulateServices
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & txt_computer.Value & "\root\cimv2")
	Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service")
	strHTML = "<table border='0' cellpadding='3' width='350'>"
	intService = 1
	For each objService In colServiceList
		dictServices.Add intService, objService.Name
		Select Case objService.State
			Case "Stopped"
				strBGColor = "#FD0000"
			Case "Running"
				strBGColor = "#00CD00"
		End Select
		strHTML = strHTML & VbCrLf & "<TR><TD align='left' bgcolor='#f0f0f0'>" & objService.Name & "</TD><td bgcolor=" & strBGColor & " align=center><i>" & objService.State & "</i></td><td><input type='checkbox' name='chk" & intService & "'></td><tr>"
		intService = intService + 1
	Next
	strHTML = strHTML & VbCrLf & "</table>"
	span_services.innerHTML = strHTML
End Sub
Sub startService
	MsgBox "Needs to iterate through selected check boxes. Exiting Sub"
	Exit Sub
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & oServer & "\root\cimv2")
	Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & service & "'")
	For each objService in colServiceList
		errReturn = objService.StartService()
	Next
	Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='" & service & "'} Where AssocClass=Win32_DependentService " & "Role=Dependent" )
	For each objService in colServiceList
		objService.StartService()
	Next
End Sub
Sub restartService
	MsgBox "Needs to iterate through selected check boxes. Exiting Sub"
	Exit Sub
End Sub
Sub CloseForm
	Window.Close
End Sub
</script>
</head><body>
<tr><td align="left" valign="bottom">
Enter computer name: <input type="text" id="txt_computer" name="txt_computer" maxlength="15" size="25">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" value="Get Services" onclick="PopulateServices" title=" Click to get the list of Services">
<input type="button" value="Start" onclick="StartService" title=" Click to Start the Services">
<input type="button" value="Stop" onclick="StopService" title=" Click to Stop the Service">
<input type="button" value="Restart" onclick="RestartService" title=" Click to Restart the Services">
<input type="button" value=" Exit " onclick="CloseForm" title=" Click to Exit Form ">
<table border="0" cellspacing="0" CellSpacing="0"><tr><td width="350">
<tr><td valign="top" colspan="3"><fieldset><legend><b>Service List</b></legend>
<span id="span_services">
</span>
<p></fieldset></table>
</td></tr></table></td>
<table border="0" cellspacing="0" cellpadding="0" width="350">
<tr><td align="left" valign="bottom">
<input type="button" value="Start" onclick="StartService" title=" Click to Start the Services">
<input type="button" value="Stop" onclick="StopService" title=" Click to Stop the Service">
<input type="button" value="Restart" onclick="RestartService" title=" Click to Restart the Services">
<input type="button" value=" Exit " onclick="CloseForm" title=" Click to Exit Form ">
</td></tr></table></body></html>

Open in new window

I was writing if from scratch Rob :)
I think rejoinder will finish it before I do; I not I will come back tomorrow with the full implementation if he doesn’t.

Notes for chasmant:
If the script will not work and will give you this message:
Error:      Permission denied: 'GetObject'
Here are a few things that you can try (mostly if your computers are not part of a domain):

Local Security Policies
1. Start -> Control Panel -> Administrative Tools -> Local Security Policy
2. Navigate to Security\Local Policies\Security Options
a. Network Access: Let everyone permissions apply to anonymous users - Set to Enabled
b. DCOM: Machine Access Restrictions - Add Anonymous, Everyone, Interactive, Network, System with full rights options set.
c. Network Access: Sharing security model for local accounts - Set to Classic

If this still does not work then try adjusting the DCOM configuration:

DCOM Configuration
1. Click Start -> Run
2. Enter DCOMCNFG and press OK. This will open the DCOMCNFG window.
3. Browse down the tree to Console Root ' Component Services ' Computers ' My Computer
4. Right click on "My Computer" and select properties
5. Select the "Default Properties" tab
a. Enable Distributed COM on this computer - option is checked.
b. Default Authentication Level - set to Connect
c. Default Impersonation Level - Set to Identify
6. Select the "COM Security" tab
7. Click on Access Permissions ' Edit Default
a. Add "Anonymous", "Everyone", "Interactive", "Network", "System" with Local and Remote access permissions set.
8. Click on Launch and Activation Permissions ' Edit Default
a. Add "Anonymous", "Everyone", "Interactive", "Network", "System" with Local and Remote access permissions set.
9. Click on OK
10. Close the DCOMCNFG window
11. Reboot
dneamtu, we should also add a Ping test, and a WMI error check, so it continues gracefully.  I have also recently written a WMI timeout checking procedure, which I will add on Monday.  It would be nice to have this fully functional in my code library, so I'll finish it off as well next week.  We'll see how different our approaches are.

Regards,

Rob.
Hi chasmant,

I had some spare time tonight, and managed to rebuild this HTA into a tool that will start, stop, and restart services on a remote or local computer.  It will perform a Ping test for initial connectivity, then perform a WMI Connection test that I recently wrote, which has a custom user defined time out. You can set the maximum wait time out for WMI (in seconds) using the second parameter on this line:
            strReturn = TestWMIConnection(txt_computer.Value, 10)

Regards,

Rob.
<html><head><title>Remote Computer Service Tool</title>
<HTA:APPLICATION
ID = "RemoteComputerServiceTool"
APPLICATIONNAME="Remote Computer Service Tool"
BORDER = "thin"
CAPTION = "yes"
RESIZE = "no"
ICON = "Msn-Messenger.ico"
SHOWINTASKBAR = "yes"
SINGLEINSTANCE = "no"
SYSMENU = "Yes"
WINDOWSTATE = "normal"
SCROLL = "yes"
SCROLLFLAT = "yes"
VERSION = "1.0"
INNERBORDER = "no"
SELECTION = "no"
MAXIMIZEBUTTON = "no"
MINIMIZEBUTTON = "yes"
NAVIGABLE = "yes"
CONTEXTMENU = "yes"
BORDERSTYLE = "normal">
</hta>
<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>
<script language="VBScript">
Const ForAppending = 8
Dim objWMIService
Dim intServiceCount
Dim strResult
Sub Window_OnLoad
	Disable_Buttons
	Dim width, height, x, y
	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
	Set objNetwork = CreateObject("WScript.Network")
	txt_computer.Value = objNetwork.ComputerName
	btnGetServices.Disabled = False
	btnExit1.Disabled = False
	btnExit2.Disabled = False
End Sub
Sub Disable_Buttons
	btnGetServices.Disabled = True
	btnStart1.Disabled = True
	btnStop1.Disabled = True
	btnRestart1.Disabled = True
	btnExit1.Disabled = True
	btnStart2.Disabled = True
	btnStop2.Disabled = True
	btnRestart2.Disabled = True
	btnExit2.Disabled = True
End Sub
Sub Enable_Buttons
	btnGetServices.Disabled = False
	btnStart1.Disabled = False
	btnStop1.Disabled = False
	btnRestart1.Disabled = False
	btnExit1.Disabled = False
	btnStart2.Disabled = False
	btnStop2.Disabled = False
	btnRestart2.Disabled = False
	btnExit2.Disabled = False
End Sub
Sub PopulateServices
	span_services.InnerHTML = ""
	Disable_Buttons
	If Ping(txt_computer.Value) = True Then
		strReturn = TestWMIConnection(txt_computer.Value, 10)
		If strReturn = "success" Then
			span_services.innerHTML = "Refreshing service list..."
			Const adVarChar = 200
			Const MaxCharacters = 255
			Set DataList = Nothing
			Set DataList = CreateObject("ADOR.Recordset")
			DataList.Fields.Append "ServiceDisplayName", adVarChar, MaxCharacters
			DataList.Fields.Append "ServiceName", adVarChar, MaxCharacters
			DataList.Fields.Append "ServiceState", adVarChar, MaxCharacters
			DataList.Open
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & txt_computer.Value & "\root\cimv2")
			Set colServiceList = objWMIService.ExecQuery ("Select DisplayName, Name, State from Win32_Service")
			For Each objService In colServiceList
				DataList.AddNew
				DataList("ServiceDisplayName") = objService.DisplayName
				DataList("ServiceName") = objService.Name
				DataList("ServiceState") = objService.State
				DataList.Update
			Next
			DataList.Sort = "ServiceDisplayName"
			If Not DataList.BOF Then DataList.MoveFirst
			strHTML = "<table border='0' cellpadding='3' width='350'>"
			intServiceCount = 0
			While Not DataList.EOF
				intServiceCount = intServiceCount + 1
				Select Case DataList("ServiceState")
					Case "Stopped"
						strBGColor = "#FD0000"
					Case "Running"
						strBGColor = "#00CD00"
					Case Else
						strBGColor = "#FFFF00"
				End Select
				strHTML = strHTML & VbCrLf & "<TR><TD align='left' bgcolor='#f0f0f0'>" & DataList("ServiceDisplayName") & "</TD><td bgcolor=" & strBGColor & " align=center><i>" & DataList("ServiceState") & "</i></td><td><input type='checkbox' name='chk" & intServiceCount & "' value='" & DataList("ServiceName") & "'></td><tr>"
				DataList.MoveNext
			Wend
			DataList.Close
			strHTML = strHTML & VbCrLf & "</table>"
			span_services.innerHTML = strHTML
		ElseIf strReturn = "failed" Then
			MsgBox "WMI Connection Error connecting to " & txt_computer.Value
		Else
			MsgBox "WMI Connection time out connecting to " & txt_computer.Value
		End If
	Else
		MsgBox "Ping test failed to " & txt_computer.Value
	End If
	If span_services.InnerHTML <> "" Then
		Enable_Buttons
	Else
		btnGetServices.Disabled = False
		btnExit1.Disabled = False
		btnExit2.Disabled = False
	End If
End Sub
Sub StartService(strMode)
	Disable_Buttons
	If strMode = "Restart" Then
		strResult = strResult & VbCrLf & "Start Service Result:"
	Else
		strResult = "Start Service Result:"
	End If
	For intService = 1 To intServiceCount
		strOptionChecked = "chk" & intService & ".Checked"
		If Eval(strOptionChecked) = True Then
			strOptionValue = "chk" & intService & ".Value"
			strServiceName = Eval(strOptionValue)
			Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strServiceName & "'")
			For Each objService In colServiceList
				If objService.State = "Running" Then
					strResult = strResult & VbCrLf & objService.DisplayName & " service is already started."
				Else
					errReturn = objService.StartService()
					HTASleep 2
					If errReturn = 0 Then
						strResult = strResult & VbCrLf & objService.DisplayName & " service was started."
					Else
						strResult = strResult & VbCrLf & objService.DisplayName & " service was not started. Error" & errReturn
					End If
				End If
			Next
			Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='" & strServiceName & "'} Where AssocClass=Win32_DependentService Role=Antecedent" )
			For Each objService In colServiceList
				If objService.State = "Running" Then
					strResult = strResult & VbCrLf & objService.DisplayName & " service is already started."
				Else
					errReturn = objService.StartService()
					HTASleep 2
					If errReturn = 0 Then
						strResult = strResult & VbCrLf & objService.DisplayName & " service was started."
					Else
						strResult = strResult & VbCrLf & objService.DisplayName & " service was not started. Error" & errReturn
					End If
				End If
			Next
		End If
	Next
	If strResult = "Start Service Result:" Or strResult = "Stop Service Result:" & VbCrLf & "Start Service Result:" Then
		MsgBox "Please select one or more services."
	Else
		MsgBox strResult
		PopulateServices
	End If
	Enable_Buttons
End Sub
Sub StopService(strMode)
	Disable_Buttons
	strResult = "Stop Service Result:"
	For intService = 1 To intServiceCount
		strOptionChecked = "chk" & intService & ".Checked"
		If Eval(strOptionChecked) = True Then
			strOptionValue = "chk" & intService & ".Value"
			strServiceName = Eval(strOptionValue)
			Set colServiceList = objWMIService.ExecQuery("Associators of {Win32_Service.Name='" & strServiceName & "'} Where AssocClass=Win32_DependentService Role=Antecedent" )
			For Each objService In colServiceList
				If objService.State = "Stopped" Then
					strResult = strResult & VbCrLf & objService.DisplayName & " service is already stopped."
				Else
					errReturn = objService.StopService()
					HTASleep 2
					If errReturn = 0 Then
						strResult = strResult & VbCrLf & objService.DisplayName & " service was stopped."
					Else
						strResult = strResult & VbCrLf & objService.DisplayName & " service was not stopped. Error" & errReturn
					End If
				End If
			Next
			Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strServiceName & "'")
			For Each objService In colServiceList
				If objService.State = "Stopped" Then
					strResult = strResult & VbCrLf & objService.DisplayName & " service is already stopped."
				Else
					errReturn = objService.StopService()
					HTASleep 2
					If errReturn = 0 Then
						strResult = strResult & VbCrLf & objService.DisplayName & " service was stopped."
					Else
						strResult = strResult & VbCrLf & objService.DisplayName & " service was not stopped. Error" & errReturn
					End If
				End If
			Next
		End If
	Next
	If strMode <> "Restart" Then
		If strResult = "Stop Service Result:" Then
			MsgBox "Please select one or more services."
		Else
			If strMode <> "Restart" Then
				MsgBox strResult
				PopulateServices
			End If
		End If
	End If
	Enable_Buttons
End Sub
Sub restartService
	StopService("Restart")
	StartService("Restart")
End Sub
Sub HTASleep(intSeconds)
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "ping 127.0.0.1 -n " & intSeconds + 1, 0, True
End Sub
Sub CloseForm
	Window.Close
End Sub
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 4 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Function TestWMIConnection(strComputer, intTimeOutInSeconds)
	' Function written by Rob Sampson - 12 Jan 2011
	' Experts-Exchange volunteer: http://www.experts-exchange.com/M_3820065.html
	' Return strings from this function are in lower case, and consist of:
	' "success": WMI Connection successful
	' "failed": WMI Connection failed
	' "time out": WMI Connection attempt timed out
	Set objFSO = CreateObject("Scripting.FileSystemObject")
    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
	strTempScript = Mid(strHTAPath, InStrRev(strHTAPath, "\")) & "TempWMITestToBeDeleted.vbs"
	Set objTempFile = objFSO.CreateTextFile(strTempScript, True)
	objTempFile.WriteLine "On Error Resume Next"
	objTempFile.WriteLine "Set objWMIService = GetObject(""winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"")"
	objTempFile.WriteLine "If Err.Number = 0 Then"
	objTempFile.WriteLine vbTab & "WScript.StdOut.Write ""success"""
	objTempFile.WriteLine "Else"
	objTempFile.WriteLine vbTab & "WScript.StdOut.Write ""failed"""
	objTempFile.WriteLine "End If"
	objTempFile.Close
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("wscript " & objFSO.GetFile(strTempScript).ShortPath)
	intSeconds = 0
	While objExec.Status = 0 And intSeconds <= intTimeOutInSeconds
		HTASleep 1
		intSeconds = intSeconds + 1
	Wend
	If objExec.Status = 1 Then
		strReturn = objExec.StdOut.ReadAll
	Else
		On Error Resume Next
		objExec.Terminate
		Err.Clear
		On Error GoTo 0
		strReturn = "time out"
	End If
	objFSO.DeleteFile strTempScript, True
	TestWMIConnection = LCase(strReturn)
End Function
</script>
</head><body>
<tr><td align="left" valign="bottom">
Enter computer name: <input type="text" id="txt_computer" name="txt_computer" maxlength="15" size="15">&nbsp;&nbsp;&nbsp;
<input type="button" id="btnGetServices" value="Get Services" onclick="PopulateServices" title=" Click to get the list of Services"><br>
<input type="button" id="btnStart1" value="Start" onclick="StartService('start')" title=" Click to Start the Services">
<input type="button" id="btnStop1" value="Stop" onclick="StopService('stop')" title=" Click to Stop the Service">
<input type="button" id="btnRestart1" value="Restart" onclick="RestartService" title=" Click to Restart the Services">
<input type="button" id="btnExit1" value=" Exit " onclick="CloseForm" title=" Click to Exit Form ">
<table border="0" cellspacing="0" CellSpacing="0"><tr><td width="350">
<tr><td valign="top" colspan="3"><fieldset><legend><b>Service List</b></legend>
<span id="span_services">
</span>
<p></fieldset></table>
</td></tr></table></td>
<table border="0" cellspacing="0" cellpadding="0" width="350">
<tr><td align="left" valign="bottom">
<input type="button" id="btnStart2" value="Start" onclick="StartService" title=" Click to Start the Services">
<input type="button" id="btnStop2" value="Stop" onclick="StopService('stop')" title=" Click to Stop the Service">
<input type="button" id="btnRestart2" value="Restart" onclick="RestartService('start')" title=" Click to Restart the Services">
<input type="button" id="btnExit2" value=" Exit " onclick="CloseForm" title=" Click to Exit Form ">
</td></tr></table></body></html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dneamtu
dneamtu
Flag of Romania 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
Hmmm, did I get my Antecedent / Dependent roles mixed up?  I haven't tested it much, but they always confuse me for some reason.

I like the use of the DIV with the absolute position...that's nice.

Rob.
Rob,

As I understand, you are trying to create a template. Please check out the recursion for Service Start and Service Stop. It is very important to use recursivity because the dependency between services does not stop at the first element.

A few notes from msdn: If you stop, start, or restart a service, any dependent services are also affected. Starting a service does not automatically restart its dependent services.

RecursiveServiceStart [above]
          Role=Dependent - checks that all the system components the service depends on are started.
RecursiveServiceStop [below]
          Role=Antecedent - checks that all the system components that depend on this service are stopped (from top to bottom) before it stops the current service.
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
I didn’t want to arrange them alphabetically, thought it will be closer to what windows gives you :)

As for modifying the elements that change, I used that approach because otherwise I had to refresh the whole service list. This is time consuming and it puts an additional stress on the network. Querying WMI with associators can perform very poorly (I speak from experience); I don’t want to perform additional queries unless I need to.

One last note: I see that you pause the script at some point; this is maybe a good thing (although it is not mentioned in msdn). You should use setTimeout or setInterval for this and not ping.
Glad to be of service; I was expecting at least an assist.
dneamtu:
Anyway to add disable and enable to the option
There are ways to do almost anything; but for that you should open a new question.
ok thanks
In my opinion, dneamtu's comment ID:34616597 is probably a more efficient that mine, so it should have been
Half points to dneamtu's comment ID:34616597
Half points to RobSampson's comment ID:34624360
We both provided perfectly valid answers.

Regards,

Rob.
not sure how I would do that I am new at this I just thought since I used his I would give him the points I will be more careful going forward to share the points for all the acceptable options
Sorry
chasmant, no need to do anything.  A moderator will check back at this question in about 4 days and close as per Experts recommendations.
I agree with Rob:
Half points to dneamtu's comment ID:34616597
Half points to RobSampson's comment ID:34624360