I need to write an easily configurable script that obtains PCs' media access control (MAC) addresses. Sometimes the script will need to target the PCs in the top level of an organizational unit (OU). Other times the script will need to target all the PCs in the lower levels of an OU. Some PCs have more that one NIC and these additional MACs need to be captured as well. Do you have any suggestions on how I can write this script?
I recommend that you use Getmac to handle this task. This tool is part of Windows Server 2003. You can also find it in the Microsoft Windows XP Resource Kit and the Microsoft Windows 2000 Server Resource Kit.
The GetMACs.bat script demonstrates how you can use the Getmac utility to capture the data you need. Getmac's output contains a number of extraneous lines, so this script uses a chain of Find commands with the /V switch to filter out those lines. The script also uses the Dsquery utility to get the list of node names. Dsquery is part of Windows 2003. You can find it in the \%SystemRoot%\System32 folder.
You can download GetMACs.bat from the Windows Scripting Solutions Web site. Go to http://www.windowsitpro.com/windowsscripting, enter 45509 in the InstantDoc ID text box, then click the 45509.zip hotlink.
To launch this script, you use the command
However, before you use it, you need to configure several variables. As Listing 2 shows, you first need to configure the target OU. Be sure to enclose the OU name in quotes. Next, you need to configure the target levels in the OU. You have two options: the OU's top level (set Scope to onelevel) or the OU's lower levels (set Scope to subtree). You then need to set the pathname to the output file. Once again, enclose the pathname in quotes. Finally, you need to configure the Universal Naming Convention (UNC) paths to the Dsquery and Getmac utilities unless these are installed locally on the node.
LISTING 2: Code to Configure in GetMACs.bat :: BEGIN COMMENT :: Set OU target. Enclose in quotes. :: END COMMENT Set OUtarget= "OU=Sales,OU=Workstations,DC=MYCOMPANY,DC=COM" :: BEGIN COMMENT :: Configure the scope (i.e., onelevel or subtree). :: END COMMENT Set Scope=subtree :: BEGIN COMMENT :: Configure the output file location. Enclose in quotes. :: END COMMENT Set Outputfile="C:\MacReports\SalesReport.txt" :: BEGIN COMMENT :: Configure the location of the Dsquery utility. :: END COMMENT Set Dsloc=\\Server2\reskit\dsquery.exe :: BEGIN COMMENT :: Configure the location of the Getmac utility. :: END COMMENT Set GMACloc=\\Server2\reskit\GETMAC.EXE