Windows

Automating IPConfig in Windows NT/2000 using Windows Scripting Host

Would you like to have a GUI IPConfig tool in Windows NT and 2000? In this Daily Drill Down, Greg Shultz, Windows Scripting Host guru, shows you how to build one.

In Windows 9x, you can use the Winipcfg utility to diagnose a system’s TCP/IP configuration. This utility works within the Windows 9x graphic environment. In Windows NT and 2000, however, the utility you use is called Ipconfig.exe, which works in the command line. In this Daily Drill Down, I’ll show you how to use a combination of scripting techniques to automate the Ipconfig.exe utility and bring it into the Windows GUI. As I do, I’ll describe each scripting technique and explain in layman’s terms how it functions—so you don’t have to be a programmer to create or understand these scripting techniques.

Winipcfg vs. IPConfig
When you need to take a look at a system’s TCP/IP configuration in Windows 9x, you access the Run dialog box, type Winipcfg.exe in the text box, and click OK. When you do, up pops a nicely organized dialog box that displays information on the system’s current TCP/IP configuration, as shown in Figure A.

Figure A
Windows 9x provides you with a graphical tool for displaying and working with a system’s TCP/IP configuration.


If a DHCP server handles your system’s TCP/IP configuration, you can easily change the system’s leased IP address by using the Release and Renew buttons provided in this dialog box. You can also click the More Info button to see an expanded dialog box containing more detailed information about your system’s TCP/IP configuration.

When you need to take a look at a Windows NT/2000 system’s TCP/IP configuration, however, you have to shell out to a command prompt and then manually type in the IPConfig command. If you want to change the system’s IP address or get more detailed information, you have to type the IPConfig command again with different switches for each operation. For example, to change the system’s leased IP address, you have to type the command Ipconfig /release followed by the command Ipconfig /renew.

Wouldn’t it be nice if you could get the same information in Windows NT and 2000 that Windows 9x presents so quickly in a nicely formatted dialog box? I’ve always thought so, so I created the tool for you.

When I set out to use Windows Script Host and VBScript to automate the Ipconfig.exe command to run in a Windows GUI, I discovered that it was advantageous to add a bit of HTML to the mix. While doing so makes the project a bit more complex, the fact that I was able to tie these three together into one package just emphasizes the power and versatility of the Windows Script Host. The end result is a nice little package that I call the IPConfig Automation Tool, which is on par with Windows 9x’s Winipcfg.exe utility and much easier to use than the standard IPConfig command-line utility.

Required: Internet Explorer
The technique that I implemented in HTML draws on a feature introduced in Internet Explorer (IE) 4.0. Therefore, in order to use the IPConfig Automation Tool, you need to be running either Internet Explorer 4.x or Internet Explorer 5.x.

Options you can add later
The job of the IPConfig Automation Tool is to automate the Ipconfig.exe utility in such a way that it emulates the Winipcfg.exe utility. To do so, the IPConfig Automation Tool will allow you to run the Ipconfig.exe utility’s five basic operations, which are available in both Windows NT and Windows 2000.

The Windows 2000 version of the Ipconfig.exe utility provides you with additional options. I didn’t include them in the IPConfig Automation Tool in order to make the tool compatible with both Windows NT and Windows 2000. Once you study and understand the techniques presented in this article, however, you can very easily add those additional options to the script.

Also, while IPConfig’s Release and Renew operations can use the network adapter name as an optional parameter, I didn’t add that feature to the IPConfig Automation Tool. Selecting the Release or Renew options will change IP address leases for all adapters bound to TCP/IP protocol. Again, you can very easily add those additional options to the IPConfig Automation Tool if you need them.

Why use HTML?
If you’ve been following along with my series of articles on using Windows Script Host and VBScript, you may be wondering why I’ve decided to bring HTML into the mix. I’ve found Windows Script Host and VBScript lacking in the ability to create robust user interfaces, which severely limits your ability to create nice-looking interactive scripts. There are basically only two types of dialog boxes that are available in Windows Script Host, VBScript, and even JScript. The first is an Input dialog box, in which you can prompt the user to type information in a text box. The second is a Message dialog box, in which you can display buttons and prompt the user to make a selection by clicking one of them. Regrettably, there are only a few standard buttons to choose from, and there is no way to customize the button text.

I could have used VBScript’s InputBox command to create a primitive menu-style dialog box. This box can display a numbered list and prompt the user to make a selection by typing a number in a text box and clicking OK. Such a dialog box is shown in Figure B.

Figure B
Windows Script Host and VBScript don’t really provide you with much in the way of user-interface creation tools.


While this would be an effective means of gathering user input, it is more elegant to be able to display a dialog box containing customized buttons, check boxes, drop-down lists, or even radio buttons. Since creating this type of dialog box isn’t possible in Windows Script Host, I turned to HTML’s FORM tags, which make it easy to create these types of customized controls.

Having decided to use HTML, the next challenge was to find a way to get an HTML file to communicate with a VBScript file so they could share information. Since Internet Explorer is also a scripting host much like Windows Script Host, and since IE allows you to program in both VBScript and JScript, I discovered that I could use VBScript commands in the HTML document to create a data file on the hard drive that can be used to transfer information from one file to the other.

IPConfig Automation Tool package overview
Now that you understand why I decided to bring HTML into the mix, let’s take a general look at the IPConfig Automation Tool.

The four files that make up the IPConfig Automation Tool are listed below in the order in which they run.
  1. ·        IpcAutoDriver.vbs
  2. ·        IpcAutoUI.htm
  3. ·        IpcAutoDialog.htm
  4. ·        IpcAutoMain.vbs

As you can see, the IPConfig Automation Tool package is made up of four files: two VBScript files and two HTML files. These four files work together in an interesting way with the two HTML files functioning together as one to create the user interface and the two VBScript files actually running the rest of the show.

In essence, the two HTML files use a combination of HTML, JScript, and VBScript to create the IPConfig Automation Tool user interface, which consists of a user-friendly dialog box that prompts the user to select one of the standard IPConfig operations. Once the user has made a selection, that selection is identified, and that information is written to a text file where it will be available to the VBScript file that acts as the workhorse for the IPConfig Automation Tool.

The first VBScript file is the IPConfig Automation Tool driver, and it performs two tasks. First, it launches the HTML files and waits for them to do their part of the operation. Once they finish, the driver launches the second VBScript file, which retrieves the user’s selection from a text file.

If the user chooses to view information generated by the IPConfig command, the script will create a report file, complete with header information that it will later use to display the information. At this point, the script shells out to a command prompt and runs the IPConfig command with the appropriate switch. Now, if the user chooses to view information, IPConfig will use good-old DOS-based redirection symbols to redirect the results to the same report file I created earlier with VBScript commands.

Getting started
Now that you understand the key parts played by each of the four files, you’re ready to get started. Create a folder in the root directory of drive C: called IpcAuto. You’ll use this folder to save each of the four files you’ll create. It’s also the folder in which the IPConfig Automation Tool will create its two data files.

Once you’ve created your folder, you’re ready to create the four files that make up the IPConfig Automation Tool. Be sure that you type all the commands exactly as shown (but don’t type the line numbers; they are for reference only). If typing in lines of code isn’t your cup of tea, don’t worry; the four files are available for download here.

The IpcAutoDriver.vbs file
Creating the IpcAutoDriver.vbs file is a snap. The smallest file of the four, the code consists of only nine lines. To begin, launch Notepad and type the IpcAutoDriver.vbs script shown here. When you’ve finished, save the file as IpcAutoDriver.vbs in the IpcAuto folder you created earlier.

The Dim statement in Line 1 defines the variables that the script will use. By doing so, the script allocates space in memory that it will use to store information as it does its job. Line 2 activates all of the features of the Windows Script Host object model by linking the WshShell variable and the Wscript object together, which ultimately provides you with direct access to the Windows Shell. Line 3 assigns the Again variable, which will act as a flag, to a value of 6.

Lines 4 through 8 make up a Do…Loop statement, which essentially runs the IpcAutoDriver.vbs script continuously until you’re done using the IPConfig Automation Tool.

Within the Do…Loop statement, Line 5 uses the Run method of the WshShell object to launch Internet Explorer and load the IpcAutoUI.htm file. The value 3 is the window’s size parameter that configures Internet Explorer to open the IpcAutoUI.htm file in a maximized window. The True parameter forces the script to wait until the Internet Explorer window is closed before processing the next command in the Do…Loop statement.

Once the Internet Explorer window is closed, the script processes Line 6, which again uses the Run method, but this time to launch the second VBScript file that is the workhorse for the IPConfig Automation Tool. Since this script doesn’t have a window, I can omit the window size parameter; however, I do use the True parameter to prevent the script from executing the next command until the IpcAutoMain script is finished.

When the IpcAutoMain script finishes its task, the IPConfig Automation Tool has done its job. Nevertheless, the user may want to run it again in some cases. For example, if as a troubleshooting technique you select the Release option to let go of the system’s leased IP address, you’ll then want to immediately select the Renew option to request and receive a new IP address from the DHCP server.

I use the MsgBox function to ask whether a user wants to rerun the program. By clicking Yes, the Again flag is once more assigned a value of 6. Clicking No assigns Again a value of 7. Line 8 specifies that the Do…Loop uses the Until construct to continue processing the loop until Again has a value of 7. Finally, Line 9 uses the Quit method to terminate the script.

The IpcAutoUI.htm file
To create IpcAutoUI.htm, launch Notepad and type the commands shown in Listing B. When you’re finished, save the file as IpcAutoUI.htm in the IpcAuto folder.

Listing B: IpcAutoUI.htm
The IpcAutoUI.htm file looks deceptively simple, but despite its simplicity, it performs two significant functions in the IPConfig Automation Tool. Click here to view the script.

First, it displays the dialog box that will contain the main user interface for the IPConfig Automation Tool. Second, it remains on the screen, effectively pausing the IpcAutoDriver.vbs file while the dialog box gathers input from the user and saves it to a file on the hard drive.

Since this is an HTML file, this listing contains a set of standard HTML tags. For instance, Line 1 is the obligatory <HTML> tag.

Line 4 identifies the next three lines as JScript code. (I couldn’t use VBScript here as it’s not capable of processing the showModalDialog method.) Line 5 declares that these three lines are part of the function called CreateDialog.

Line 6 accesses the window object and a special method provided by Internet Explorer 4.x and up. This is called showModalDialog. The showModalDialog method opens a second Internet Explorer window and configures this window to appear as a standard dialog box by removing all the toolbars and menus. The term Modal in the method name indicates that this second Internet Explorer window will appear on top of the first and will retain the focus until closed. What this means is that until you close the second window, you won’t be able to access the first window.

The parameters contained in parenthesis specify which file contains the contents for the dialog box, how large the dialog box should be, and its position on the screen. I’ve configured this command to get the contents for the dialog box from the file IpcAutoDialog.htm, specified that the dialog box is to be 300 by 300 pixels in size, and that it should appear in the middle of the screen.

Line 10 contains a special event handler called onLoad. The job of onLoad is to run the JScript script as soon as the HTML file loads.

Lines 12 and 13 simply create a header on the page to identify it as a part of the IPConfig Automation Tool. Line 14 is a prompt that informs the user to close this window in order to continue running the IPConfig Automation Tool. We’ll see how this works in a moment when we look at the IPConfig Automation Tool in action.

The IpcAutoDialog.htm file
Use Notepad to create the IpcAutoDialog.htm file shown here. When you’ve finished, save the file as IpcAutoDialog.htm in the IpcAuto folder.

IpcAutoDialog.htm, as I mentioned above, contains the HTML commands necessary to create the customized controls for the dialog box. This HTML file also contains a VBScript subroutine that determines what option the user selected in the dialog box and then saves that information to a file. Again, I’ll bypass the standard HTML tags and go straight for the pertinent sections of the file.

Line 5 declares that the next 12 lines make up the DetermineSelection_OnClick subroutine that will launch as soon as the user makes a selection in the dialog box and clicks the OK button. Line 6 establishes the three variables that the script will use to store information.

Line 7 uses a Set command to assign the FileSystemObject object reference to the FileSystem variable, which provides you with the ability to access and manipulate files and folders. Line 8 uses the OpenTextFile method to create a new file called IpcAutoChoice.txt, and then the Set command assigns that information to the TheFile object variable. (While Internet Explorer will allow you to use this object in a script that it’s hosting, it considers it a breach in security and will let you know, as you’ll see in a moment when you look at the IPConfig Automation Tool in action.) Line 9 uses a Set command to assign the form object to the Checker variable, which will allow the script to access the form and determine which option the user selects in the dialog box.

Lines 10 through 15 consist of a For…Next statement, which cycles through each of the five options on the form and looks for the one that’s selected. When it finds the selected option, it assigns the value of that option to the Whichone variable and then exits the loop.

Line 16 writes the contents of the Whichone variable to the IpcAutoChoice.txt file. Then, line 17 closes the file. Line 18 closes the dialog box window.

Dropping down to the HTML part of the file, line 24 sets up the dialog box header. Line 25 uses the FORM tag to set up the form, which will contain the controls for the dialog box. I set the action attribute to Null since the form isn’t actually going to be sent to a Web server. I then use the id and name attributes to identify the form as IPCForm.

Lines 26 through 35 use the INPUT tag to create a series of radio buttons that display the IPConfig command’s basic options. The Checked attribute at the end of Line 26 specifies that the Minimal option is selected by default. You’ll notice that the id and name attributes identify each of the option buttons as being IPCOptions. This will then allow the script to determine exactly what piece of information from the form it is to look at. In Line 37, I use another INPUT tag to create an OK button. I then use the id and name attributes to identify the VBScript subroutine and the place to jump to when the user clicks the OK button.

The IpcAutoMain.vbs file
To create the IpcAutoMain.vbs file, launch Notepad and type the commands shown here. When you’re finished, save the file as IpcAutoMain.vbs in the IpcAuto folder.

The IpcAutoMain.vbs file is the real workhorse of the IPConfig Automation Tool. This script will retrieve the data in the IpcAutoChoice.txt file, determine what option the user selected in the dialog box, and then shell out to a command prompt window and then run the appropriate IPConfig command. If the user selects an option for displaying information from the IPConfig command, the script will launch Notepad and display the information. Let’s take a look at the VBScript commands that make this script work.

Line 1 establishes the variables that this script uses, and line 2 establishes two constants I’ll use to control the commands that work with the data files. Lines 3 and 4 activate the WshShell and FileSystem object variables, the same way as I explained for the script above. Line 5 allows the script to open the IpcAutoChoice.txt file for reading.

Line 6 reads the data from the IpcAutoChoice.txt file and assigns it to the Choice variable. Line 7 then closes the IpcAutoChoice.txt file.

Lines 8 through 16 make up an If…Then statement that first determines if the Choice variable contains any of the three options that cause the IPConfig utility to generate output. If it does, line 9 allows the script to create the IpcAutoFile.txt file that will later be used to display the output generated by the IPConfig utility. Lines 10 through 14 use a series of WriteLine methods to create the file header, and then line 15 closes the file.

Lines 17 through 33 make up a Select Case statement that determines what value is stored in the Choice variable and runs the appropriate set of commands. For example, if the Choice variable contains the value “Full,” then the Case “Full” statement on Line 21 would be true. Line 22 then would use the Run method of the WshShell object to shell out to a command prompt window via the Cmd.exe /c command. Here, Cmd.exe starts a command prompt session and the /c parameter configures the command interpreter to carry out the commands that follow it and then exit. In this case, the command that follows it is the Ipconfig.exe /all command. I then use the DOS-based redirection symbols (>>) to pipe the results of the command into the IpcAutoFile.txt file in the IpcAuto folder.

Then, Line 23 uses the Run method of the WshShell object to launch Notepad and open the report file. As soon as that occurs, Line 24 ends the Select…Case statement and Line 34 runs the Windows Script Host Quit method, which terminates the script.

Using the IPConfig Automation Tool
Now that you understand how the IPConfig Automation Tool works, let’s use it. To begin, double-click IpcAutoDriver.vbs in the IpcAuto folder. When you do, you’ll see the IPConfig Automation Tool dialog box, which overlays the Internet Explorer/IpcAutoUI window, as shown in Figure C.

Figure C
When you launch the program, you’ll see the IPConfig Automation Tool dialog box on top of the Internet Explorer/IpcAutoUI window.


Once you choose an option button and click OK, Internet Explorer will display the message box shown in Figure D. This is a result of Internet Explorer’s security features discovering that the script it is running is about to write information to the hard drive. Normally, you wouldn’t want to allow this to occur, but in this case it’s okay, so click the Yes button. Both the warning and the IPConfig Automation Tool dialog box will disappear. You’ll then close the Internet Explorer/IpcAutoUI window.

Figure D
Because Internet Explorer sees that the script that it is running is about to write a file to the hard drive, it displays this security warning.


What you’ll see next will depend on the option you selected. If you chose either the Release or the Renew option, you’ll see one of the dialog boxes shown in Figure E. If you chose Minimal, for example, you’ll see the results of the operation displayed in a Notepad window, as shown in Figure F.

Figure E
If you select either the Release or the Renew option, you’ll see one of these dialog boxes.


Figure F
The IPConfig Automation Tool uses Notepad to display the results of the IPConfig utility.


When you close Notepad, you’ll see the dialog box, shown in Figure G, which asks whether or not you want to run the IPConfig Automation Tool again. If you select Yes, you’ll see the dialog box again and you will be able to make another selection. If you select No, the IPConfig Automation Tool will terminate.

Figure G
To make it easy to run the IPConfig Automation Tool multiple times, the driver script displays this dialog box.


Conclusion
IPConfig is a valuable Windows NT/2000 TCP/IP troubleshooting tool. Unfortunately, this utility runs in a less-efficient command prompt. In this Daily Drill Down, I’ve shown you how to use a combination of scripting techniques to bring the IPConfig utility into the Windows GUI via the IPConfig Automation Tool.

Further reading
For more information on Windows Scripting Host, read these TechProGuild features by Greg Shultz:

 

About

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

0 comments

Editor's Picks