Switch characters in MAC address from a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Switch characters in MAC address from a variable

#1 Post by bardman » 22 Mar 2013 12:36

Hi All,

This batch is used with a few other files as start-up scripts as a part of the Symantec Ghost Cast re-imaging process for Windows 7 we are about to undertake. Together they totally automate the entire re-imaging process of Sysprep, renaming the computer from text file, then joining the computer to the domain and placing in the correct OU.
I know there are other programming languages and scripting languages that can perform these same processes far better but no time for learning them at this moment. Sure MSs Power-shell and VBS are better candidates but I am not spent up on those just quite yet, nor are my coworkers.
Other renaming applications works intermittently for me. Compname.exe does not work with Windows 7. Wsname works intermittently or not at all with our some of Dell models 790, Power Edge servers 850, 860, and R200. Our other models do not seem to have any major issues using the third party applications--GX620, 740, 745, 755, 760, and 780.
I prefer to use native applications already within the OS. Obviously netdom.exe is not native (have to obtain files from the RSAT executable) but it is an easy OS import and a must have on the images.
So please, provide any answers with the programs I am currently using (batch, netdom, getmac, and good ole text files).
This batch file accomplishes the following:
1. Uses getmac.exe to find the MAC address of the local machine (being reimaged)
2. Variablizes the local MAC address (%%G)
3. Pipes the variablized MAC address and compares it to getmac2.txt file (%%A)
4. Grabs the associated variable (%%B) and
5. Places the %%B as the strComputername
6. The && statement stops the operation when the MAC address is found instead of parsing the entire file
7. The :insert_name label runs netdom.exe, closes the batch and reboots the computer. The /FORCE switch is used but not listed here while testing. The 'pause' is used only for testing as well.

A subsequent script joins the computer to the domain.

Now, this is what I would like to try...
I would like to be able to modify the %%G variable by replacing the dashes '-' in the MAC address with colons ':'. Getmac.exe produces the MAC address like this 11-22-33-44-55-66. However, another admin program provides me a list with MAC addresses like this 11:22:33:44:55:66. My getmac2.text file lists the MAC addresses with the colons. Obviously, I can automatically remove the colons from a text editor but the intent is to reduce as many steps as possible.
Also piping the %%G variable to another file would not be a problem if it is easier to perform the string replacement.

BELIEVE IT OR NOT, THE SCRIPT DOES WORK PERFECTLY!

getmac2.txt file contents are formatted like this:
11:22:33:44:55:66,88D-350-03-780
11:22:33:44:55:77,44E-351-13-790
11:22:33:44:55:88,66F-352-23-760
11:22:33:44:55:99,22G-353-33-R200
11:22:33:44:55:00,11H-354-43-620

Code: Select all

setlocal enabledelayedexpansion


FOR /F "usebackq delims= " %%G IN (`getmac.exe /FO table /NH`) DO (
        FOR /F "usebackq delims=, tokens=1,2" %%A IN (`type C:\wsname\getmac2.txt`) DO if %%G EQU %%A SET strComputername=%%B && goto insert_name
)   

:insert_name
C:\windows\system32\netdom.exe renamecomputer %COMPUTERNAME% /NewName:%strComputername%

pause


Any help is greatly appreciated.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Switch characters in MAC address from a variable

#2 Post by mfm4aa » 22 Mar 2013 14:16

Here we go:

Code: Select all

setlocal enabledelayedexpansion


FOR /F "usebackq delims= " %%G IN (`getmac /FO table /NH`) DO (
      set "MAC=%%G"
      set "MAC=!MAC:-=:!"
        FOR /F "usebackq delims=, tokens=1,2" %%A IN (`type C:\wsname\getmac2.txt`) DO if "!MAC!" EQU "%%A" SET strComputername=%%B && goto insert_name
)   

:insert_name
C:\windows\system32\netdom.exe renamecomputer %COMPUTERNAME% /NewName:%strComputername%

pause

bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Re: Switch characters in MAC address from a variable

#3 Post by bardman » 23 Mar 2013 16:26

Thank you soo very much mfm4aa! I would have never figured this out. :D

Post Reply