0Batch script to rename PC based on reserved DHCP name

I use WDS on a standalone server to set up new PCs and to reimage PCs when they have ended up mangled beyond repair. With a good number of PCs in my own house it is useful to reimage them ocasionally when swapping or upgrading hardware. Most of this process is now automated by an unattend.xml file but one step the unattended process doesn’t seem to support is renaming PCs back to their original name.

All of my PCs have static DHCP reservations with their correct hostname (which is also registered in DNS) so I figured there must be a way to pull this information and use it to rename them. After much fiddling about I have produced a batch script that will do this (when run as admin).

Simple paste the following code into a .cmd file and run it as part of your unattended process (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> section of ImageUnattend.xml.

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

To run it as admin I use a little utility (called elevate.exe) that does this (commands run during setup can elevate without popping up a prompt!). You can get it from it’s original source

The final piece of the puzzle for my setup is that I modify the windows install image to include an exe file and a little poweershell script – these allow the copying of all the other installers from the network. The exe file is ‘streams‘ which is a sysinternals (now part of Microsoft) utility to remove the “this file came from the internet are you sure you want to run it” warning.

I also have the following script as a .ps1 file

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

My first 4 LogonCommands are then as follows…

<SynchronousCommand wcm:action="add">
  <Order>1</Order>
  <Description>No standby during setup</Description>
  <CommandLine>powercfg -change standby-timeout-ac 0</CommandLine>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
  <Order>2</Order>
  <Description>Add credentials for access to server via network</Description>
  <CommandLine>cmdkey /add:wds-server /user:wds-server\administrator /pass:password123</CommandLine>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
  <Order>3</Order>
  <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>
<SynchronousCommand wcm:action="add">
  <Order>4</Order>
  <Description>Remove internet warning from copied apps</Description>
  <CommandLine>C:\Extras\Streams64.exe -d -d -nobanner C:\Extras\*.* /accepteula</CommandLine>
</SynchronousCommand>

Please send us your thoughts by commenting below! If you would like to subscribe please use the subscribe link on the menu at the top right. You can also share this with your friends by using the social links below. Cheers.

Leave a Reply