Recipe2.8.Setting the Name of a Server


Recipe 2.8. Setting the Name of a Server

Problem

You want to configure the host name for a server.

Solution

Using a graphical user interface

  1. From the Control Panel, open the System applet.

  2. Select the Computer Name tab (or Network Identification on Windows 2000).

  3. Click the Change button (or Properties on Windows 2000).

  4. Enter the new name in the Computer name field.

  5. If the computer is a member of the domain, you will be prompted for credentials to use for renaming the account in the domain unless you are logged in with a domain administrator account.

  6. Click OK.

  7. Click OK to confirm that you'll need to restart (this won't actually restart the computer).

  8. Click OK again.

  9. Click Yes if you want to restart now or No to restart later.

Using a command-line interface

Use this command to display the current computer name:

> hostname

The compname.exe utility, which is available from http://www.willowhayes.co.uk/, can also display the current computer name:

> compname /s

But this command can also do a whole lot more. This changes a computer's name:

> compname /c <NewServerName>

The truly powerful feature of compname is its ability to name a system based on certain variables (or what it calls templates). You can name a computer based on its serial number, UUID, MAC address, and date, among other things. The following example changes a computer name to contain its serial number at the end:

> compname /c pc-?s

Run compname /? for more information on the available templates.

This is a registry-based method for setting a computer's name:

> reg add \\<CurrentServerName>\HKLM\SYSTEM\CurrentControlSet\Services\ Tcpip\Parameters /v "NV Hostname" /d <NewServerName> > reg add \\<CurrentServerName>\HKLM\SYSTEM\CurrentControlSet\Control\ ComputerName\ComputerName /v ComputerName /d <NewServerName> > shutdown \\<CurrentServerName> /r

If the computer is already part of a domain and you use either the compname or registry methods, the computer's domain account will become out of sync and it will no longer be able to function in the domain. To rename a computer that is part of a domain, use the netdom.exe command:

> netdom renamecomputer <CurrentServerName> /newname:<NewServerName> /userd:<DomainUserName>

Using VBScript
' This code changes the name of a computer.  It does NOT modify  ' the computer's account in the domain if one exists. ' ------ 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. ' ------ 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

Discussion

Setting the name of a computer is a straightforward operation, but it does require a reboot. If the computer is member of an Active Directory domain, the corresponding computer account must also be renamed. Some of the solutions I described do this automatically and some do not.

In some cases, renaming a computer can adversely affect services running on the computer. For example, you cannot rename a machine that is a Windows 2000 domain controller, Exchange 2000 or 2003 server, or a Windows Certificate Authority without first removing those services.


Using a command-line interface

The renamecomputer option in netdom is new to Windows Server 2003. You can run it against a remote computer and it includes a /Reboot option that allows you to automatically reboot the computer after the rename is done.

Using VBScript

The Win32_ComputerSystem::Rename method must be run on the local machine unless the computer is a member of a domain. Unlike the graphical and command-line solutions, you cannot specify alternate credentials for the connection to the computer other than domain credentials. For this reason, the user and password you use with the Rename method must have administrative privileges on the target machine (i.e., part of the Administrators group) and on the computer object in Active Directory.

This method is new in Windows XP and Windows Server 2003, and is not available on Windows 2000 and earlier machines.


See Also

MS KB 228544 (Changing Computer Name in Windows 2000 Requires Restart), MS KB 238793 (Enhanced Security Joining or Resetting Machine Account in Windows 2000 Domain), MS KB 260575 (HOW TO: Use Netdom.exe to Reset Machine Account Passwords of a Windows 2000 Domain Controller), MS KB 325354 (HOW TO: Use the Netdom.exe Utility to Rename a Computer in Windows Server 2003), and MSDN: Win32_ComputerSystem:: Rename



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net