The primary way to track a script’s effectiveness is to use logs, counters, or a combination thereof, as the script PopulateGroups.bat does. If a script performs numerous functions or performs a function numerous times, a log might become so large that it makes reviewing the script’s successes and failures difficult. The use of counters makes it easier to track how well a script performed its job.
To make a counter work in a script, you need to include code that isolates, initializes, and increments the counter. You also need to include code that informs you about the counter’s resulting value.
Isolate the counter. When using a counter, you need to use the paired Setlocal and Endlocal commands at the beginning and end of the script, respectively. These commands prevent one script run’s counter value from affecting another script run’s counter value.
Initialize the counter. Before you use a counter, you must use the Set command to initialize the counter with a starting value. Typically, that value is 0. For example, to initialize a counter called CounterA to 0, you use the code
Set /a CounterA=0
Increment the counter. After initializing the counter, you need to increment it. Typically, you increment a counter by a value of 1. For example, to increment CounterA by 1, you use the code
Set /a CounterA+=1
Each time the script executes this line of code, the counter’s value increases by 1.
Inform you about the result. Near the end of a script run, you will want to output the resulting counter value to the console screen, a log, or another venue. For example, to output the CounterA’s value to the console screen, you use the Echo command
To output CounterA’s value to a log, you use the Echo command and redirect the output to the log with code such as
To output CounterA’s value to another computer, you can use the Net Send command with code such as
Net Send %Computername% the value of CounterA is: %CounterA%
PopulateGroups.bat uses five counters to track the successes and failures of key items. Those counters are
- Leftcntr—tracks the number of PCs that were off during the script’s run
- Addcntr—tracks the number of groups successfully added
- Alreadycntr—tracks the number of groups that were already in place
- Logoncntr—tracks the number of logon errors
- Errorcntr—tracks the number of errors encountered when the script tried to add the group