Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Modifying an expert's script

Posted on 2009-06-29
6
268 Views
Last Modified: 2012-05-07
Hi guys,
This code was written by one of you gurus (rejoinder).
It presents all services on a local system and is brilliant.
Id like to modify the below code so that:

1) It allows you to input a computer name to check services against.
2) Does not present all services, but only the following:
a) Alerter
b) Workstation
c) Server

So basically, the hta as it is, Id like left unchanged, but instead of all services returned, only the 3 mentioned above, plus the ability to inputer a computer name to run this against.
Any help would be greatly appreciated.
<head>
<title>Server Services</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Server Services"
     BORDER="thin"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     ID="oHTA"
>
<APPLICATION:HTA>
</head>
 
<script language="VBScript">
 
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
    ListServices(".")
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()
    strComputer = "."
    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()
    strComputer = "."
    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()
    strComputer = "."
    '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(".")
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>
 
<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

0
Comment
Question by:Simon336697
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:rejoinder
ID: 24740661
This should do as you ask.
<head>
<title>Server Services</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Server Services"
     BORDER="thin"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     ID="oHTA"
>
<APPLICATION:HTA>
</head>
 
<script language="VBScript">
 
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
End SUb
 
Sub btn_Poll_onClick()
    ListServices(txt_computername.value)
End Sub
 
Sub ListServices(sComputer)
    ON ERROR RESUME NEXT
    
    set objDic = CreateObject("Scripting.Dictionary")
    objDic.Add "Alerter","Alerter"
    objDic.Add "lanmanworkstation","Workstation"
    objDic.Add "lanmanserver","Server"
    
    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
        if objDic.Exists(objItem.Name) then
            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
        end if
    Next
End Sub
 
Sub btn_start_onClick()
    strComputer = txt_computername.value
    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()
    strComputer = txt_computername.value
    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()
    strComputer = txt_computername.value
    '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(txt_computername.value)
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>
Computer Name <INPUT TYPE="text" ID="txt_computername" NAME="txt_computername" SIZE="40" MAXLENGTH="255"><input type="submit" value="Poll Computer" name="btn_Poll" id="btn_Poll" title="Click to poll the computer"><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

0
 
LVL 1

Author Comment

by:Simon336697
ID: 24740985
rejoinder,
Youre a gun.
Can I please just ask one more thing of your kind self?
Is there a way to first check if the machine is pingable, and:
a) if not ---> output "System down"
b) if up, do as normal.
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 500 total points
ID: 24741035
This provides the prompt plus the ability to retry just in case the ping didn't make it back and you know the computer is on.
<head>
<title>Server Services</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Server Services"
     BORDER="thin"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     ID="oHTA"
>
<APPLICATION:HTA>
</head>
 
<script language="VBScript">
 
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
End SUb
 
Sub btn_Poll_onClick()
    ListServices(txt_computername.value)
End Sub
 
Sub ListServices(sComputer)
    ON ERROR RESUME NEXT
    
    if NOT IsComputerOn(sComputer) then
        response = msgbox("Machine unavailable." & vbCRLF & "Do you wish to retry or cancel?",21,"ERROR!")
        if response = vbRetry then
            ListServices(sComputer)
        else
            Exit Sub
        end if
    end if
    
    set objDic = CreateObject("Scripting.Dictionary")
    objDic.Add "Alerter","Alerter"
    objDic.Add "lanmanworkstation","Workstation"
    objDic.Add "lanmanserver","Server"
    
    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
        if objDic.Exists(objItem.Name) then
            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
        end if
    Next
End Sub
 
Sub btn_start_onClick()
    strComputer = txt_computername.value
    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()
    strComputer = txt_computername.value
    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()
    strComputer = txt_computername.value
    '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(txt_computername.value)
End Sub
 
Sub btn_exit_onClick()
    Window.Close
End Sub
 
function list_servicenames_ColumnClick(colheader)
    list_servicenames.SortKey = colheader.index-1
end function
 
Function IsComputerOn(computer)
    'Ping to see if computer is active
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & computer & "'")
    For Each objStatus in objPing
        if IsNull(objStatus.StatusCode) or objStatus.StatusCode <> 0 Then
            boolPingReturned = False
        else
            boolPingReturned = True
        end if 
    next
    IsComputerOn = boolPingReturned
End Function
 
</script>
 
<body>
Computer Name <INPUT TYPE="text" ID="txt_computername" NAME="txt_computername" SIZE="40" MAXLENGTH="255"><input type="submit" value="Poll Computer" name="btn_Poll" id="btn_Poll" title="Click to poll the computer"><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

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:Simon336697
ID: 24741081
Champion,
rejoinder,
ive attached a screendump.
I know how to increase the widh of each section (the blue arrows), but not the total width of the box
housing the services output (the red arrow)
width.jpg
0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31598127
thanks so much rejoinder
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24741201
Thanks for the points!

The window size can be set in the "onload" sub.  Something like this could be used...

      intWidth = 800
      intHeight = 900
      Me.ResizeTo intWidth, intHeight
      Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))

The key is the the "Me.ResizeTo intWidth, intHeight" line
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Script is being strange 8 75
VBA taking too long 5 24
Need VBA code to change date format of Column A 5 23
VBA Delete selected Worksheets of each Workbook in a Folder 4 27
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question