Link to home
Start Free TrialLog in
Avatar of Sid_F
Sid_F

asked on

rename computer from serial number

I want to rename a computer with its serial number using a script. The machine is on a workgroup. I was thinking of putting something to run at logon and then deleting itself in some way when the rename is done. Its windows XP. Most of the scripts I have seen so far include username password etc for domains but this is only on a workgroup. I don't have much experience of vbscript so I would need a step by step script
Avatar of Ernie Beek
Ernie Beek
Flag of Netherlands image

Perhaps through setting a registry key?

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]
"ComputerName" = "YourName"
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
"NV Hostname" = "YourName"
I have coded and tested this and it is renaming the computers I have run it on. i would run it at login with a Local GPO. let me know if you need any more help, here is the VBS:
Set oShell = CreateObject( "WScript.Shell" )
Dim strComputer
Dim strSerial
strComputer = oShell.ExpandEnvironmentStrings("%ComputerName%")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & 

"\root\cimv2") 
Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure") 
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
For Each objSMBIOS in colSMBIOS
strSerial = objSMBIOS.SerialNumber
Next
err = objComputer.Rename(strSerial)
Next

Open in new window


This will pick up the serial number from the PC and then rename the pc with the only this left to do is reboot which we could add to the code or you could just wait for the end of the day? would really be up to you.
Avatar of Sid_F
Sid_F

ASKER

When I take the above text what should I save the file as can I set it to run at startup? as I presume this may cause an issue when the machine does the reboot as it will try to run the same script again. The machines are workgroup
Avatar of Sid_F

ASKER

Erniebeek I copied the registry text into a notepad file and saved it as .reg and ran it, it said it imported but the name did not change
Hi,

the script must be saved as filename.vbs

If you want to run the script only onces, then copy it somewhere to the client and add the following to the registry (below code is a .reg file):
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
"vbs-execution"="c:\\FILELOCATION\\filename.vbs"

Open in new window

thats a very valid point, i will take a look at the code in a moment to change it to do a check to if it has run before.

when you save it make sure you save it as "filename.VBS" i will upload the next version in file form as well as show you the code in the comment so you could just download the file.

even though the machines are workgrouped you should still be able to access the local group policies for that machine and add the script to execute at login.

bring up run ( windows key + r) and type in "gpedit.msc" you should then get this:
User generated image
from here you want to navigate from the left panel: user configuration/windows settings/scripts(logon log off). and then on the right hand panel you can double click the log on item and specify a scipt to run when a user logs on, this will be the script that i am about to tweak for you, so you would reference that script here for all work grouped machines and then it will run when a user logs on and should do what you want!User generated image
Mmm, I noticed there are some spaces in the text that shouldn't be there. Might want to check that and try again.
Let me check, I found another one in one of my XP machines:

Windows Registry Editor Version 5.0

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName]
"ComputerName"="NEWNAME"
Ok, so i have re worked the code and this does seem to be working for me, as mentioned there were spaces where there shouldn't've been in my post up there, im not sure why i will try to put the code in this post but if that doesnt work i have attached the file to this post as well so you can just download it as a file ready to go.

if you then add it to logon as shown in one of my above posts then everything should work.

the code runs like this:
checks for file created once the code is processed, if this file is there then the script is stopped as it must have been run before, if the file is not there then:
collect the serial from the computer then
rename the computer to this serial number then
create file to stop future execution of script
end script.

let me know how it goes this time!

const sFName = "PrintersInst.log"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshShell = WScript.CreateObject("WScript.Shell")
sAppFolder = oWshShell.Environment("PROCESS")("AppData") & "\"
If Not oFSO.FileExists(sAppFolder & sFName) Then
Set oFile = oFSO.CreateTextFile(sAppFolder & sFName, True)
oFile.Close
Set oShell = CreateObject( "WScript.Shell" )
Dim strComputer
Dim strSerial
strComputer = oShell.ExpandEnvironmentStrings("%ComputerName%")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure") 
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
For Each objSMBIOS in colSMBIOS
strSerial = objSMBIOS.SerialNumber
Next
err = objComputer.Rename(strSerial)
Next
End If

Open in new window

serial.vbs
ASKER CERTIFIED SOLUTION
Avatar of Tm-L
Tm-L
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sid_F

ASKER

Do I need to rename this script or do I leave the name as serial.vbs
Does it matter if it runs multiple times, aside from the need to reboot?  Maybe instead of dropping a file to check, just compare the current computername to the serial to see if it's already been set.

In line 12, I believe you can use "." as the computername for the local machine, such as
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Open in new window


Here's an alternate means, by setting the values directly in registry.
' Fetch Computername in Windows Script
Set wshNetwork = WScript.CreateObject( "WScript.Network" )
sComputerName = wshNetwork.ComputerName

' Get Serial Number from WMI
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colSMBIOS = objWMIService.ExecQuery ("SELECT * FROM Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
 sAsset= objSMBIOS.SerialNumber
Next

' Change Hostname in Registry
sNewName=sAsset
Script.Echo ("Changing ComputerName "+sComputerName+" to "+sAsset)
Set oShell = CreateObject ("WSCript.shell" ) 
sCCS = "HKLM\SYSTEM\CurrentControlSet\" 
sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\" 
sCompNameRegPath = sCCS & "Control\ComputerName\" 
With oShell 
.RegDelete sTcpipParamsRegPath & "Hostname" 
.RegDelete sTcpipParamsRegPath & "NV Hostname" 
.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName 
.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName 
.RegWrite sTcpipParamsRegPath & "Hostname", sNewName 
.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName 
End With ' oShell 
WScript.Echo ("Computer name changed, please reboot your computer")

Open in new window

Avatar of Sid_F

ASKER

Tim_L that script worked
you shouldn't have to rename it, and once you have set it up to run at login it will execute each time a user logs on but it check to see if i has run before, if so it stops itself from running.

I'm glad the code worked and you have your solution! :)