Ask:
Hey, scripting guy!. I want to create a form with four radio buttons, each of which represents a single computer. You can select one radio button, click another button, and then the script will run on the selected computer. How can I do that?
--CW
For:
Hi, CW. If all we're talking about is VBScript and Windows Script Host, the problem is simple: it's impossible. In addition to displaying a message box, VBScript and WSH cannot create a graphical user interface; There is no way to use radio buttons, list boxes, Drop-down lists, and other graphic elements with scripts.
But--oh, you must have seen this before. Yes: Let's tell you something, and then tell you the way you do things. Hey, everybody needs a little more skill, doesn't it? Yes: Although you don't do this in VBScript, you can do it with an HTA (an HTML application).
We don't spend a lot of time talking about HTA today; If you're interested, you can find the webcast we launched on that topic about a year ago. It can be said that an HTA enables us to combine Internet Explorer and scripting code, and in turn provides a graphical user interface for scripting. While there are some ways to incorporate a graphical user interface into a script, this may be the easiest way for users to start dabbling in GUI development.
Let's first provide the code for the HTA and then explain how it works. This sample HTA displays four radio buttons, each of which represents a single computer. Select a computer, click a Run Script button, and when you are done, a subroutine will run. The program connects to the selected computer, and then reports the name of the operating system that is installed on the computer. Pretty good, huh? To understand how it works, copy the code, paste it into Notepad, and then save the file with an. hta file name extension (such as: Os_name.hta). Do not use the. vbs file name extension; The extension must be an. hta.
Copy Code code as follows:
<script language= "VBScript" >
Sub RunScript
If computeroption (0). Checked Then
StrComputer = computeroption (0). Value
End If
If computeroption (1). Checked Then
StrComputer = computeroption (1). Value
End If
If computeroption (2). Checked Then
StrComputer = Computeroption (2). Value
End If
If computeroption (3). Checked Then
StrComputer = Computeroption (3). Value
End If
If strComputer = "" Then
Exit Sub
End If
Set objWMIService = GetObject _
("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colitems = objWMIService.ExecQuery _
("SELECT * from Win32_OperatingSystem")
For each objitem in colitems
Msgbox objitem.caption
Next
End Sub
Sub Cancelscript
Self.close ()
End Sub
</SCRIPT>
<BODY>
<input type= "Radio" name= "Computeroption" value= "atl-ws-01" >atl-ws-01<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-02" >atl-ws-02<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-03" >atl-ws-03<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-04" >atl-ws-04<P>
<input Id=runbutton class= "button" type= "button" value= "Run Script" name= "Ok_button"
onclick= "RunScript" >
<input Id=runbutton class= "button" type= "button" value= "Cancel" name= "Cancel_button"
onclick= "Cancelscript" >
</BODY>
So what does the code contain? We can split the code into four parts: there are two parts that use HTML tags to implement radio buttons as well as the run script and Cancel buttons, while the other two sections are based on whether you clicked Run script or cancel To run the subroutine. Let's take a closer look at these parts.
For example, the HTML code displays these four radio buttons here. (If you know HTML, there's nothing special about it; This is standard HTML encoding.) Note that all buttons have the same name (computeroption); This is to ensure that only one button can be selected at a time. Also note that the value of each button is set to the name of the appropriate computer:
<BODY>
<input type= "Radio" name= "Computeroption" value= "atl-ws-01" >atl-ws-01<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-02" >atl-ws-02<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-03" >atl-ws-03<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-04" >atl-ws-04<P>
This is the code that displays the Run Script and Cancel buttons. The key here is the OnClick parameter (indicates which subroutine to run when the button is clicked). As you can see by clicking the first button, the RunScript subroutine will run; Click the second button and the Cancelscript subroutine will run:
<input Id=runbutton class= "button" type= "button" value= "Run Script" name= "Ok_button"
onclick= "RunScript" >
<input Id=runbutton class= "button" type= "button" value= "Cancel" name= "Cancel_button"
onclick= "Cancelscript" >
</BODY>
By the way, the Cancelscript subroutine shuts down an HTA. As you can see, the whole process is not complicated at all:
Sub Cancelscript
Self.close ()
End Sub
Now--eventually! -We're going to have a good thing. Select a radio button, and then click Run Script. How does our HTA know what buttons are selected and how do we know which computer is running the script? So where do you want to run the script? Take it easy; it's all in the RunScript routine:
Copy Code code as follows:
Sub RunScript
If computeroption (0). Checked Then
StrComputer = computeroption (0). Value
End If
If computeroption (1). Checked Then
StrComputer = computeroption (1). Value
End If
If computeroption (2). Checked Then
StrComputer = Computeroption (2). Value
End If
If computeroption (3). Checked Then
StrComputer = Computeroption (3). Value
End If
If strComputer = "" Then
Exit Sub
End If
Set objWMIService = GetObject _
("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colitems = objWMIService.ExecQuery _
("SELECT * from Win32_OperatingSystem")
For each objitem in colitems
Msgbox objitem.caption
Next
End Sub
The first half of the subroutine is used to determine the specific button that is selected. The radio button (at least a button with the same name) is stored as an array; the first button in the array is item 0, the second button is Item 1, and so on. What we're doing here is determining which button is selected, which can be achieved by looking at the Checked property of each button. For example, this line of code determines whether the "Checked" property of button 0 (the first button in an array) is true, and if so, indicates that the button is selected:
If computeroption (0). Checked Then
What if "Checked" is true? Then, the value of the radio button is then assigned to the variable StrComputer (remember that the button's value is exactly the name of the computer):
StrComputer = computeroption (0). Value
What if "Checked" is False? It's no big deal; After all, we've checked the values of each radio button. Sooner or later, you'll figure out which button (and only one) is selected. If you finalize that no button is selected, the subroutine exits. This is the work that the code has done:
Copy Code code as follows:
If strComputer = "" Then
Exit Sub
End If
If a button is selected, then StrComputer will be the name of the computer to which we are connecting. This is what the second half of the subroutine does: connecting to the specified computer and returning the name of the operating system installed on it is a standard WMI script.
Yo! We'll bet that you'll be glad to get it done, won't you? Or, at least if we did, you'd be happy. However, there is one more point to be noted. The sample HTA we provide here can retrieve the name of the operating system that is installed on the computer and then display it in a message box. That's great, but what if you want to show a list of all the services that are installed on your computer? At this point, you will find yourself answering dozens of message boxes. This is not the user experience that you and I would like to get.
So, can we solve this problem? Of course. We don't want to spend more time on this, but we need to do a few things. First, we added a SPAN area to the HTA; this is just a recognizable area on the screen where you can write information. Use this type of code to place the SPAN (with the DataArea ID) below the button:
<P> <span id=dataarea></span>
Second, you need to collect the data and save it all in a variable instead of displaying all the data in a message box. The code sets the value of the variable StrText to any value currently in StrText plus the value of the "caption" attribute, plus the <BR> tag (equivalent to pressing the "enter" key on the keyboard in an HTML script):
StrText = strText & objitem.caption & "<BR>"
Finally, you need to set the SPAN's "InnerHTML" property to the value of the variable StrText:
dataarea.innerhtml = StrText
Are you clear? In addition, to avoid excessive interpretation, here is a modified HTA that collects the names of all the services installed on the computer and then automatically writes them to the HTA:
Copy Code code as follows:
<script language= "VBScript" >
Sub RunScript
If computeroption (0). Checked Then
StrComputer = computeroption (0). Value
End If
If computeroption (1). Checked Then
StrComputer = computeroption (1). Value
End If
If computeroption (2). Checked Then
StrComputer = Computeroption (2). Value
End If
If computeroption (3). Checked Then
StrComputer = Computeroption (3). Value
End If
If strComputer = "" Then
Exit Sub
End If
Set objWMIService = GetObject _
("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colitems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service")
For each objitem in colitems
StrText = strText & objitem.name & "<BR>"
Next
dataarea.innerhtml = StrText
End Sub
Sub Cancelscript
Self.close ()
End Sub
</SCRIPT>
<BODY>
<input type= "Radio" name= "Computeroption" value= "atl-ws-01" >atl-ws-01<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-02" >atl-ws-02<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-03" >atl-ws-03<BR>
<input type= "Radio" name= "Computeroption" value= "atl-ws-04" >atl-ws-04<P>
<input Id=runbutton class= "button" type= "button" value= "Run Script" name= "Ok_button"
onclick= "RunScript" >
<input Id=runbutton class= "button" type= "button" value= "Cancel" name= "Cancel_button"
onclick= "Cancelscript" >
<P>
<span id=dataarea></span>
</BODY>