Solved

rename computer from serial number

Posted on 2013-01-08
13
1,197 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
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Switch ports not working 8 54
Internet Service Provider 3 55
VPN problems 4 31
Connection Dropouts to Database on Windows Server 2008 R2 DFS 12 23
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

827 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