1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
   | Dim RegKeyCompName, RegKeyTCPIP, WSHShell, ComputerName, HostName, DomainName, FQDN, ADRootDSE, ADSysInfo, ADComputerName, ADRenameOK, ADRNewName, vStartRenameCA, NewNAmeU, NewNameL, vStartRenameAD
 
On Error Resume Next
 
'###### READ IN EXISTING COMPUTERNAME AND FQDN ######
 
RegKeyCompName = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\"
RegKeyTCPIP = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\"
 
Set WSHShell = CreateObject("WScript.Shell")
 
ComputerName = WSHShell.RegRead (RegKeyCompName & "ComputerName\ComputerName")
Hostname = WSHShell.RegRead (RegKeyTCPIP & "Hostname")
DomainName = WSHShell.RegRead (RegKeyTCPIP & "Domain")
FQDN = HostName & "." & DomainName
 
Set ADRootDSE = GetObject("LDAP://RootDSE")
If Err.Number <> 0 then
    ADComputerName = "Unable to determine this information"
    ADOU = "Unable to determine this information"
    ADRenameOK = "0"
else
    Set ADSysInfo = CreateObject("ADSystemInfo")
    ADComputerName = ADSysInfo.ComputerName                'Get DN of local computer
    ADRenameOK = "1"
    ADOU = Mid(ADComputerName, InStr(ADComputerName, "=") + 1)    'Strip off just after the first = sign
    ADOU = Mid(ADOU, InStr(ADOU, "=") - 2)                'Strip off at 2 before the second = sign
    ComputerPath = "LDAP://" & ADComputerName
    OUPath = "LDAP://" & ADOU
End if
 
'###### ASK USER FOR NEW DETAILS ###########
 
MsgBox "This script renames this computer and its active directory account" & vbCr & vbCr & "Name: " & ComputerName & vbCr & "FQDN: " & FQDN & vbCr & vbCr & "AD DN: " & ADComputerName & vbCr & "AD OU: " & ADOU, 0, "Information"
 
NewName = InputBox("Enter the new computer name below and click OK to continue","Rename: Step 1")
NewNameU = UCase(NewName)
NewNameL = LCase(NewName)
NewNameUCN = "CN=" & NewNameU
 
if NewName = "" then
    wscript.echo "The computer name has not been changed"
else
    vStartRenameCA = MsgBox ("Continue and rename computer to: " & NewName,vbYesNo or vbExclamation,"Rename: Step 2")
    if vStartRenameCA = 6 then
        With WSHShell
            .RegDelete RegKeyTCPIP & "Hostname"
            .RegDelete RegKeyTCPIP & "NV Hostname"
            .RegWrite RegKeyCompName & "ComputerName\ComputerName", NewNameU
            .RegWrite RegKeyCompName & "ActiveComputerName\ComputerName", NewNameU
            .RegWrite RegKeyTCPIP & "Hostname", NewNameL
            .RegWrite RegKeyTCPIP & "NV Hostname", NewNameL
        End With
        wscript.echo "The computer name and FQDN have been changed"
    elseif vStartRenameCA = 7 then
        wscript.echo "The computer name and FQDN have NOT been changed"
    end if
 
    if ADRenameOK = 1 then
        vStartRenameAD = MsgBox ("Continue and rename AD Account to: " & NewName,vbYesNo or vbExclamation,"Rename: Step 3")
        if vStartRenameAD = 6 then
            Set objItem = GetObject(ComputerPath)
            objItem.Put "dNSHostName", NewNameL & "." & DomainName
            objItem.SetInfo
            objItem.Put "displayName", "DESKTOP_" & NewNameU & "$"
            objItem.SetInfo
            objItem.Put "sAMAccountName", NewNameU & "$"
            objItem.SetInfo
 
            Set objNewOU = GetObject(OUPath)
            Set objMoveComputer = objNewOU.MoveHere _
                (ComputerPath, NewNameUCN)
            wscript.echo "The active directory computer account has been changed"
        elseif vStartRenameAD = 7 then
            wscript.echo "The computer account in AD has NOT been changed"
        End If
    else
        wscript.echo "Insufficient information to rename AD account"
 
    End If
 
End if |