Link to home
Start Free TrialLog in
Avatar of Dennis D
Dennis D

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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 Dennis D
Dennis D

ASKER

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