Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to change PC name 3

Status
Not open for further replies.

chrisosullivan1

IS-IT--Management
Aug 4, 2004
50
CA
Is it possible to write a script to change a computer's name in XP PRO?
 
Copy and paste the following into NotePad and save a CompName.vbs (or any name so long as it ends .vbs).

Code:
sNewName = inputbox("Enter New Computer Name")

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

MsgBox "Computer name changed, please reboot your computer"

Greg Palmer
Free Software for Adminstrators
 
I have changed the code slightly so that it will show you the existing Computer Name.

Code:
with Wscript.CreateObject("Wscript.Network") 
     sNewName = inputbox("Enter New Computer Name","Change Computer Name", .Computername)
end with

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

MsgBox "Computer name changed, please reboot your computer"

Greg Palmer
Free Software for Adminstrators
 
Your program works well, but when it asks me to restart, it does not connect to the domain. Maybe your code does sync up with the server?
let me know
 
by renaming the computer, do you have to run netdom.exe to make sure it gets rejoined to the same domain ?

XM
 
Apologies if this is slightly off-topic but we found that the computername was stored in more places in the registry than we expected.

We use Ghost (but not SysPrep) to clone all our XP PC's so, after cloning, needed a way to individualize them.

I cobbled together a VBS script (uses WMI and netdom.exe) that changes computername, IP settings, workgroup, removes the user account from the 'Administrators' group, sets up AutoLogon (we're in a Novell server environment) then re-boots automatically so the PC can be used by the user immediately.

It's not the best example of scripting by any means but it does the job and is fairly-well documented.

It's a bit long so I haven't included it here. Let me know if it would be of any help and I'll post it.
 
yes..I'd appreciate it if you can help. I have a similar situation. I have XP with a NT4 domain. All I need to do is individualize the computer name after it boots up for all my PC's in my labs.
much appreciated
 
The script's below. Delete or comment out any section or lines that you don't need, e.g. 106-117 (which refer to a default HP printer). If you're in a Domain instead of a Workgroup then line 132 will need amending.

Copy and paste the following into Notepad, save as .vbs:-

'==============================================================================='
' Script name : xp-clone-changer-v04.vbe
' Creation Date : 29-10-2003
' OS : Windows XP
' Amendments : See below
' Added auto-capitalization of Asset no. and Workgroup (26-01-2004)
' Added changes to Computername in printer section of registry (26-01-2004)
' Added check for installed memory (03-07-2004)
'
' PURPOSE
' This script carries out the following function(s):-
' 1) Changes the IP address, subnet mask and gateway.
' 2) Changes the Autologon to the 'Windows User' account.
' 3) Changes the Computername and Workgroup.
' 4) Removes the 'Windows User' account from the 'Administrators' group.
' 5) Deletes the desktop shortcut to 'Clone Changer'.
' 6) Shows amount of installed RAM.
' 7) Re-boots the PC so the changes are carried out.
'
'==============================================================================='
'**Start Encode**
Option Explicit 'Enforce strict naming
Dim WSH, FSO
Dim objComputer, objWMIService, objOperatingSystem, objNetAdapter
Dim strTitle, strVersion, strThisPC, strComputername, strCompNameRegPath, strWorkgroup
Dim strCCS, strTcpipParamsRegPath, strIPAddress, strSubnetMask, strGateway, strGatewayMetric
Dim colMEM, colNetAdapters, colOperatingSystems
Dim errEnable, errGateways

'Setup scripting environment
Set WSH = CreateObject ("WSCript.shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set Version info
strVersion = "v04"

'Set target PC for WMI
strThisPC = "."

'Set the Title for any dialogs
strTitle = "XP Clone Changer " & strVersion

'------------------------------------------------------------------------
'Section 1 - Use WMI to change IP address, subnet mask and gateway
WSH.Popup "Preparing to change IP address, subnet mask and gateway... Please wait.",2, strTitle 'Inform user

'Setup WMI
Set objWMIService = GetObject("winmgmts:\\" & strThisPC & "\root\cimv2")

'Set specific WMI query so 1394 adapter is excluded
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

'Get user input into an array
strIPAddress = Array(InputBox("Enter the new IP address"))
strSubnetMask = Array(InputBox("Enter the new Subnet mask"))
strGateway = Array(InputBox("Enter the new Gateway address"))

'Inform user
WSH.Popup "Please be patient whilst changes are made. This could take 10 seconds or more.",3, strTitle 'Inform user
strGatewayMetric = Array(1)

'Make the changes
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WSH.Popup "The IP address has been changed.",2,strTitle 'Inform user of success
Else
WSH.Popup "The IP address could not be changed automatically." & vbCrLf & vbCrLf &_
"This may be because the PC/Laptop is not connected to an active network." & vbCrLf & vbCrLf &_
"Please check the IP settings manually afterwards.",2,strTitle 'Inform user of failure
End If
Next
'------------------------------------------------------------------------
'Section 2 - Change the Autologon to 'Windows User' account
WSH.Popup "Preparing to change AutoLogon to Windows User account... Please wait.",2, strTitle

'Change details in the Registry
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", "Windows User"
WSH.Popup "OK... Done!",2, strTitle 'Inform user
'------------------------------------------------------------------------
'Section 3 - Change Computername and Workgroup
WSH.Popup "Preparing to change Computername.",2, strTitle 'Inform user

'Get user input
strComputername = InputBox("Enter the asset no. to use as the Computername, e.g A051468")
strComputername = UCase(strComputername) 'Force capitalization
WSH.Popup "Please wait.",2, strTitle 'Inform user

'Computername
strCCS = "HKLM\SYSTEM\CurrentControlSet\"
strTcpipParamsRegPath = strCCS & "Services\Tcpip\Parameters\"
strCompNameRegPath = strCCS & "Control\Computername\"

'Delete current settings in registry and write new ones
With WSH
.RegDelete strTcpipParamsRegPath & "Hostname"
.RegDelete strTcpipParamsRegPath & "NV Hostname"

.RegWrite strCompNameRegPath & "Computername\Computername", strComputername
.RegWrite strCompNameRegPath & "ActiveComputername\Computername", strComputername
.RegWrite strTcpipParamsRegPath & "Hostname", strComputername
.RegWrite strTcpipParamsRegPath & "NV Hostname", strComputername
End With ' WSH
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AltDefaultDomainName", strComputername
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", strComputername
WSH.RegWrite "HKCU\Software\Microsoft\Windows\ShellNoRoam\", strComputername
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\serverName", LCase(strComputername)
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\shortServerName", strComputername
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\uNCName", "\\" & LCase(strComputername) & "\HP LaserJet 4000 Series PCL"
WSH.RegWrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\url", " & LCase(strComputername) & "/"

WSH.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\serverName", LCase(strComputername)
WSH.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\shortServerName", strComputername
WSH.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\uNCName", "\\" & LCase(strComputername) & "\HP LaserJet 4000 Series PCL"
WSH.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\HP LaserJet 4000 Series PCL\DsSpooler\url", " & LCase(strComputername) & "/"

WSH.RegWrite "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows Media\WMSDK\General\Computername", strComputername

WSH.Popup "OK... Done!",2, strTitle 'Inform user

'Workgroup
WSH.Popup "Preparing to change Workgroup.",2, strTitle 'Inform user

'Get user input
strWorkgroup = InputBox("Enter the name to use as the Workgroup, e.g SS38COL")
strWorkgroup = UCase(strWorkgroup) 'Force capitalization
WSH.Popup "Please wait.",2, strTitle 'Inform user

'Use Win2k resource kit utility to make change
WSH.Run "NETDOM.EXE MEMBER \\" & strComputername & " /JOINWorkgroup " & strWorkgroup, 0, True
WSH.Popup "OK... Done!",2, strTitle 'Inform user

'------------------------------------------------------------------------
'Section 4 - Remove Windows User account from Administrators group
WSH.Popup "Preparing to remove Windows User account from Administrators group.",2, strTitle 'Inform user

'Use commandline to carry out removal forcibly in hidden mode
WSH.Run "cmd /c NET LOCALGROUP Administrators " & """Windows User""" & " /DELETE", 2, True
WSH.Popup "OK... Done!",2, strTitle 'Inform user

'------------------------------------------------------------------------
'Section 5 - Delete desktop shortcut
FSO.DeleteFile("C:\Documents and Settings\All Users\Desktop\Clone Changer.lnk"),True

'------------------------------------------------------------------------
'Section 6 - Show amount of installed RAM
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strThisPC & "\root\cimv2")
Set colMEM = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer In colMEM
objComputer.TotalPhysicalMemory = Left((objComputer.TotalPhysicalMemory/1000000),3)
If objComputer.TotalPhysicalMemory < 512 Then
WSH.Popup "Installed memory:" & vbTab & (objComputer.TotalPhysicalMemory) & "Mb"_
& VbCrLf & VbCrLf & "Warning: This PC has less than 512 Mb of memory installed."_
& VbCrLf & "The swap size is set incorrectly and performance will suffer."_
& VbCrLf & VbCrLf & "Please install more memory ASAP." &_
VbCrLf & VbCrLf & "Click on the OK button.", 0, strTitle
Else
WSH.Popup "Installed memory:" & vbTab & (objComputer.TotalPhysicalMemory) & "Mb"_
& VbCrLf & VbCrLf & "Click on the OK button.", 0, strTitle
End If
Next

'------------------------------------------------------------------------
'Section 7 - Use WMI to re-boot PC
WSH.Popup "Re-booting the PC so changes take effect. Please wait...",2, strTitle 'Inform user
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" & strThisPC & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Reboot()
Next

'------------------------------------------------------------------------
'Finish up
Set WSH = Nothing
Set FSO = Nothing
Set objWMIService = Nothing
Set colNetAdapters = Nothing
Set colMEM = Nothing
WScript.Quit 'Yay!
 
A star to you from me.
I am still baffled as to why netdom.exe is not easier as just a commnand line (my original post above), but one thing I have come to admire a lot is a coder that includes lots of notes. This is a very professional piece of work, and thank you.

Bill Castner


 
I realize that this thread is about a scripting solution but I wanted to mention a tool for making a cloned computer unique.

With the Ghost software package there is a small program called GhostWalk (ghstwalk.exe) ... this program takes an existing install of XP (or any NT based OS) and gives it a new SID. There is also an option to change the name of the machine at the same time (although it has to have the same number of characters).

We regularly use this on cloned machines that have not even been Sysprep'd and have had no problems with conflicts.

I'm not sure what command line options this program has but it may be usefull if called from a script with the right switches.
 
Bill,

Thanks for your thanks and star.

I have 3 F/T non-IT-qualified staff to implement/support 1200+ (rising daily) Win XP and Win 98 SE desktops PC's and 100+ laptops, primarily using Novell file servers, but always in a cash-strapped British local authority environment. As a result, I use the cheapest (i.e. free) methods available. This means scripting and automating a lot.

I had to ignore DOS-looking commandlines 'cos I wanted a GUI-based environment whereby new XP PC's were first cloned by our hardware supplier (based on a Ghost disk image I supplied) then delivered to desktops to be 'individualized' by anyone (e.g. local Admin staff without any IT knowledge and/or 'Administrative' privileges) without having to wait for 1 of my 3 staff to turm up.

It was important that neither the hardware supplier nor the local 'PC installer' ever learned the 'Administrator' password for any of our XP PC's due to confidentiality of client info.

Only my 'IT Support Staff' have un-restricted access both before and after my 'XP Clone Changer' script is run.

What I aimed for (and believe I've achieved) is that local Admin staff can take a newly-supplied PC out of its box, set up printing and configure it for our networks with little or no knowledge of what's going on in the background.

Once the new box has been configured then user rights are re-set (via my 'Clone Changer' script) to 'Power Users' only.

Hope this explains...

(PS - Perhaps I should mention... like gpalmer711 and linney, I'm a Brit too. <grin>)

Regards,

Rick
 
I have no problem with Brits, love Aussies such as linney, but have a problem until New Zealand returns the America Cup it stole.

There is on Tek-Tips a UK Forum, that is funny, unlesss uproariously like me you are US born and bred. In which case it makes no sense at all.

I irregularly travel to London, but I will add you to my list of Tek-Tips folks to visit while there. Several UK members have promised a "bust-up" if I visit, and as a guess this is not something Claridge's wants to hear about.

If and when I get there, I will send you a note.

Bill



 
I am also giving Rick998 a star, as it is a very good piece of code....
 
OzCDN - I tried using SysInternal's freeware 'NewSID.exe' ( at the end of 'Clone Changer' but found a side-effect was to make several un-wanted changes to the desktop like putting default shortcuts back on.

As a result I stopped using NewSID and have shied away from using any other SID changer since. We buy a Ghost licence for all our PC's so I'll have a look at GhostWalk to see whether it has the same side-effects. Thanks.

tfg13 - Thanks for the star.

Rick
 
Rick,

Post a note at the sysinternals site.
I am sure Mark will get back to you.
 
Bill,

This is so slap-head obvious I don't know why I didn't think of it before. I just gave up and moved on quickly. Thanks for the suggestion.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top