0Batch script to rename PC based on reserved DHCP name

I use WDS on a stan­dalone serv­er to set up new PCs and to reim­age PCs when they have ended up mangled bey­ond repair. With a good num­ber of PCs in my own house it is use­ful to reim­age them oca­sion­ally when swap­ping or upgrad­ing hard­ware. Most of this pro­cess is now auto­mated by an unattend.xml file but one step the unat­ten­ded pro­cess does­n’t seem to sup­port is renam­ing PCs back to their ori­gin­al name.

All of my PCs have stat­ic DHCP reser­va­tions with their cor­rect host­name (which is also registered in DNS) so I figured there must be a way to pull this inform­a­tion and use it to rename them. After much fid­dling about I have pro­duced a batch script that will do this (when run as admin).

Simple paste the fol­low­ing code into a .cmd file and run it as part of your unat­ten­ded pro­cess (I describe mine below)

@echo off
set ip_address_string="IPv4 Address"
FOR /F "usebackq" %%i IN (`hostname`) DO SET MYVAR=%%i
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
    for /f "usebackq tokens=2 delims=:" %%g in (`nslookup %%f ^| findstr /c:Name`) do (
        for /F "tokens=1 delims=." %%a in ("%%g") do (
            for /F "tokens=1 delims= " %%b in ("%%a") do (
                WMIC ComputerSystem where Name="%myvar%" call Rename Name="%%b"
                goto :eof

I call my script from the <FirstLogonCommands> sec­tion of ImageUnattend.xml.

<SynchronousCommand wcm:action="add">
  <Description>Rename system</Description>
  <CommandLine>c:\extras\elevate -c c:\extras\rename-pc-from-dns.cmd</CommandLine>

To run it as admin I use a little util­ity (called elevate.exe) that does this (com­mands run dur­ing setup can elev­ate without pop­ping up a prompt!). You can get it from it’s ori­gin­al source

The final piece of the puzzle for my setup is that I modi­fy the win­dows install image to include an exe file and a little poweer­shell script — these allow the copy­ing of all the oth­er installers from the net­work. The exe file is ‘streams’ which is a sys­in­tern­als (now part of Microsoft) util­ity to remove the “this file came from the inter­net are you sure you want to run it” warning.

I also have the fol­low­ing script as a .ps1 file

copy-item -Path \\wds-server\RemInst\Custom\*.* -Destination C:\Extras

My first 4 Logon­Com­mands are then as follows…

<SynchronousCommand wcm:action="add">
  <Description>No standby during setup</Description>
  <CommandLine>powercfg -change standby-timeout-ac 0</CommandLine>
<SynchronousCommand wcm:action="add">
  <Description>Add credentials for access to server via network</Description>
  <CommandLine>cmdkey /add:wds-server /user:wds-server\administrator /pass:password123</CommandLine>
<SynchronousCommand wcm:action="add">
  <Description>Copy scripts and apps from network to extras folder</Description>
  <CommandLine>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -ExecutionPolicy ByPass -File C:\Extras\copyapps.ps1</CommandLine>
<SynchronousCommand wcm:action="add">
  <Description>Remove internet warning from copied apps</Description>
  <CommandLine>C:\Extras\Streams64.exe -d -d -nobanner C:\Extras\*.* /accepteula</CommandLine>

Leave a Reply