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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to change remote computer name

Status
Not open for further replies.

999Dom999

Technical User
Apr 25, 2002
266
GB
Hi I have found this script which works very well it prompts for a name to change the computer name to and also changes the computer account name in Active Directory. I have 100 machines I need to change the name of and would like to either be able run this script and have it change a remote machine or have the script run at startup through Group Policy and have the users username set as their computer name? Anyone able to tweak the following?[/color blue]

Dim RegKeyCompName, RegKeyTCPIP, WSHShell, ComputerName, HostName, DomainName, FQDN, ADRootDSE, ADSysInfo, ADComputerName, ADRenameOK, ADRNewName, vStartRenameCA, NewNAmeU, NewNameL, vStartRenameAD

On Error Resume Next

'###### READ IN EXISTING COMPUTERNAME AND FQDN ######

RegKeyCompName = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\"
RegKeyTCPIP = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\"

Set WSHShell = CreateObject("WScript.Shell")

ComputerName = WSHShell.RegRead (RegKeyCompName & "ComputerName\ComputerName")
Hostname = WSHShell.RegRead (RegKeyTCPIP & "Hostname")
DomainName = WSHShell.RegRead (RegKeyTCPIP & "Domain")
FQDN = HostName & "." & DomainName

Set ADRootDSE = GetObject("LDAP://RootDSE")
If Err.Number <> 0 then
ADComputerName = "Unable to determine this information"
ADOU = "Unable to determine this information"
ADRenameOK = "0"
else
Set ADSysInfo = CreateObject("ADSystemInfo")
ADComputerName = ADSysInfo.ComputerName 'Get DN of local computer
ADRenameOK = "1"
ADOU = Mid(ADComputerName, InStr(ADComputerName, "=") + 1) 'Strip off just after the first = sign
ADOU = Mid(ADOU, InStr(ADOU, "=") - 2) 'Strip off at 2 before the second = sign
ComputerPath = "LDAP://" & ADComputerName
OUPath = "LDAP://" & ADOU
End if

'###### ASK USER FOR NEW DETAILS ###########

MsgBox "This script renames this computer and its active directory account" & vbCr & vbCr & "Name: " & ComputerName & vbCr & "FQDN: " & FQDN & vbCr & vbCr & "AD DN: " & ADComputerName & vbCr & "AD OU: " & ADOU, 0, "Information"

NewName = InputBox("Enter the new computer name below and click OK to continue","Rename: Step 1")
NewNameU = UCase(NewName)
NewNameL = LCase(NewName)
NewNameUCN = "CN=" & NewNameU

if NewName = "" then
wscript.echo "The computer name has not been changed"
else
vStartRenameCA = MsgBox ("Continue and rename computer to: " & NewName,vbYesNo or vbExclamation,"Rename: Step 2")
if vStartRenameCA = 6 then
With WSHShell
.RegDelete RegKeyTCPIP & "Hostname"
.RegDelete RegKeyTCPIP & "NV Hostname"
.RegWrite RegKeyCompName & "ComputerName\ComputerName", NewNameU
.RegWrite RegKeyCompName & "ActiveComputerName\ComputerName", NewNameU
.RegWrite RegKeyTCPIP & "Hostname", NewNameL
.RegWrite RegKeyTCPIP & "NV Hostname", NewNameL
End With
wscript.echo "The computer name and FQDN have been changed"
elseif vStartRenameCA = 7 then
wscript.echo "The computer name and FQDN have NOT been changed"
end if

if ADRenameOK = 1 then
vStartRenameAD = MsgBox ("Continue and rename AD Account to: " & NewName,vbYesNo or vbExclamation,"Rename: Step 3")
if vStartRenameAD = 6 then
Set objItem = GetObject(ComputerPath)
objItem.Put "dNSHostName", NewNameL & "." & DomainName
objItem.SetInfo
objItem.Put "displayName", "DESKTOP_" & NewNameU & "$"
objItem.SetInfo
objItem.Put "sAMAccountName", NewNameU & "$"
objItem.SetInfo

Set objNewOU = GetObject(OUPath)
Set objMoveComputer = objNewOU.MoveHere _
(ComputerPath, NewNameUCN)
wscript.echo "The active directory computer account has been changed"
elseif vStartRenameAD = 7 then
wscript.echo "The computer account in AD has NOT been changed"
End If
else
wscript.echo "Insufficient information to rename AD account"

End If

End if
[/color red]
TA [pipe]
 
Here are 3 different ways to change the name of the computer.

' This code changes the name of a computer. It does NOT modify
' the computer's account in the domain if one exists.
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' Publisher: O'Reilly Media
' ISBN: 0-596-00633-0
' Book web site: ' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strComputer = "<CurrentServerName>"
strNewName = "<NewServerName>"
' ------ END CONFIGURATION ---------
const HKLM = &H80000002
strKeyPath = "System\CurrentControlSet\Control\ComputerName\ComputerName"
set objReg = GetObject("winmgmts:\\" & strComputer & _
"\root\default:StdRegProv")
intRC = objReg.SetStringValue(HKLM, strKeyPath, "ComputerName", strNewName)
if intRC <> 0 then
WScript.Echo "Error setting ComputerName value: " & intRC
else
WScript.Echo "Successfully set ComputerName value to " & strNewName
end if

strKeyPath = "System\CurrentControlSet\Services\Tcpip\Parameters"
intRC = objReg.SetStringValue(HKLM, strKeyPath, "NV Hostname", strNewName)
if intRC <> 0 then
WScript.Echo "Error setting NV Hostname value: " & intRC
else
WScript.Echo "Successfully set NV Hostname value to " & strNewName
end if

WScript.Echo "Rebooting system..."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
for each objOS in objWMI.InstancesOf("Win32_OperatingSystem")
objOS.Reboot()
next

-----------------------------------------------------

' This code renames a computer in its domain and on the computer itself.
' This script works only against Windows XP and Windows Server 2003 computers.
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' Publisher: O'Reilly Media
' ISBN: 0-596-00633-0
' Book web site: ' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strComputer = "<ComputerName>" ' e.g. joe-xp
strNewComputer = "<NewComputerName>" ' e.g. joe-pc
strDomainUser = "<DomainUserUPN>" ' e.g. administrator@rallencorp.com
strDomainPasswd = "<DomainUserPasswd>"
strLocalUser = "<ComputerAdminUser>" ' e.g. joe-xp\administrator
strLocalPasswd = "<ComputerAdminPasswd>"
' ------ END CONFIGURATION ---------
' Connect to Computer
set objWMILocator = CreateObject("WbemScripting.SWbemLocator")
objWMILocator.Security_.AuthenticationLevel = 6
set objWMIComp = objWMILocator.ConnectServer(strComputer, _
"root\cimv2", _
strLocalUser, _
strLocalPasswd)
set objWMICompSys = objWMIComp.Get("Win32_ComputerSystem.Name='" & _
strComputer & "'")
' Rename Computer
intRC = objWMICompSys.Rename(strNewComputer, _
strDomainPasswd, _
strDomainUser)
if intRC <> 0 then
WScript.Echo "Rename failed with error: " & intRC
else
WScript.Echo "Successfully renamed " & strComputer & " to " & strNewComputer
end if

WScript.Echo "Rebooting system..."
Set colOS = objWMIComp.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
objOS.Reboot()
next

-------------------------------------------------

And here is a command utility that will change the name.

It is called "compname.exe" and it is found at
Let me know if this helps.

Thanks,

Ace
 
Thanks thats a good script but I still need to visit each machine to change the name. I want the process to be automatic, I have a list of the current computer names and the new names, it would be nice to run one script at startup to make the change, I'm not a script writer but I will have a play with the above and see what I can come up with thanks. [pipe]
 
What you need to do is use Netdom. It is a free utility. Install the Support Tools from your Server CD. You can easily script it and it will remotely rename a computer both locally and on the domain and reboot the machine.

I would suggest having it pull old and new names out of an Excel Spreadsheet.

Refer to this FAQ for information on how to use Excel with your script: faq329-4871

I hope you find this post helpful.

Regards,

Mark
 
I ended up finding a great script here searching for netdom on tek-tips. This allowed me to change machine names remotely and in active directory, it prompts me for the remote machine name and then the new name. I wanted to do it automatically but I can see lots of errors happening so I have my list and I can go through and make sure the script executes properly for each machine, I didn't do the reboot, and added /K after CMD so that the window doesn't close and I can see the command was successful here was the code:

'On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")

Dim sNewCname 'Variable to hold Computer Name you enter in the inputbox
Dim sOldCname 'computers current name

'First param is message text, second is message title
sOldCname = InputBox("Enter computers current name","Windows 2k3 Netdom Rename Script")
sNewCname = InputBox("Enter computers new name","Windows 2k3 Netdom Rename Script")

Call WSHShell.Run("cmd.exe /K NETDOM RENAMECOMPUTER " & sOldCname & " /newname:" & sNewCname & " /userd:domain\admin /passwordd:pass /usero:admin /passwordo:pass /force ")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top