0

Here is what I am trying to accomplish...if this can be accomplished with only batch...for now?

Instead of actually renaming the client here from the netdom statement, I want to INSERT the %strComputer% variable into an xml file or at least replace the text with that same variable. The xml file will be an unattend.xml file used for SysPrepping a Windows 7 client.

This is the section I would like to modify:

%strComputername%

In essence, the insert_name statement would be for the new name to be inserted into the xml file vice the netdom.exe command.

The posted code works pretty darn good in my autorenaming process! The steps outlines how the script works.

Please pay attention to the for /f statements and the :insert_name label. Everything else can be disregarded.

  1. Retrieves the MAC address using WMIC
  2. Compares that MAC address to the pdqmac2pcname.txt file for a MAC address match or mismatch.
  3. When the MAC address matches, the name is piped to the netdom statement as the newname variable %strComputername%
  4. then the script will goto insert_name and
  5. If MAC address is found with a computer name, the script goes to the label kill_autologon to force computer to not autologon. This procedure prevents an improperly renamed computer from being joined to the domain under a SysPreped randomly generated name.
  6. If name is not found or MAC Address is not matched, the computer will not join the domain and
  7. computer reboots to login screen so that it can be manually renamed and joined to the domain.

setlocal enabledelayedexpansion


FOR /F "skip=1 tokens=*" %%I IN ('WMIC Path Win32_NetworkAdapter Where "NetConnectionID='Local Area Connection'" Get MACAddress') DO IF NOT DEFINED MACAddress SET MACAddress=%%I
    FOR /F "usebackq delims=, tokens=1,2" %%J IN (`type C:\rename_computer\pdqmac2pcname.txt`) DO IF %MACAddress% EQU %%J SET strComputername=%%K && GOTO insert_name
)

goto kill_autologon


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

goto shutdown


:kill_autologon
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /f

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f


:shutdown
shutdown -r -t 0


exit

1 Answer 1

0

First, enabledelayedexpansion is done with !, not with % So !var! instead of %var%

If that XML is known to be consistent all you have to do is split it and join it later with the missing bits:

xml
blah blah
strComputername
blah blah
/xml

Split it like this:

file1:
xml
blah blahEOL

file3:
blah blah
/xml

not put strComputername in file2 and do a binary merge: copy /b file1+file2+file3 unattend.xml

Note that each file will have an EOL, basically in file2 you'll have to put the entire xml line that contains strComputername.

Doable?

4
  • Thanks for the quick response emm980. The enabledelayedexpansion is smart enough to work with ! and % so not a real issue. This works perfectly. I had a similar example with XP sysprep.inf file. For some weird reason, the new computer name has a space at the end of it? Do you know how I can remove this space? codeEcho Computer Name should be: %strComputername% ECHO Now editing unattend2.xml... ECHO ^<ComputerName^>%strComputername%^</ComputerName^>%>>testfile2.txt COPY /b testfile.txt+testfile2.txt+testfile3.txt unattend2.xml ECHO ComputerName section completed...code
    – Terrence
    Apr 28, 2013 at 5:06
  • In this case it's true, but it's not one and the same thing... do a for loop, add to the var the counter and do an echo as well with % and ! inside that loop and then one outside and notice the difference. It's a good programming practice to stick to the particularities. ----> %%K or strComputername comes from pdqmac2pcname.txt, can you edit in that file without consequences?
    – JasonXA
    Apr 28, 2013 at 5:10
  • Also if pdqmac2pcname.txt contains only two rows of data, MAC and NAME, just use tokens=1,* for ease of use with names with spaces in them.
    – JasonXA
    Apr 28, 2013 at 5:16
  • Yes I can edit the pdqmac2name.txt file without any issues. I see your point on the ! and % issues. I did substitute them and got different output for the echo statements but the name did output to the xml correctly for both. In this case, the % shows the name for each echo statement whereas the ! echo statements does not. The pdqmac2name.txt is formatted like this: 00:11:22:33:44:55,new-puter-name with several rows. Yes I will make this change--tokens=1,*, though we should not have any computer names with any spaces in them. I am not exactly clear on the use of EOL?
    – Terrence
    Apr 28, 2013 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .