Microsoft

An in-depth look at the Find Master Browser Utility script

Having trouble communicating on your network? You can locate the master browser in a Windows workgroup and start troubleshooting in seconds with Greg Shultz's handy Find Master Browser Utility. Here, he explains it step-by-step.


To troubleshoot peer-to-peer networks, you must first be able to locate the master browser. I found a way to make this task a quick one by developing the Find Master Browser Utility, which I introduced in my previous Daily Drill Down. The utility uses VBScript, the NBTSTAT utility, and NetBIOS table output to pinpoint which server is the current master browser in a matter of seconds.

In this Daily Drill Down, I’ll analyze each segment of code in the script to give you a better understanding of how it all works.

Getting started
Lines 1 through 3 use the Dim statement to define the variables used in the script. You’ll notice quite a few variables in this script—22 to be exact. As you look at the variable names, you’ll see that I use names that represent the data each one will hold. Since I have several variables that hold similar data, I use the same name and append numbers to the ends of the names to keep track of them.

Line 4 sets up the ForReading constant and sets its value to 1. Setting up this constant makes it easy to configure the command that opens text files for reading information.

In lines 5 through 7, I use Set statements to assign object references to variables. Line 5 uses the Set statement to activate all of the features of the Windows Script Host object model and to assign those features to the WshShell variable. Because this script will need to access the file system in order to open text files, line 6 uses the Set statement to activate all of the file system features provided by the FileSystemObject and assigns them to the FileSystem variable.

Line 7 activates the regular expression object with the New RegExp command. The object reference is then assigned to the RegularExpression variable.

In line 8, I use the Popup method to display a progress indicator and ask the user to wait. This dialog box is displayed on the screen for one second and then closes itself in order for the script to continue processing the remaining commands.

Getting a list of the computers
Lines 9 and 10 basically create and retrieve the list of the computers in the peer-to-peer workgroup. In line 9, I use the Run method of the WshShell object to shell out to DOS via the Cmd.exe /c command.

Note
Cmd.exe is the command interpreter for Windows NT and Windows 2000. If you plan to run this script from Windows 95, Windows 98, or Windows Me, simply replace Cmd.exe with Command.com.

Here, Cmd.exe starts a DOS 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 a Net View command. I then use the DOS-based redirection symbol (>) to pipe the results of the Net View command into the NetViewList.txt file, which is created in the C:\Winnt\Temp folder.

Note
If you plan to run this script from Windows 95, Windows 98, or Windows Me, simply replace C:\Winnt\Temp with C:\Windows\Temp.

In line 10, I use the OpenTextFile method to open the file for reading and use the Set command to assign that information to the TheNVFile object variable.

The main routine
Lines 11 through 29 make up a Do While…Loop statement that constructs the main routine in the script. Within this loop, I call three functions (two of which use the regular expressions feature) that extract a computer name, get that computer’s NetBIOS table, and search for the master browser identifier. I also search for and extract the workgroup name to use in the Find Master Browser Utility’s output display. I’ll discuss the three functions in more detail in a moment.

In line 11, I initiate the loop that runs until the AtEndOfStream file pointer is set to True, which indicates that the script has read all the data in the NetViewList.txt file. Line 12 reads one line of the text file and assigns that string to the TheLine variable. Then, line 13 assigns the ThePattern1 variable to a string containing the characters “\\”, which, as I mentioned earlier, signify that the text immediately following is the name of the computer. I then call the FindPattern function in line 14 and send it the TheLine and ThePattern1 variables. The FindPattern function will return a Boolean variable of True if the characters are found in the string and of False if they’re not. That result will then be assigned to the PFound1 variable.

Line 15 then uses an If…Then statement to test the value of the PFound1 variable. If the PFound1 variable is set to False, execution of the script drops to line 29, and the loop begins again. If the PFound1 variable is set to True, execution of the script proceeds to line 16, which assigns ThePattern2 variable to a string containing the characters “\\” and two of the special regular expression characters, “\w” and “*”.

You can think of these special characters as wild cards. The regular expression character “\w” specifies that any characters, including letters and numbers, that follow the “\\” should be considered a match. The regular expression character “*” specifies that there can be any number of characters (i.e., one or more).

Note
At first glance, this string “\\\\\w*” looks kind of funny due to the fact that the backslash (\) character is both part of the computer’s UNC name and a regular expression character. This regular expression character indicates that the next character is either a special character or a literal. I need to precede each backslash character in the UNC name with the regular expression special character “\”. So the first five characters in this string would read: special character, literal character, special character, literal character, and special character.

Moving on to line 17, I set the Flag variable to 1. This variable will be sent to the GetPattern function and will indicate that this is the first time this function is being called and that the result to be returned should be a computer name. (More on this when I discuss the GetPattern function in a moment.)

In line 18, I call the GetPattern function and send it the TheLine, ThePattern2, and Flag variables. The result returned will be the name of the computer, which will then be assigned to the SysName variable.

I then send the SysName variable to the GetNBTable function in line 19, which will run the NBTSTAT utility and return the NetBIOS table for that particular computer. That result will then be assigned to the NBTable variable.

In line 20, I assign the ThePattern3 variable to a string containing the string “MSBROWSE.” Line 21 then calls the FindPattern function and sends it the NBTable and ThePattern3 variables. The result, which will be a Boolean value, is then assigned to the Pfound2 variable.

Line 22 then uses an If…Then statement to test the value of the Pfound2 variable. If the Pfound2 variable is set to False, execution of the script drops to line 29, and the loop begins again.

If the Pfound2 variable is set to True, then the “MSBROWSE” string was found in the NetBIOS table, and execution of the script proceeds to line 23. Line 23 assigns ThePattern4 variable to a string containing two of the special regular expression characters “\w*”, three blank spaces, and the NetBIOS code “<1E>”, which signifies the name of the workgroup. This regular expression configuration basically indicates that the function is to find the NetBIOS code <1E> and return all the text that precedes it.

In line 24, I set the Flag variable to 2. This variable will be sent to the GetPattern function and will indicate that this is the second time this function is being called and that the result to be returned should be the workgroup name.

Line 25 calls the GetPattern function and sends it the TheLine, ThePattern4, and Flag variables. The result returned will be the name of the workgroup, which will then be assigned to the WrkGrp variable. Because at this point the master browser has been located, I use an Exit Do statement in line 26 to quit the loop, and the script execution picks up at line 30.

Lines 30 through 37 make up an If…Then…Else statement that tests the value of the Pfound2 variable and then uses the MsgBox function to display the result of the Find Master Browser Utility. If the Pfound2 variable is set to True, the master browser was found, and a message listing the name of the computer and the name of the peer-to-peer workgroup is displayed in a dialog box. If the Pfound2 variable is set to False, the master browser wasn’t found, and an appropriate message is displayed.

Then, line 38 closes the NetViewList.txt file, and line 39 deletes it from the hard disk. Finally, line 40 calls the Windows Script Host Quit method, which terminates the script.

The FindPattern function
The FindPattern function, in lines 41 through 48, is very straightforward. The function is passed by two variables: TheText, which contains a string of text, and ThePattern, which contains a specific group of characters that can include some of the special regular expression characters. (Again, these characters work like wild cards.) This group of characters is called a pattern. Now, the job of the FindPattern function is to search the text string for the pattern and return a Boolean value that indicates whether the pattern was found.

In line 42, I assign the contents of the ThePattern variable to the regular expression Pattern property. In lines 43 through 47, I use an If…Then…Else statement to run the regular expression Test method, which determines whether the pattern is contained in the string of text. If it is, I return the Boolean value True by assigning that result to the function name in line 44. If it isn’t, I set the return result to False in line 46.

The GetPattern function
The GetPattern function in lines 49 through 58 is sent three variables: TheText, which contains a string of text, ThePattern, which contains a specific group of characters that can include some of the special regular expression characters, and Flag, which contains a value that will indicate which of the two possible pieces of information the function is to return. This function’s job is to extract the pattern from the text string and then return it to the calling routine.

In line 50, I assign the contents of the ThePattern variable to the regular expression Pattern property. In line 51, I run the regular expression Execute method, which locates and extracts the pattern from the string and then assigns it to the Matches collection. Now, since a collection is a form of an array, I must use a For…Next loop to read through the collection and access the pattern. To do so, I use the Value property of the Match object in line 53 and assign the pattern to the TheMatch variable.

Once I’ve accessed the pattern and assigned it to the TheMatch variable, I have to clean it up a bit. The method I use will depend on what specific piece of information is in the pattern—either a system name or a workgroup name. If the Flag is set to 1, the TheMatch variable contains a system name, and cleaning it up simply means removing the UNC “\\” characters that precede the name. To do so, I use the Mid function to advance to the third character in the string, or pattern, and return it and every character that follows and then reassign that to the TheMatch variable. If the Flag is set to 2, the TheMatch variable contains a workgroup name, and cleaning it up simply means returning the first 15 characters in the string. Fifteen characters is the maximum length of a Windows workgroup name. I then return the contents of the TheMatch variable by assigning its contents to the function name in line 57.

The GetNBTable function
The GetNBTable function in lines 59 through 66 is sent the name of a computer in the workgroup and returns that computer’s NetBIOS table. Line 60 creates the command line by concatenating the NBTSTAT command with the computer name. Then, line 61 uses the Run method of the WshShell object to shell out to DOS via the Cmd.exe /c command.

Note
Cmd.exe is the command interpreter for Windows NT and Windows 2000. If you plan to run this script from Windows 95, Windows 98, or Windows Me, simply replace Cmd.exe with Command.com.

Here, Cmd.exe starts a DOS 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 contained in the NBCommand variable. I then use the DOS-based redirection symbol (>) to pipe the results of the NBTSTAT command into the NBTList.txt file, which is created in the C:\Winnt\Temp folder.

Note
If you plan to run this script from Windows 95, Windows 98, or Windows Me, simply replace C:\Winnt\Temp with C:\Windows\Temp.

In line 62, I use the OpenTextFile method to open the file for reading and use the Set command to assign that information to the TheNBTFile object variable. Then, in line 63, I use the ReadAll method to read the entire contents of the file and assign the contents to the function name. Line 64 closes the NBTList.txt file, and line 65 deletes it from the hard disk.

Using the Find Master Browser Utility
Now that you have a good idea of how the Find Master Browser Utility script works, let’s take a look at how you’ll use it. As an example, let’s use the network shown in the Net View command in the Technical Overview section of “Find the master browser in a peer-to-peer workgroup using regular expressions.“

When you run the Find Master Browser Utility, you’ll see the progress dialog box, as shown in Figure A, which alerts you that the Find Master Browser Utility has begun its job.

Figure A


When the Find Master Browser Utility locates the system acting as the master browser on the peer-to-peer workgroup, it will display that information in the dialog box shown in Figure B.

Figure B


If the master browser isn’t found on the network, chances are that an election is in progress and you’ll see the dialog box shown in Figure C. Wait a few minutes and then run the utility again.

Figure C


Conclusion
While this script is extremely complex, it’s a powerful tool you can use for troubleshooting a Windows workgroup. I’ve examined in detail each line of the script to give you a better idea of what goes on behind the scenes. Taken individually, the Net View and NetBIOS commands are cumbersome to work with when searching for the master browser. With this script, the process of locating the master browser takes only seconds. If you need the script without the lesson, you can download the finished product by clicking here.

About Greg Shultz

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.

Editor's Picks