Here is what I am trying to do.
A script will run after our sysprep image during the "runonce" phase of setup. The computer is imaged with a random computer name and joined to a workgroup. It will preform the following steps.
prompt for AD username
prompt for AD password
search AD to see if a GUID already exists for this computer
If GUID is found present a box saying "this computer was found in AD as %computername%, do you wish to keep this name?"
If yes, rename computer to matching AD computer name and join domain.
If no/or no computer GUID was found, prompt for what you want to call this computer then take that input and add to domain.
I have most of the code already, but the one thing I don't know how to do is search AD for a computer by GUID (or at all)
The problem I think will be that the computer currently has a random generated computer name after it's imaged, so I'm not sure how to search AD to see if it used to exist. Lets say the computer used to be called "finance34" but has since been reimaged and now it is on a workgroup with a random computer name of "winnt-22947387". How would I search AD to see if the computer USED to be called something even though it isn't currently called finance34? I assume the best way would be to look for the GUID, but if someone has a better suggestion that would be great.
Perhaps this will help (I did not write this code):
Code:
'Enable explicit variable declaration
'Option Explicit
Const ADS_SCOPE_SUBTREE = 2
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144
title = "Notebook Config"
strDomain = InputBox("Enter your domain", title & " - Enter Credentials", "domain.internal")
strUser = InputBox("Enter a username with administrative privelages on the " & strDomain & " domain", title & " - Enter Credentials", "administrator")
strPassword = InputBox("Enter password for " & strUser, title & " - Enter Credentials")
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
message = "Please enter computer name. Leave blank or press cancel to quit."
newComputerName = InputBox(message, title & " - New Computer Name", "NB")
If newComputerName = "" Then
WScript.quit
End If
If isComputerAccountExists(newComputerName) = True Then
deleteaccount(newComputerName)
MsgBox "the newcomputer name you enter found and deleted"
End If
areYousure = MsgBox("Are you sure you want to add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion, "Add computer to domain")
If areYouSure = "7" Then
MsgBox "Exiting script.", vbInformation
WScript.quit
End If
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=ou1,ou=ou2,ou=ou3,ou=ou4,ou=ou5,dc=domain,dc=internal", _
JOIN_DOMAIN + ACCT_CREATE)
If ReturnValue = 0 Then
MsgBox "Computer added to the " & strDomain & " domain without error. Proceeding to change computer name..."
Else
MsgBox "Computer not added to the " & strDomain & " domain successfully. Return value: " & ReturnValue
End If
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
( "Select * from Win32_ComputerSystem" )
For Each objComputer In colComputers
MsgBox "Renaming computer to: " & newComputername
ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)
If ErrCode = 0 Then
Set WSHShell = WScript.CreateObject("WScript.Shell")
msgShutdown = WshShell.Popup("Computer renamed correctly. Computer will restart in 5 seconds.", 4, "Restart")
WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 1"
Else
MsgBox "Error changing computer name. Error code: " & ErrCode
End If
Next
Sub deleteaccount(Computer)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select ADsPath From " & _
"'LDAP://DC=domain,DC=com' Where objectClass='computer'" & _
" and Name = '" & computer & "'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Set objComputer = GetObject(objRecordSet.Fields("ADsPath").Value)
objComputer.DeleteObject(0)
objRecordSet.MoveNext
Loop
End Sub
Function isComputerAccountExists(computer)
Dim conn, cmd, rs
Set conn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conn.provider = "adsdsoobject"
conn.open "active directory provider"
cmd.activeconnection = conn
cmd.commandtext = "<LDAP://" & GetObject("LDAP://rootdse").Get("defaultnamingcontext") & ">;(&(objectcategory=computer)(objectclass=computer)(cn=" & computer & "));cn;subtree"
Set rs = cmd.Execute
If rs.recordcount = 0 Then
isComputerAccountExists = False
Else
isComputerAccountExists = True
End If
End Function