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!

Remove computer from NT domain join to 2003 AD Domain 1

Status
Not open for further replies.

NJDEV1K

Programmer
Jan 10, 2004
19
US
I have a script I have been playing with that uses netdom, but I would like to have a script that can remove the computer from one domain and rename the PC in the process of joining it to a new domain. For the new computername I wanted it to rename it with the username of the person logged in, if the user is logged in to three machines I want it to be able to name the PC like so "User01", "User02" and "User03". any help would be appreciated.

The script I am playing with though have not tested can be supplied if needed.
 
Here is my script that has not been tested

Set wshShell = CreateObject("WSCript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")

NetDomLocation = "C:\netdom.exe"
For i = 1 To 10
ComputerName = "%computername%"
ComputerNameNew = "PC%username%" & i
DomainNameOld = TEST
DomainNameNew = TEST2
DomainUser = NJDEV1K
DomainUserPassword = testpassword
LocalUser = NJDEV1K
LocalUserPassword = testpassword

wshShell.run NetDomLocation & " REMOVE " & ComputerName & "/domain:" & DomainNameOld & " /userd:" & DomainUser & "/passwordd:" & DomainUserPassword & " /usero:" & LocalUser & " /passwordO:" & LocalUserPassword, 0, TRUE

wshShell.run NetDomLocation & " JOIN " & ComputerNameNew & "/domain:" & DomainNameNew & " /userd:" & DomainUser & "/passwordd:" & DomainUserPassword & " /usero:" & LocalUser & " /passwordO:" & LocalUserPassword, 0, TRUE
 
I've done this via script without the rename part. I can tell you that you need to play with Wscript.Sleep command to adjust the timing after the REMOVE command. Following the remove the computer will automatically reboot and if it does you won't be able to remote control it any longer. The above script will attempt to execute the JOIN command before the REMOVE command has completed and will fail. Not NJDEV1K's fault, I tried the same thing originally and he added the disclaimer this wasn't tested.

Here is my script that I know works and that uses a list of computer names to act upon. I suggest you synchronize the admin passwords on both domains.

If you manage to rework this to get it to do the rename, please post the code back up.

'==========================================================================
'
' NAME: NetDomJoinWorkstations
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: ' DATE : 6/24/2003
'
' COMMENT: Joins computers to a new domain. Edit domain name,
' user ID and passwords below. Uses a workstation list wslist.txt.
' Modification 7/28/2003 to include Remove command. Suggest synchronizing old and new server passwords
'
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strWorkstation In RemotePC
'Do something useful with strWorkstation
Call WSHShell.Run(&quot;cmd.exe /c NETDOM REMOVE &quot; & strWorkstation &&quot;/Domain:<domain> /UserD:<user> /PasswordD:<password> UserO:<user> /PasswordO:<password> /REBoot:30000&quot;)
Wscript.sleep 15000
Call WSHShell.Run(&quot;cmd.exe /c NETDOM JOIN &quot; & strWorkstation &&quot;/Domain:<domain> /UserD:<user> /PasswordD:<password> UserO:<user> /PasswordO:<password> /REBoot:0&quot;)

Next
 
I found this script, maybe someone can tell me what I need to edit to get it to work,



Option Explicit

Dim oFileSystem 'Scripting Dictionary object
Dim oWshShell 'Windows Script Host Shell object
Dim sCurrentName 'holds computername environment variable
Dim oWshEnvironment 'Windows Script Host environment object
Dim sTempDir 'temporary directory of computer on which comprename.vbs is run
Dim sPHASE 'holds number indicating PHASE in rename operation
Dim sProgram 'name of this script
Dim sProgramDir 'Path to this script

Set oFileSystem = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set oWshShell = CreateObject(&quot;WScript.Shell&quot;)
Set oWshEnvironment = oWshShell.Environment(&quot;Process&quot;)


sCurrentName = oWshEnvironment(&quot;COMPUTERNAME&quot;)
sTempDir = oWshEnvironment(&quot;TEMP&quot;)
sProgram = &quot;computer_rename.vbs&quot;
sProgramDir = oFileSystem.GetAbsolutePathName(&quot;.&quot;)

On Error Resume Next

sPHASE = oWshShell.RegRead(&quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Phase&quot;)

Select Case sPHASE
Case &quot;&quot;
Call REMOVE 'Subroutine
Case &quot;1&quot;
Call REJOIN 'Subroutine
End Select

'Restart computer
Dim OpSysSet, OpSys
Set OpSysSet = GetObject(&quot;winmgmts:{(Shutdown)}//./root/cimv2&quot;).ExecQuery(&quot;select * from Win32_OperatingSystem where Primary=true&quot;)
For Each OpSys In OpSysSet
OpSys.Reboot()
Next
WScript.Quit

'REMOVE Subroutine: Remove computer from domain, rename it and restart it

Sub REMOVE
'Create Dictionary (key: Name, item: NewName) and load data into it
Dim oCompName, oTextStream, sArray, sLine, sNewName
Dim oDictionary 'VBScript Dictionary Object

Set oDictionary = CreateObject(&quot;Scripting.Dictionary&quot;)
Set oCompName = oFileSystem.GetFile(sProgramDir & &quot;\&quot; & &quot;compname.txt&quot;)
Set oTextStream = oCompName.OpenAsTextStream(1)

Do While Not oTextStream.AtEndOfStream
sLine = oTextStream.ReadLine
sArray = Split(sLine, &quot; = &quot;, -1, 1)
' sArray(0) contains NAME.
' sArray(1) contains NEWNAME.
oDictionary.Add sArray(0), sArray(1)
Loop
oTextStream.Close

'Abort if computer is NOT in list of those to be renamed
If oDictionary.Exists(sCurrentName) = FALSE Then
MsgBox(&quot;Error&quot;)
WScript.Quit
End If
'It's OK to proceed, so retrieve new computer name and place in variable sNewName
sNewName = oDictionary.Item(sCurrentName)
'Copy files necessary for the rename operation to local machine
oFileSystem.CopyFile sProgramDir & &quot;\&quot; & sProgram, sTempDir & &quot;\&quot; & sProgram
oFileSystem.CopyFile sProgramDir & &quot;\&quot; & &quot;NETDOM.EXE&quot;, sTempDir & &quot;\&quot; & &quot;NETDOM.EXE&quot;
'Backup user logon name to PreviousName value
Dim sUserName
sUserName = oWshShell.RegRead(&quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName&quot;)
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\PreviousUser&quot;, sUserName
'Increment PHASE value
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Phase&quot;, 1
'Place reference to program in RunOnce key
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\RUN_RENAME&quot;, sTempDir & &quot;\&quot; & sProgram
'Enable AutoAdminLogon
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon&quot;, 1
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName&quot;, &quot;XXX&quot;
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword&quot;, &quot;XXX&quot;
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName&quot;, sNewName

'Execute NETDOM REMOVE
oWshShell.Run sTempDir & &quot;\&quot; & &quot;NETDOM.EXE REMOVE &quot; & sCurrentName & &quot; /D:XXX /Ud:XXX /Pd:XXX&quot;, 1, TRUE
'Rename computer
oWshShell.RegWrite &quot;HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName&quot;, sNewName
oWshShell.RegWrite &quot;HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname&quot;, sNewName
End Sub

'REJOIN Subroutine: Rejoin computer to the domain and clean some stuff up

Sub REJOIN
'Restore previous user logon name from PreviousUser Value
Dim sUserName
sUserName = oWshShell.RegRead(&quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\PreviousUser&quot;)
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName&quot;, sUserName
'Delete PHASE value
oWshShell.RegDelete &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Phase&quot;
'Disable AutoAdminLogon
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon&quot;, 0
oWshShell.RegDelete &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword&quot;
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName&quot;, &quot;XXX&quot;
'Place clean-up routine in RunOnce key
Dim sCMD
sCMD = &quot;c:\winnt\system32\&quot;
oWshShell.RegWrite &quot;HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\RUN_CLEANUP&quot;, sCMD & &quot;CMD.EXE /c DEL &quot; & sTempDir & &quot;\&quot; & sProgram
'Execute NETDOM JOIN
oWshShell.Run sTempDir & &quot;\&quot; & &quot;NETDOM.EXE JOIN &quot; & sCurrentName & &quot; /D:XXX /Ud:XXX /Pd:XXX&quot;, 1, TRUE
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top