Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

rename computer from serial number

Posted on 2013-01-08
13
Medium Priority
?
1,321 Views
Last Modified: 2013-01-08
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
0
Comment
Question by:Sid_F
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 35

Expert Comment

by:Ernie Beek
ID: 38753857
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"
0
 
LVL 3

Expert Comment

by:Tm-L
ID: 38753858
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.
0
 
LVL 6

Author Comment

by:Sid_F
ID: 38753912
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Author Comment

by:Sid_F
ID: 38753936
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
0
 
LVL 2

Expert Comment

by:thombeck
ID: 38753944
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

0
 
LVL 3

Expert Comment

by:Tm-L
ID: 38753945
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:
GPedit.msc dialog
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!gpedit dialog local login script
0
 
LVL 35

Expert Comment

by:Ernie Beek
ID: 38753946
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"
0
 
LVL 3

Expert Comment

by:Tm-L
ID: 38753995
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
0
 
LVL 3

Accepted Solution

by:
Tm-L earned 2000 total points
ID: 38754154
the code is wrong! the one attached here is the one i think will work...
serial.vbs
0
 
LVL 6

Author Comment

by:Sid_F
ID: 38754158
Do I need to rename this script or do I leave the name as serial.vbs
0
 
LVL 1

Expert Comment

by:bmarone
ID: 38754159
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

0
 
LVL 6

Author Comment

by:Sid_F
ID: 38754208
Tim_L that script worked
0
 
LVL 3

Expert Comment

by:Tm-L
ID: 38754233
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! :)
0

Featured Post

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question