Hi I have found this script which works very well it prompts for a name to change the computer name to and also changes the computer account name in Active Directory. I have 100 machines I need to change the name of and would like to either be able run this script and have it change a remote machine or have the script run at startup through Group Policy and have the users username set as their computer name? Anyone able to tweak the following?[/color blue]
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
[/color red]
TA![[pipe] [pipe] [pipe]](/data/assets/smilies/pipe.gif)
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
[/color red]
TA
![[pipe] [pipe] [pipe]](/data/assets/smilies/pipe.gif)