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 83 84 85 86 87 88 89 90 91
| Dim RegKeyCompName, RegKeyTCPIP, WSHShell, ComputerName, HostName, DomainName, FQDN, ADRootDSE, ADSysInfo, ADComputerName, ADRenameOK, ADRNewName, vStartRenameCA, NewNAmeU, NewNameL, vStartRenameAD
On Error Resume Next
'Definition du nouveau nom de la machine = UC + adresse MAC
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
dim mac
dim NouveauNom 'Nouveau nom ?d閒inir
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
mac = IPConfig.MACAddress(i)
Next
End If
Next
'MsgBox mac
NouveauNom = "UC" & Replace(mac,":","")
'MsgBox NouveauNom
'###### Recuperation des infos sur le PC local ######
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 'Recup DN de l'ordinateur local
ADRenameOK = "1"
ADOU = Mid(ADComputerName, InStr(ADComputerName, "=") + 1)
ADOU = Mid(ADOU, InStr(ADOU, "=") - 2)
ComputerPath = "LDAP://" & ADComputerName
OUPath = "LDAP://" & ADOU
End if
'###### MODIFICATION DU NOM DE L'ORDINATEUR EN LOCAL ET SUR L'AD ###########
NewName = NouveauNom
NewNameU = UCase(NewName)
NewNameL = LCase(NewName)
NewNameUCN = "CN=" & NewNameU
if NewName = "" then
wscript.echo "Le nom de l'ordinateur n'a pas pu etre change!"
else
'Modification dans la base de registre
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
'Modification dans l'AD
if ADRenameOK = 1 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)
else
wscript.echo "Informations insuffisante pour renommer sur l'AD"
End If
End if |
Partager