Jump to content

Rename Computer and Join Domain


Recommended Posts

Hello all,

I want to rename a computer and then join the domain WITHOUT a reboot. Since I am able to do so manually, I figured it would also be possible thru scripting...

My code works BUT The Account created on the domain uses the old computer

name (the DNS Name field is using the new name).

post-15410-1144596404_thumb.png

RENAME METHOD 1 (using the registry, may be compatible with Win2K)

Set shell = CreateObject("WScript.Shell")
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", NewName
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", NewName

OR RENAME METHOD 2 (WinXP Only)

Set objWMIService = GetObject("Winmgmts:root\cimv2")
For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
objComputer.rename(NewName, NULL, NULL)
Next

JOIN DOMAIN (WinXP Only?)

Const JOIN_DOMAIN			 = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

strDomain = "AD"
strPassword = "password"
strUser = "username"

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = GetObject ("winmgmts:\\" & strComputer & _
"\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & _
"\" & strUser, NULL, 1 + 2)

Should I create the account on the domain before joining?

Do I have to use the MoveHere method of ADSI to apply the new name to the account or there is a better way?

Any help appreciated

Link to comment
Share on other sites


I don't think you can join a domain and have it take effect until a reboot. Same would go for renaming a computer. As far as new installs go, I always prestage the account for RIS, and the domain join happens during the GUI portion of setup. For pre-existing installs, going from workgroup mode to joining a domain has always given me a prompt to reboot.

I'd be interested in knowing how you join a domain without needing to reboot though. Manual or otherwise.

Link to comment
Share on other sites

I'd be interested in knowing how you join a domain without needing to reboot though. Manual or otherwise.

Well, we are currently adding those new computer manually and we always change the name and then adding them to the domain and it works without a reboot (clean install on Windows XP SP2). It may occasionally fails (about 1 time on 10 attemps) for unknown reasons but most of the times it works.

This code will be part of a HTA Wizard at the end of our unattended Installation to perform some additional task like adding the inventory tag, updating the Intranet, Etc.

I don't want to always add the computer to the domain that's why I want to do it this way (instead of adding them on the domain during the unattended install).

What if I use my wizard to Create the computer account first on the domain? Would that work?

Edited by Incroyable HULK
Link to comment
Share on other sites

I've never tried to prestage a computer account outside of the scope of RIS. I would imagine that it should work the same however. You would need to enter in the UUID of the computer, or in the absence of a UUID, create one using the MAC address padded with zeros.

If you take a look at my script pack, I have an HTA application in there that among other things, names the computer. I had always intended it for use in workgroup environments, but I think I could modify it to join a domain of the user's choice.

Link to comment
Share on other sites

I do not know if this script will help

Save as RenameComputer.vbs

Dim Act, CName, Fso, Key, RegName, Rename1, Rename2, Vbs, UnameSet Act = CreateObject("WScript.Shell")Set Fso = CreateObject("Scripting.FileSystemObject")CName = Act.ExpandEnvironmentStrings("%ComputerName%")Uname = Act.ExpandEnvironmentStrings("%UserName%")Vbs = Act.ExpandEnvironmentStrings("%Systemdrive%\ChangeComputerName.Vbs")Key = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"RegName = Act.RegRead(Key & "ComputerName")Rename1 = InputBox (Uname & ", please type in the new Computer Name","Change Computer Name",,5500,4800)Rename2 = Msgbox ("Did You Want To Change This" & vbCrLf & "Old Name : " & CName &_vbCrLf & "To This Name : " & Rename1, 4 + 48,"Rename Confirm")If Rename2 = 6 Then Act.RegWrite Key & "ComputerName", A2 End IfIf Rename2 = 7 Then Q = MsgBox (UName & " Has Cancel The Name Change", 0 + 32,"Cancel") End IfIf Fso.FileExists(Vbs) Then Fso.DeleteFile(Vbs) End If
Here is a SFX of the above code

I Gunsmokingman give all users full permission to do what ever they want with this script.

ChangeComputerName.exe

Link to comment
Share on other sites

That'll rename it just fine, but you'll still need a reboot to have it take effect. I've been doing way too much research into this today. So far everything I run into indicates that a name change or a change in domain membership on a computer that already has the OS installed will require a reboot.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...