Link to home
Start Free TrialLog in
Avatar of Lee025_
Lee025_Flag for Australia

asked on

HTA with VBScript to stop/start windows services

Hi all,

I have written a vbscript that queries the services of a local machine then writes the results out to an HTA file. To the right hand side of each listed service is a checkbox. What I want to do is ahve the user select one/multiple checkboxes and hit a start/stop/restart button then it will go off and perform the tasks.

the hta is writing successfully but I don't know how to relate the checkbox to the actual service that needs starting etc.

I'm not worried about being able to stop/start multiple services to start with only one.

Attached is a copy of the HTA code. Excuse the formatting, as it's being dynamically written!

TIA

Lee
<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

Avatar of jwarnken
jwarnken
Flag of United States of America image

I am attaching an hta that I wrote to clean up sms packages. It does not fit exactly to your question but it should give you the ideal of how to pass the information from the html controls back to your script functions.

I appoligize that I don't have the time today to convert your example into a working example.
<html>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>My HTML Application</title>
<script language="vbscript">
<!-- Insert code, subroutines, and functions here -->
'Version 1.1 
'Update Log
' 3/28/2007 - added Log2 sub and calls to it
Const ForWriting = 2
Const ForReading = 1
Const ForAppending = 8
Const TristateFalse = 0
Dim WSHNetwork, wshshell, fso
Set WSHNetwork = CreateObject("WScript.Network")
Set wshshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim list 
Dim Systems()
list = "System_List.txt"
 
Sub Window_Onload
OutputArea.InnerHTML = "...."
Call Selected
End Sub
 
Sub ExitWindow
Window.close
End Sub
 
Sub Cleanup
If PkgID.value <> "" Then 
	OutputArea.InnerHTML = "Starting CleanUp of " & PkgID.value & "<BR>"
	log2 "Starting CleanUp of " & PkgID.value
	Set infile = fso.OpenTextFile (list, ForReading, True)
	Do While infile.AtEndOfStream <> True
		If infile.AtEndOfStream <> True Then
		strServername = Trim(infile.ReadLine)
			if reachable(strServername) Then
				OutputArea.InnerHTML = OutputArea.InnerHTML & strServername & vbTab & "is reachable. <BR>" 
				Log2 strServername & "is reachable."
				Update(strServername)
			Else
				OutputArea.InnerHTML = OutputArea.InnerHTML & strServername & vbTab & "is Unreachable !!! <BR>" 
				Log2 strServername & "is Unreachable !!! "
			End if   
		End If
	Loop
Else 
OutputArea.InnerHTML = "You must enter a Package ID!!! <BR>"
End If
End Sub
 
sub Selected
Dim data
data = Getfile(list)
SelectionArea.InnerHTML = data
end sub
Function Getfile(FileName)
On error Resume Next
Dim strServername
Getfile = "<select id='selectX' size='10' multiple='multiple'>"
	If FileName<>"" Then
    	Set infile = fso.OpenTextFile (FileName, ForReading, True)
	Do While infile.AtEndOfStream <> True
		If infile.AtEndOfStream <> True Then
			strServername = Trim(infile.ReadLine)
				If strServername <> "" Then 
					Getfile = Getfile & "<option value='" &  strServername & "'>" & strServername & "</option>"
				End If
		End If
	Loop
  	End If
Getfile = Getfile & "</select>"
End Function 
Function reachable(HostName)
sHost		= "."
sTarget	= HostName
reachable = false 
Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & "WHERE Address = '" + sTarget + "'")
 
For Each oPingResult In cPingResults
	If oPingResult.StatusCode = 0 Then
		reachable = True
	Else
		reachable = false 
	End If
Next
End Function
Function Update(pc)
Dim oExec
Set oExec = WshShell.Exec("reg query " & chr(34) & "\\" & pc & "\HKLM\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\System\" & PkgID.value & chr(34) )
	Do While Not oExec.StdOut.AtEndOfStream
		Out = oExec.StdOut.ReadAll
	Loop
if Out ="" then
	OutputArea.InnerHTML = OutputArea.InnerHTML & " -----> " &  PkgID.value & " not found. <BR>"
	Log2  PkgID.value & " not found."
	Exit Function
else
	OutputArea.InnerHTML = OutputArea.InnerHTML & " -----> " &  PkgID.value & " located <BR>"
	Log2  PkgID.value & " located "
	Set oExec = WshShell.Exec("reg delete " & chr(34) &  "\\" & pc & "\HKLM\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\System\" & PkgID.value & chr(34) & " /f")
		Do While Not oExec.StdOut.AtEndOfStream
			OutputArea.InnerHTML = OutputArea.InnerHTML & " -----> " &  oExec.StdOut.ReadAll & " <BR>"
			Log2 oExec.StdOut.ReadAll
		Loop
end if 
End Function
function DeletePC()
Dim elSel
Set elSel = document.getElementById("selectX")
	for each sel in elsel.options
		if sel.selected then
			sel.removenode
		end if 
	next
Call  SyncFile(list)
End Function
function AddPC()
Dim NewText, elOptNew, elSel
Set NewText = document.getElementById("NewPC")
Set elOptNew = document.createElement("option")
  elOptNew.text = NewText.value
  elOptNew.value = NewText.value
Set elSel = document.getElementById("selectX")
On Error resume Next
elSel.add elOptNew, null 'standards compliant; doesn't work in IE
	if err then 
		elSel.add elOptNew 'IE only
	End if 
On Error Goto 0
Call  SyncFile(list)
End Function
Sub SyncFile(FileName)
Dim elSel, infile
Set elSel = document.getElementById("selectX")
If FileName<>"" Then
    	Set infile = fso.OpenTextFile (FileName, ForWriting, True)
		For each Sel in elsel.options
			infile.Writeline Sel.value
		Next
  	End If
infile.Close
End Sub
 
'----------------------------------------------------------------------
Sub Log2 (msg)
	outputfP = PkgID.value & "_Cleanup.log"
	Set outputf = fso.OpenTextFile (outputfP, ForAppending, True)
	outputf.WriteLine(Now & " --> " & msg)
End Sub
'----------------------------------------------------------------------
</script>
<hta:application
	applicationname="IS SMS2K3 Package Cleanup"	
	border="dialog"
	borderstyle="normal"
	caption="IS SMS2K3 Package Cleanup"
	contextmenu="no"
	icon="myicon.ico"
	maximizebutton="yes"
	minimizebutton="yes"
	navigable="no"
	scroll="yes"
	selection="no"
	showintaskbar="yes"
	singleinstance="yes"
	sysmenu="yes"
	version="1.0"
	windowstate="normal"
>
</script>
</head>
<body>
<!-- HTML goes here -->
<table border=0 cellspacing=0 cellpadding=1 width=100% align=Center><tr><td valign=top><IMG src=Logo.bmp align=right></td>
<td valign=bottom><font size=6em color=#78AF14 align=Right>Information Systems Engineering</font></td></tr>
<tr><th colspan=2 valign=Center><font size=4em align=Center>SMS 2003 Package Cleanup</font></th></tr></table>
<table border=2 cellspacing=0 cellpadding=0 width=80% align=Center>
 
</table>
    <table  border=2 style="width: 100%; height: 20%">
        <tr>
            <td style="width: 403px; text-align: left; height: 20px;" valign=Top>
                Package ID:
                <input id="PkgID" autocomplete="off" style="width: 187px" type="text" /></td>
            <td colspan="2" rowspan="1" style="height: 20px; text-align: Center" valign=Top>
                <input id="Exec" type="button" value="Start Cleanup" onclick=Cleanup />    <input id="Exit" type="button" value="Cancel" onclick=ExitWindow /></td>
        </tr>
        <tr>
            <td style="width: 403px; text-align: left; height: 108px;" valign=Top >
                Computer Name: <input id="NewPC" type="text" style="width: 168px" />  <input id="NewPC_Button" type="button" value="Add Machine" style="width: 90px" onclick=AddPC />
                </td>
            <td colspan="2" rowspan="1" style="height: 108px; text-align: left" valign=Top>
                Selected Systems:    <input id="RemovePC_Button" type="button" value="Remove Machine" style="width: 120px" onclick=DeletePC />
                <br />
                <span id=SelectionArea >
                </span>
            </td>
        </tr>
    </table>
    <hr />
<table  border=0 >
<BR>
	<span id=OutputArea >
                </span>
 </table>	
</body>
</html>

Open in new window

Avatar of Lee025_

ASKER

Great thanks, ill take a look see if I can pull out the relevant code
Avatar of Lee025_

ASKER

Just had a quick look and can see where the variables are being passed back from the html controls. However my problem is that the controls themselves (in my case check boxes) are written on the fly by a vbscript based on the services installed on the machine chosen. So the list of services could be different every time. I'm not sure how to name/create the check boxes so they can be referenced in relation to the service name to the left of it.

Thanks
Lee
ASKER CERTIFIED SOLUTION
Avatar of rejoinder
rejoinder
Flag of Canada image

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

ASKER

Just what I was after!!! Thanks v much been looking for 2 weeks to find an answer to this problem!

Thanks again

Lee
Avatar of chasmant
chasmant

Anyway to make this go to a remote pc
I adjusted a few lines to make it easier for you to edit what remote computer the script connect to.

Edit line 15 to match the computer name of the machine you want to see.
<head>
<title>Server Services</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Server Services"
     BORDER="thin"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     ID="oHTA"
>
<APPLICATION:HTA>
</head>
 
<script language="VBScript">
Dim strcomputer
strcomputer = "Computer-Name-Here"

Sub Window_OnLoad
    span_computername.InnerHTML = uCase(strcomputer)
    Set objlst_groupnames = document.getElementById( "list_servicenames" )
    If objlst_groupnames Is Nothing Then
        MsgBox "A problem was encountered while creating the listview." & vbCRLF & "Please see your administrator."
    Else
        With objlst_groupnames
            .View              = 3
            .Width             = 800
            .Height            = 600
            .SortKey           = 0
            .Arrange           = 0
            .LabelEdit         = 1
            .SortOrder         = 0
            .Sorted            = 1
            .MultiSelect       = 0
            .LabelWrap         = -1
            .HideSelection     = -1
            .HideColumnHeaders = 0
            .OLEDragMode       = 0
            .OLEDropMode       = 0
            .Checkboxes        = 1
            .FlatScrollBar     = 0
            .FullRowSelect     = 1
            .GridLines         = 0
            .HotTracking       = 0
            .HoverSelection    = 0
            .PictureAlignment  = 0
            .TextBackground    = 0
            .ForeColor         = -2147483640
            .BackColor         = -2147483643
            .BorderStyle       = 1
            .Appearance        = 1
            .MousePointer      = 0
            .Enabled           = 1
            .ColumnHeaders.Clear
            .ColumnHeaders.Add , , "Caption", 150
            .ColumnHeaders.Add , , "State", 150
            .ColumnHeaders.Add , , "Name", 150
            .ColumnHeaders.Add , , "Description", 150
            .ColumnHeaders.Add , , "Start Mode", 150
            .ListItems.Clear
        End With
    End If
    ListServices(strcomputer)
End Sub
 
Sub ListServices(sComputer)
    ON ERROR RESUME NEXT
    Set objList = document.getElementById( "list_servicenames" )
    objList.ListItems.Clear
    
    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
    For Each objItem in colItems
        Set objListItem  = objList.ListItems.Add
        objListItem.Text = objItem.Caption
        objListItem.ListSubItems.Add.Text = objItem.State
        objListItem.ListSubItems.Add.Text = objItem.Name
        objListItem.ListSubItems.Add.Text = objItem.Description
        objListItem.ListSubItems.Add.Text = objItem.StartMode
    Next
End Sub
 
Sub btn_start_onClick()
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StartService()
            Next
        end if
    next
    ListServices(strComputer)
End Sub
 
Sub btn_stop_onClick()
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StopService()
            Next
        end if
    next
    ListServices(strComputer)
End Sub
 
Sub btn_restart_onClick()
    'Stop services
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StopService()
            Next
        end if
    next
    'Start services
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StartService()
            Next
        end if
    next
    ListServices(strComputer)
End Sub
 
Sub btn_Refresh_onClick()
    ListServices(strComputer)
End Sub
 
Sub btn_exit_onClick()
    Window.Close
End Sub
 
function list_servicenames_ColumnClick(colheader)
    list_servicenames.SortKey = colheader.index-1
end function
 
</script>
 
<body>
You are connected to: <span id="span_computername"> </span><br><br>
<OBJECT id="list_servicenames" name="list_servicenames" classid="clsid:BDD1F04B-858B-11D1-B16A-00C0F0283628"></OBJECT>
<br />
<input type="button" value="Refresh" name="btn_Refresh" id="btn_Refresh" title="Click to refresh the services list"> &nbsp;
<input type="button" value="Start"   name="btn_start"   id="btn_start"   title="Click to Start the Services"> &nbsp;
<input type="button" value="Stop"    name="btn_stop"    id="btn_stop"    title="Click to Stop the Service"> &nbsp;
<input type="button" value="Restart" name="btn_restart" id="btn_restart" title="Click to Restart the Services"> &nbsp;
<input type="button" value="Exit"    name="btn_exit"    id="btn_exit"    title="Click to Exit Form ">
 
</body>

Open in new window

great if I manually plug in pc name it works how can I get it to prompt for pc name?
I added a button near the top to click on.  A popup will open for you to type the computer name.



<head>
<title>Server Services</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Server Services"
     BORDER="thin"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     ID="oHTA"
>
<APPLICATION:HTA>
</head>
 
<script language="VBScript">
Dim strcomputer
strcomputer = "Computer-Name-Here"

Sub Window_OnLoad
    Set objlst_groupnames = document.getElementById( "list_servicenames" )
    If objlst_groupnames Is Nothing Then
        MsgBox "A problem was encountered while creating the listview." & vbCRLF & "Please see your administrator."
    Else
        With objlst_groupnames
            .View              = 3
            .Width             = 800
            .Height            = 600
            .SortKey           = 0
            .Arrange           = 0
            .LabelEdit         = 1
            .SortOrder         = 0
            .Sorted            = 1
            .MultiSelect       = 0
            .LabelWrap         = -1
            .HideSelection     = -1
            .HideColumnHeaders = 0
            .OLEDragMode       = 0
            .OLEDropMode       = 0
            .Checkboxes        = 1
            .FlatScrollBar     = 0
            .FullRowSelect     = 1
            .GridLines         = 0
            .HotTracking       = 0
            .HoverSelection    = 0
            .PictureAlignment  = 0
            .TextBackground    = 0
            .ForeColor         = -2147483640
            .BackColor         = -2147483643
            .BorderStyle       = 1
            .Appearance        = 1
            .MousePointer      = 0
            .Enabled           = 1
            .ColumnHeaders.Clear
            .ColumnHeaders.Add , , "Caption", 150
            .ColumnHeaders.Add , , "State", 150
            .ColumnHeaders.Add , , "Name", 150
            .ColumnHeaders.Add , , "Description", 150
            .ColumnHeaders.Add , , "Start Mode", 150
            .ListItems.Clear
        End With
    End If
	if strComputer <> "" then
		ListServices(strcomputer)
	end if
End Sub
 
Sub ListServices(sComputer)
    ON ERROR RESUME NEXT
    Set objList = document.getElementById( "list_servicenames" )
    objList.ListItems.Clear
    
    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
    For Each objItem in colItems
        Set objListItem  = objList.ListItems.Add
        objListItem.Text = objItem.Caption
        objListItem.ListSubItems.Add.Text = objItem.State
        objListItem.ListSubItems.Add.Text = objItem.Name
        objListItem.ListSubItems.Add.Text = objItem.Description
        objListItem.ListSubItems.Add.Text = objItem.StartMode
    Next
End Sub
 
Sub btn_start_onClick()
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StartService()
            Next
        end if
    next
    ListServices(strComputer)
End Sub
 
Sub btn_stop_onClick()
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StopService()
            Next
        end if
    next
    ListServices(strComputer)
End Sub
 
Sub btn_restart_onClick()
    'Stop services
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StopService()
            Next
        end if
    next
    'Start services
    for n = 1 to list_servicenames.ListItems.Count
        if list_servicenames.ListItems(n).checked = True then
            strService = list_servicenames.ListItems(n).ListSubItems(2).Text
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
            For each objService in colServiceList
                errReturn = objService.StartService()
            Next
        end if
    next
    ListServices(strComputer)
End Sub
 
Sub btn_Refresh_onClick()
    ListServices(strComputer)
End Sub
 
Sub btn_Connect_onClick()
	strComputer = InputBox("Enter a computer","Computer Name","computer-name-here")
	if strComputer = "" then
		strComputer = "."
	end if
    span_computername.InnerHTML = uCase(strcomputer)
	ListServices(strComputer)
End Sub
 
Sub btn_exit_onClick()
    Window.Close
End Sub
 
function list_servicenames_ColumnClick(colheader)
    list_servicenames.SortKey = colheader.index-1
end function
 
</script>
 
<body>
You are connected to: <span id="span_computername"> </span><br>
<input type="button" value="Connect to computer..." name="btn_Connect" id="btn_Connect" title="Click to enter a computer name to connect to"><br><br>
<OBJECT id="list_servicenames" name="list_servicenames" classid="clsid:BDD1F04B-858B-11D1-B16A-00C0F0283628"></OBJECT>
<br />
<input type="button" value="Refresh" name="btn_Refresh" id="btn_Refresh" title="Click to refresh the services list"> &nbsp;
<input type="button" value="Start"   name="btn_start"   id="btn_start"   title="Click to Start the Services"> &nbsp;
<input type="button" value="Stop"    name="btn_stop"    id="btn_stop"    title="Click to Stop the Service"> &nbsp;
<input type="button" value="Restart" name="btn_restart" id="btn_restart" title="Click to Restart the Services"> &nbsp;
<input type="button" value="Exit"    name="btn_exit"    id="btn_exit"    title="Click to Exit Form ">
 
</body>

Open in new window