Solved

rename computer from serial number

Posted on 2013-01-08
13
1,133 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
Comment Utility
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
Comment Utility
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 5

Author Comment

by:Sid_F
Comment Utility
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
 
LVL 5

Author Comment

by:Sid_F
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 35

Expert Comment

by:Ernie Beek
Comment Utility
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
Comment Utility
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
Comment Utility
the code is wrong! the one attached here is the one i think will work...
serial.vbs
0
 
LVL 5

Author Comment

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

Expert Comment

by:bmarone
Comment Utility
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 5

Author Comment

by:Sid_F
Comment Utility
Tim_L that script worked
0
 
LVL 3

Expert Comment

by:Tm-L
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Even if you have implemented a Mobile Device Management solution company wide, it is a good idea to make sure you are taking into account all of the major risks to your electronic protected health information (ePHI).
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now