Buttons in HTA file not working

I'm writing an HTA file to monitor two specific services on my local computer.  I'd like to add one process to it, but I don't think I can monitor process status with HTA.  That's a problem for later.  Right now, with my current code, everything works except the Start, Stop, and Restart buttons I have.  I would appreciate any ideas on how to get them working.  I changed to names of the monitored services in my code to Spooler and Fax for compatibility in case anyone wants to test it on their machine.  


<head>
<title>Server Services</title>
<HTA:APPLICATION 
    APPLICATIONNAME="Server Services"
    BORDER="thin"
    SCROLL="no"
    MinimizeButton="no"
    MaximizeButton="no"
    SINGLEINSTANCE="yes"
    ID="oHTA"
>
<APPLICATION:HTA>
</head>

<style>
body {
    background-color: black;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 25px;
}
table {
    width: 100%;
    border-color: firebrick;
    background-color: black;
    color: khaki;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 12px;
}
th {
    border-color: firebrick;
    background-color: black;
    color: khaki;
}
td {
    border-color: firebrick;
}

</style>

<script language="VBScript">

Sub Resize()
    Window.ResizeTo 380,200
End Sub
 
Sub Window_OnLoad
    ' Populate table with list of services
    Window.ResizeTo 380,200
    ListServices(".")
End Sub
 
Sub ListServices(sComputer)
    Set oTable = Document.All("list_servicenames")

    ' Delete any existing data rows
    rowCount = oTable.Rows.Length
    If rowCount > 1 Then
        For i = rowCount - 1 To 1 Step -1
            oTable.DeleteRow(i)
        Next
    End If

    ON ERROR RESUME NEXT

    ' Get all services from WMI
    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * From Win32_Service Where DisplayName = 'Spooler' Or DisplayName = 'Fax'")
	
    ' For each services add a data row to the table
    For Each objItem in colItems
        Set TR = oTable.InsertRow()
        Set TD = TR.InsertCell()
        TD.InnerHTML = "<input type=""checkbox""></td>"
        Set TD = TR.InsertCell()
        TD.InnerHTML = objItem.Name
        Set TD = TR.InsertCell()
        TD.InnerHTML = objItem.State

    Next
End Sub
 
Sub btn_start_onClick()

    strComputer = "."                                                                                                   
    Set oTable = Document.All("list_servicenames")
    rowCount = oTable.Rows.Length

    ' Process all selected rows of table, Start service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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                                                                                                                

    ' Refresh Service table
    ListServices(strComputer)                                                                                           

End Sub
 
Sub btn_stop_onClick()

    strComputer = "."                                                                                                   
    Set oTable = Document.All("list_servicenames")
    rowCount = oTable.Rows.Length

    ' Process all selected rows of table, Stop service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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()

    strComputer = "."                                                                                                   
    Set oTable = Document.All("list_servicenames")
    rowCount = oTable.Rows.Length

    ' Process all selected rows of table, Stop service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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                                                                                                                

    ' Process all selected rows of table, Start service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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(".")
End Sub
 
Sub btn_exit_onClick()
    Window.Close
End Sub
 
</script>
 
<body>

<table border=1 id=list_servicenames> 
<col align="center">
<col align="left">
<col align="center">
<tr> 
<th align="center">Select</td>
<th align="center">Name</td>
<th align="center">State</td>
</tr> 
</table> 

 
<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

Dennis DAsked:
Who is Participating?
 
Bill PrewCommented:
Okay, the code is actually performing properly.  I added a little error checking in the code below to give some visibility if WMI Start / Stop return non-zero error codes.  Links to pages where those codes are described below also.

For one thing, to stop and start services, you will need this running in elevated admin mode.  One way to accomplish that is to start a Command Prompt using "Run as Administrator" and then just launch the HTA file name from the prompt.  Or you can probably create a shortcut to the HTA file and set it to run in elevated privs.

I was able to stop and start the "iPod Service" here with no problems.  Keep in mind many services won't be as simple as starting / stopping them.  For example the Fax service runs on demand, so even though your Start request is processed successfully, it will immediately stop itself since there is no request for it.  In addition many services have dependencies to other services.  So if you try to stop a service when other running services are active that depend on it, you will get an error.  The same is true if you try to start a service that is dependent on other services that are not running, it will error.  Hope this helps.


<head>
<title>Server Services</title>
<HTA:APPLICATION 
    APPLICATIONNAME="Server Services"
    BORDER="thin"
    SCROLL="no"
    MinimizeButton="no"
    MaximizeButton="no"
    SINGLEINSTANCE="yes"
    ID="oHTA"
>
<APPLICATION:HTA>
</head>

<style>
body {
    background-color: black;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 25px;
}
table {
    width: 100%;
    border-color: firebrick;
    background-color: black;
    color: khaki;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 12px;
}
th {
    border-color: firebrick;
    background-color: black;
    color: khaki;
}
td {
    border-color: firebrick;
}

</style>

<script language="VBScript">

Sub Resize()
    Window.ResizeTo 380,200
End Sub
 
Sub Window_OnLoad
    ' Populate table with list of services
    Window.ResizeTo 380,200
    ListServices(".")
End Sub
 
Sub ListServices(sComputer)
    Set oTable = Document.All("list_servicenames")

    ' Delete any existing data rows
    rowCount = oTable.Rows.Length
    If rowCount > 1 Then
        For i = rowCount - 1 To 1 Step -1
            oTable.DeleteRow(i)
        Next
    End If

    ON ERROR RESUME NEXT

    ' Get all services from WMI
    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * From Win32_Service Where DisplayName = 'Spooler' Or DisplayName = 'Fax' Or DisplayName = 'iPod Service'")
	
    ' For each services add a data row to the table
    For Each objItem in colItems
        Set TR = oTable.InsertRow()
        Set TD = TR.InsertCell()
        TD.InnerHTML = "<input type=""checkbox""></td>"
        Set TD = TR.InsertCell()
        TD.InnerHTML = objItem.Name
        Set TD = TR.InsertCell()
        TD.InnerHTML = objItem.State

    Next
End Sub
 
Sub btn_start_onClick()

    strComputer = "."                                                                                                   
    Set oTable = Document.All("list_servicenames")
    rowCount = oTable.Rows.Length

    ' Process all selected rows of table, Start service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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
            If errReturn <> 0 Then
                MsgBox "Action = Start" & vbCrLf & "Service = " & strService & vbCrLf & "Error = " & errReturn, 0, "Error occured"
            End If
        End If                                                                                                          
    Next                                                                                                                

    ' Refresh Service table
    ListServices(strComputer)                                                                                           

End Sub
 
Sub btn_stop_onClick()

    strComputer = "."                                                                                                   
    Set oTable = Document.All("list_servicenames")
    rowCount = oTable.Rows.Length

    ' Process all selected rows of table, Stop service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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                                                                                                        
            If errReturn <> 0 Then
                MsgBox "Action = Stop" & vbCrLf & "Service = " & strService & vbCrLf & "Error = " & errReturn, 0, "Error occured"
            End If
        End If                                                                                                          
    Next                                                                                                                
    ListServices(strComputer)                                                                                           

End Sub
 
Sub btn_restart_onClick()

    strComputer = "."                                                                                                   
    Set oTable = Document.All("list_servicenames")
    rowCount = oTable.Rows.Length

    ' Process all selected rows of table, Stop service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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                                                                                                        
            If errReturn <> 0 Then
                MsgBox "Action = Stop" & vbCrLf & "Service = " & strService & vbCrLf & "Error = " & errReturn, 0, "Error occured"
            End If
        End If                                                                                                          
    Next                                                                                                                

    ' Process all selected rows of table, Start service
    For i = 1 To rowCount - 1 Step 1
        If UCase(Left(oTable.Rows(i).Cells(0).InnerHTML, 14)) = "<INPUT CHECKED" Then
            strService = oTable.Rows(i).Cells(1).InnerHTML
            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                                                                                                        
            If errReturn <> 0 Then
                MsgBox "Action = Start" & vbCrLf & "Service = " & strService & vbCrLf & "Error = " & errReturn, 0, "Error occured"
            End If
        End If                                                                                                          
    Next                                                                                                                
    ListServices(strComputer)                                                                                           
End Sub                                                                                                                 
 
Sub btn_Refresh_onClick()
    ListServices(".")
End Sub
 
Sub btn_exit_onClick()
    Window.Close
End Sub
 
</script>
 
<body>

<table border=1 id=list_servicenames> 
<col align="center">
<col align="left">
<col align="center">
<tr> 
<th align="center">Select</td>
<th align="center">Name</td>
<th align="center">State</td>
</tr> 
</table> 

 
<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


»bp
0
 
Dennis DAuthor Commented:
Thank you again Bill.  I apologize for wasting your time with something so simple.  I launched it from a batch file with admin rights and it worked perfectly.  And I'm sorry if starting a new question wasn't the right thing to do.  Being new here, I wasn't sure if I was supposed to continue with the original thread or start a new one since the original was already closed.  Either way, I really appreciate all of your help.  Thank You!!
0
 
Bill PrewCommented:
No need for apologies, it's all good.  If we all know everything we wouldn't be here...

Starting another question was the right choice, especially since the prior was closed.  I'd suggest that when a prior question is closed, or when you are asking something that may be related to, but wasn't really part of the original question, then a new question is a good idea.  You can put a link in the old question referencing the new one for future readers if that makes sense.

Welcome.


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.