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.
- Retrieves the MAC address using WMIC
- Compares that MAC address to the pdqmac2pcname.txt file for a MAC address match or mismatch.
- When the MAC address matches, the name is piped to the netdom statement as the newname variable %strComputername%
- then the script will goto insert_name and
- 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.
- If name is not found or MAC Address is not matched, the computer will not join the domain and
- 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