Incroyable HULK Posted April 9, 2006 Posted April 9, 2006 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).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", NewNameShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", NewNameOR RENAME METHOD 2 (WinXP Only)Set objWMIService = GetObject("Winmgmts:root\cimv2")For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem") objComputer.rename(NewName, NULL, NULL)NextJOIN DOMAIN (WinXP Only?)Const JOIN_DOMAIN = 1Const ACCT_CREATE = 2Const ACCT_DELETE = 4Const WIN9X_UPGRADE = 16Const DOMAIN_JOIN_IF_JOINED = 32Const JOIN_UNSECURE = 64Const MACHINE_PASSWORD_PASSED = 128Const DEFERRED_SPN_SET = 256Const INSTALL_INVOCATION = 262144strDomain = "AD"strPassword = "password"strUser = "username"Set objNetwork = CreateObject("WScript.Network")strComputer = objNetwork.ComputerNameSet 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
RogueSpear Posted April 9, 2006 Posted April 9, 2006 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.
Incroyable HULK Posted April 9, 2006 Author Posted April 9, 2006 (edited) 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 April 9, 2006 by Incroyable HULK
RogueSpear Posted April 9, 2006 Posted April 9, 2006 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.
gunsmokingman Posted April 9, 2006 Posted April 9, 2006 I do not know if this script will helpSave as RenameComputer.vbsDim 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 IfHere is a SFX of the above codeI Gunsmokingman give all users full permission to do what ever they want with this script.ChangeComputerName.exe
RogueSpear Posted April 9, 2006 Posted April 9, 2006 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.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now