| Login

Search this Blog


Links I like to keep around




Here are the most recent postings to this blog. Use the archive calendar or search to find other entries.
Nov16

Written by:R Teachout
11/16/2006 6:16 PM

'==========================================================================
'
' VBScript Source File
'
' NAME: computer_rename.vbs
'
' AUTHOR: Bradly T. Bolin
' DATE  : 4/5/2001
'
' COMMENT: Removes computer from an NT domain, renames it, then rejoins it
'  Requires NETDOM.EXE from Windows 2000 Resource Kit
'
'==========================================================================

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("Scripting.FileSystemObject")
Set oWshShell = CreateObject("WScript.Shell")
Set oWshEnvironment = oWshShell.Environment("Process")


sCurrentName = oWshEnvironment("COMPUTERNAME")
sTempDir = oWshEnvironment("TEMP")
sProgram = "computer_rename.vbs"
sProgramDir = oFileSystem.GetAbsolutePathName(".")

On Error Resume Next

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

Select Case  sPHASE
 Case ""
  Call REMOVE 'Subroutine
 Case "1"
  Call REJOIN 'Subroutine
End Select

'Restart computer
Dim OpSysSet, OpSys
Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
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("Scripting.Dictionary")
 Set oCompName = oFileSystem.GetFile(sProgramDir & "\" & "compname.txt")
 Set oTextStream = oCompName.OpenAsTextStream(1)

 Do While Not oTextStream.AtEndOfStream
  sLine = oTextStream.ReadLine
  sArray = Split(sLine, " = ", -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("Error")
  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 & "\" & sProgram, sTempDir & "\" & sProgram
 oFileSystem.CopyFile sProgramDir & "\" & "NETDOM.EXE", sTempDir & "\" & "NETDOM.EXE"
 'Backup user logon name to PreviousName value
 Dim sUserName
 sUserName = oWshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName")
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\PreviousUser", sUserName
 'Increment PHASE value
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Phase", 1
 'Place reference to program in RunOnce key
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\RUN_RENAME", sTempDir & "\" & sProgram
 'Enable AutoAdminLogon
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", 1
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", "XXX"
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", "XXX"
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", sNewName

 'Execute NETDOM REMOVE
 oWshShell.Run sTempDir & "\" & "NETDOM.EXE REMOVE " & sCurrentName & " /D:XXX /Ud:XXX /Pd:XXX", 1, TRUE
 'Rename computer
 oWshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", sNewName
 oWshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", 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("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\PreviousUser")
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", sUserName
 'Delete PHASE value
 oWshShell.RegDelete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Phase"
 'Disable AutoAdminLogon
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", 0
 oWshShell.RegDelete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword"
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", "XXX"
 'Place clean-up routine in RunOnce key
 Dim sCMD
 sCMD = "c:\winnt\system32\"
 oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\RUN_CLEANUP", sCMD & "CMD.EXE /c DEL " & sTempDir & "\" & sProgram
 'Execute NETDOM JOIN
 oWshShell.Run sTempDir & "\" & "NETDOM.EXE JOIN " & sCurrentName & " /D:XXX /Ud:XXX /Pd:XXX", 1, TRUE
End Sub

Download : RenameWorkstation.vbs.txt

Tags:

Even as a longtime critic of the company, I must admit that Microsoft occasionally flirts with the truth. Well, perhaps 'flirt' is too strong a word. Let's just say Microsoft sometimes honks and waves as it drives by her house.
-InfoWorld Editor Nicholas Petreley, 06-14-99

Inspired by Nina