Subscribe to Windows IT Pro
October 06, 2003 12:00 AM

Enumerating Group Membership

Drilling down through nested groups
Windows IT Pro
InstantDoc ID #40230
Rating: (6)
Downloads
40230.zip

You can provide input to scripts in many ways. Commonly used approaches include using script arguments that you specify at runtime, manually editing variables in the script on each run, and using an input file. The lesser-known approaches include using the Choice command, which works well when users need to choose an option from a small static set of options, and using the Set /p command, which lets users enter any information they want.

For EnumGroups.bat, I used the Choice command to handle the input for the file types. The Choice command was ideal because this set of options consists of only three static items. The Choice command accepts only those options you specify; any other option will cause the script to stop. The script won't continue until the user enters a valid choice.

Although the Choice command is a good fit for capturing the file type, it wouldn't work for capturing the group name or domain name because many possible names exist and they can change frequently. The Set /p command works well for these two inputs. However, the Set /p command doesn't test whether the user's input is valid, so EnumGroups.bat contains some error-handling code to intercept bad input.

Sometimes you might need to change users' input to all uppercase or lowercase characters. I uppercased the domain group names that the Set /p command captures, so if a user enters sales for the domain name, the script changes that string to SALES. Having all uppercase letters isn't necessary to have EnumGroups.bat work properly, nor is it a formatting requirement for the script's output report. I just wanted to use this capability to demonstrate how you can change the case of strings because, although this procedure is obscure, it can be quite useful.

Listing 2 shows the code that uppercases the domain names. EnumGroups.bat uses the Setlocal Enabledelayedexpansion command, so this code expands the Dom variable while performing the character replacement. Typically, the character replacement syntax is

Set %Var:oldchar=newchar%

where oldchar is the character you want to replace and newchar is the replacement character. If you want to use variables to represent oldchar and newchar, the command would look like

Set%Var:%Oldchar%=%Newchar%%

However, this command would fail at runtime because of the nested percent signs. An alternative is to use exclamation points instead of percent signs as variable symbols so that the command would look like

Set !Var%Oldchar%=%Newchar%!

This command lets you accomplish the character replacement without any errors.

Customize the Script
I tested EnumGroups.bat on machines running Win2K Professional Service Pack 3 (SP3) and Windows XP and was able to query group members in both Win2K and NT 4.0 domains. To get the script working in your environment, follow these steps:

  1. Download EnumGroups.bat from the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com).
  2. Put EnumGroups.bat in a separate folder, then make a backup copy of the original script. This backup will prove useful if you make a mistake when entering your path locations or when trying to increase the number of enumeration levels.
  3. By default, the script generates the output file to the same folder in which the script resides. If this folder isn't your preference, you can modify the code to use a different folder. The comments in the script detail the changes you need to make and even provide an option for you if you want to create the file in the %Temp% folder.
  4. Set the path leading to the folder that contains the Local, Global, and Choice utilities. In the line
  5. Set Reskit=\\server\reskitShare

    change \\server\reskitShare to your folder's path. Be aware that when you embed the Local and Global commands inside a For command, they're sensitive to spaces in the path.

  6. EnumGroups.bat opens the completed report file in Excel. If you don't have that application installed or you want to use another program to display the completed file, you need to change that section of code. The comments in the script explain how to adapt it.

Now Give It a Try
After you have customized EnumGroups.bat, give the script an initial test. Enter the group name of a moderately complicated group that has several levels of nesting. If the script passes that test, try running it against your toughest group structures. Keep in mind that if you have a complicated structure, the script will take a while to run.

Related Content:

ARTICLE TOOLS

Comments
  • wangbin42
    4 years ago
    Sep 21, 2008

    DAD

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.