Solved How to Gather, Truncate and Set Serial Number as Variable

May 9, 2016 at 12:49:40
Specs: Windows 7
I am trying to write a batch file that will accomplish the following:

1 - Gather PC Serial Number.
2 - Truncate Serial Number to be last 4 of it only. So if it is E73H47S2 I want it to end up as 47S2.
3 - Assign those truncates results to a variable.
4 - Use netdom to rename computer to STATIC-LAST4OFSERIAL.

This needs to work in Windows 7. I can pull the serial, but everytime I try to truncate using :~4% at the end of the variable, all I get for output is :~4% and not the actual truncated serial.

Can someone help me with this?


See More: How to Gather, Truncate and Set Serial Number as Variable

Report •


✔ Best Answer
May 9, 2016 at 17:05:40
Unable to replicate:
E:\>type cn.cmd
@echo off
for /F %%a in ('wmic bios get serialnumber') do call :TRUNCATESERIAL %%a
echo The serial number is %SerialNumber%
echo The last four of serial number is %SerialLastFour%
goto :EOF

:TRUNCATESERIAL
if not "%*"=="" set SerialNumber=%*
set SerialLastFour=%SerialNumber:~-4%
goto :EOF
E:\>cn
The serial number is System
The last four of serial number is stem

So here's a quick VBScript that should do what you're asking for, assuming you're on a domain:

Const user = "userWithRenamePowersInDomain"
Const pass = "somePass"
Set wmi = GetObject("winmgmts:")
For Each b In wmi.InstancesOf("Win32_BIOS")
  serial = b.SerialNumber
  newName = "CCTYY-" & Right(serial, 4)
Next 'b

For Each c In wmi.InstancesOf("Win32_ComputerSystem")
  ret = c.Rename(newName, user, pass)
Next 'c

WScript.Echo "Serial number: " & serial & vbNewLine _
 & "New name: " & newName & vbNewLine _
 & "Rename request returned: " & ret

'Uncomment the following line to reboot after the rename
'If Not ret Then GetObject("winmgmts:{(Shutdown)}Win32_OperatingSystem=@").Reboot

How To Ask Questions The Smart Way



#1
May 9, 2016 at 13:47:51

Report •

#2
May 9, 2016 at 14:51:49
I got the truncation working. However, now I am having problems with the netdom portion. It does not work for some reason.

@echo off
set SOURCE=\\VMSCCM\SOFTWARE\Computer_Rename
set TEMP1=c:\TSS_TEMP\Computer_Rename
call :COPYFILES
for /F %%a in ('wmic bios get serialnumber') do call :TRUNCATESERIAL %%a
echo The serial number is %SerialNumber%
echo The last four of serial number is %SerialLastFour%
echo The new hostname of this computer will be CCTYY-%SerialLastFour%
goto :EOF

:COPYFILES
echo Copying Netdom.exe to local PC
ROBOCOPY %SOURCE% %TEMP% *.* /e
goto EOF

:TRUNCATESERIAL
if not "%*"=="" set SerialNumber=%*
set SerialLastFour=%SerialNumber:~-4%
goto EOF

:RenameComp
CD C:\TSS_TEMP\Computer_Rename
netdom.exe renamecomputer %COMPUTERNAME% /newname:CCTYY-%SerialLastFour% /force
goto EOF

:EOF


Report •

#3
May 9, 2016 at 15:12:13
1. Are you running the batch file as Administrator?

2. You need to specify the /userd: (and possibly /passwordd) parameter for the netdom command.

https://technet.microsoft.com/en-us...


Report •

Related Solutions

#4
May 9, 2016 at 17:05:40
✔ Best Answer
Unable to replicate:
E:\>type cn.cmd
@echo off
for /F %%a in ('wmic bios get serialnumber') do call :TRUNCATESERIAL %%a
echo The serial number is %SerialNumber%
echo The last four of serial number is %SerialLastFour%
goto :EOF

:TRUNCATESERIAL
if not "%*"=="" set SerialNumber=%*
set SerialLastFour=%SerialNumber:~-4%
goto :EOF
E:\>cn
The serial number is System
The last four of serial number is stem

So here's a quick VBScript that should do what you're asking for, assuming you're on a domain:

Const user = "userWithRenamePowersInDomain"
Const pass = "somePass"
Set wmi = GetObject("winmgmts:")
For Each b In wmi.InstancesOf("Win32_BIOS")
  serial = b.SerialNumber
  newName = "CCTYY-" & Right(serial, 4)
Next 'b

For Each c In wmi.InstancesOf("Win32_ComputerSystem")
  ret = c.Rename(newName, user, pass)
Next 'c

WScript.Echo "Serial number: " & serial & vbNewLine _
 & "New name: " & newName & vbNewLine _
 & "Rename request returned: " & ret

'Uncomment the following line to reboot after the rename
'If Not ret Then GetObject("winmgmts:{(Shutdown)}Win32_OperatingSystem=@").Reboot

How To Ask Questions The Smart Way


Report •


Ask Question