'author: mrmovie
'description: script to add machine to domain
'version=1.2
' updated mrmovie, returncode 2224 odd behavour where the inital discovery said it couldnt find machine account in AD
'but the WMI method says there is already one there...
' 1.2, 4.1.2.0, 7/12/2008, updated mrmovie, added the defou switch to override the strOU so we can just let it build into the domains default ou
Option Explicit
Dim intReturn
Call Main("", "", "", "", "", "")
If intReturn <> 0 Then
WScript.Quit intReturn
Else
WScript.Quit Err.Number
End If
'sub to add machine to domain, will error i believe if there is a machine account there already
'that type of check should be done before with a different thread.
Sub Main(ByVal strDomain, ByVal strPassword, ByVal strUsername, ByVal strOU, ByVal strDeleteAccount, ByVal strDefOU)
Dim WshNetwork, objComputer, strComputer, blnDeleteAccount, blnCreateAccount, blnTargetOK
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
'overwrite these with arguments passed at cmdline
If Wscript.Arguments.Named.Exists("help") Then
Wscript.Echo "joindomain.vbs CmdLine Params:" & vbCrLf & "/domain:<domain2join>" & vbCrLf & _
"/username:<username>" & vbCrLf & "/password:<password>" & VbCrLf & _
"parameters are case sensitive." & vbCrLf & vbCrLf & _
"example: joindomain.vbs /username:addtodomain /domain:domainX.local /password

assword123"
Wscript.Quit
End If
'get the domain?
If Wscript.Arguments.Named.Exists("domain") Then
strDomain = Wscript.Arguments.Named.Item("domain")
End If
'get the password
If Wscript.Arguments.Named.Exists("password") Then
strPassword = Wscript.Arguments.Named.Item("password")
End If
'get the username
If Wscript.Arguments.Named.Exists("username") Then
strUsername = Wscript.Arguments.Named.Item("username")
End If
'get the ou
If Wscript.Arguments.Named.Exists("ou") Then
strOU = Wscript.Arguments.Named.Item("ou")
End If
'build into default ou
If Wscript.Arguments.Named.Exists("defou") Then
strDefOU = LCase(Wscript.Arguments.Named.Item("defou"))
End If
'get the ou
If Wscript.Arguments.Named.Exists("deleteaccount") Then
strDeleteAccount = Wscript.Arguments.Named.Item("deleteaccount")
End If
'shall we delete the old machine account?
If LCase(strDeleteAccount) = "true" Then
blnDeleteAccount = True
Else
blnDeleteAccount = False
End If
'default create account, will update later by checking for existance of machine account
blnCreateAccount = True
'can we use the target OU?
blnTargetOK = True
'Wscript.Quit
Set WshNetwork = CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName
Set WshNetwork = Nothing
'Wscript.Echo strPassword
'Wscript.Echo strUsername
'Wscript.Echo strDomain
Wscript.Echo strOU
'does out target OU exist
Dim objTargetOU, adsNameSpace, strTemp
Set adsNameSpace = GetObject("LDAP:")
On Error Resume Next
Set objTargetOU = adsNamespace.OpenDSObject("LDAP://" & strDomain & ":389/" & strOU, strUsername, strPassword, 1)
strTemp = objTargetOU.name
On Error GoTo 0
If strTemp = "" Then
intReturn = 333
Exit Sub
End If
'does the machine account already exist in the domain?
Dim adoConnection, adoCommand, adoRecordSet, strQuery
Dim strDN, strMachineOU, strName
strMachineOU = "_blank_"
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Properties("User ID") = strUserName
adoConnection.Properties("Password") = strPassword
adoConnection.Properties("Encrypt Password") = True
'MsgBox "W1"
adoConnection.Open("Active Directory Provider")
'MsgBox "W2"
Set adoCommand = CreateObject("ADODB.Command")
adoCommand.ActiveConnection = adoConnection
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = True
strQuery = "<LDAP://" & strDomain & ":389>;(&(objectCategory=computer)(objectClass=computer)(name=" & Trim(strComputer) & "));" _
& "cn,DistinguishedName,name,SerialNumber;subtree"
adoCommand.CommandText = strQuery
Set adoRecordset = adoCommand.Execute
'more than one machine object has this serial number associated with it!!!! oh dear, bad, bad, bad
If adoRecordset.recordCount > 1 Then
Wscript.Echo "There are " & adoRecordSet.recordCount & " machines with the same Name (" & Trim(strComputer) & ")"
End If
'we have found a pre-existing machine account with this machines serial number associated with it
'lets get the distinguisedname and see if it matches our machine name
If adoRecordset.recordCount = 1 Then
Wscript.Echo "found one machine with that info"
Do Until adoRecordset.EOF
strDN = adoRecordset.Fields("DistinguishedName").Value
strMachineOU = Right(strDN, Len(strDN) - InStr(strDN, ","))
'MsgBox strMachineOU
strName = adoRecordset.Fields("name").Value
'txthostname.innertext = UCase(strName)
adoRecordset.MoveNext()
Loop
'ok we have a machine account with the serial number in question, does it match the machinename we are trying to build?
If strDN <> "" And strMachineOU <> "" And strName <> "" Then
Wscript.Echo strDN & " " & strMachineOU & " " & strName
End If
End If
'do we have a pre-existing machine account and is it in the right place?
If strMachineOU <> "_blank_" Then
WScript.Echo "machine name already exists"
'should we delete the old machine account
If blnDeleteAccount = True Then
'ok we need to delete the account
Else
'we dont need to delete the account, we need to move it if it is not in the right place already
'is it in the right place?
If strMachineOU = strOU Then
WScript.Echo "machine in the right ou already"
Else
WScript.Echo "not in the right ou"
'we definitely need to move it
If strDN <> "" Then
'not sure i really need to bind to this object! i have the ADsPath and Name already!!!!
Set objComputer = adsNamespace.OpenDSObject("LDAP://" & strDomain & ":389/" & strDN, strUsername, strPassword, 1)
'Set objComputer = GetObject("LDAP://" & strDN)
Dim intA
On Error Resume Next
WScript.Echo objComputer.ADsPath & " .... " & objComputer.Name
WScript.Echo objTargetOU.ADsPath
objTargetOU.MoveHere objComputer.ADsPath, objComputer.Name
'MsgBox intA
'If intA <> 0 Or intA = "" Then
If Err.Number <> 0 Then
blnTargetOK = False
End If
On Error Goto 0
Set objComputer = Nothing
End If
End If
End If
'we dont need to create machine account as it is already there
blnCreateAccount = False
Else
WScript.Echo "machine name is not there, nice one"
End If
'WScript.Quit
Set objTargetOU = Nothing
'MsgBox ""
'are we ok to move into the target OU?
If blnTargetOK = False Then
intReturn = "666"
strOU = strMachineOU
End If
'######################################
If InStr(strUserName, "\") Then
WScript.Echo strUsername ' just do something
Else
strUserName = strDomain & "\" & strUsername
End If
Dim intAction
If blnCreateAccount = False Then
intAction = JOIN_DOMAIN
Else
intAction = JOIN_DOMAIN + ACCT_CREATE
End If
'Set objComputer = Nothing
'strOU = ""
Set objComputer = GetObject("winmgmts:!\\" & strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
If strDefOU = "true" Then
WScript.Echo strDomain & " " & strPassword & " " & strUsername & " " & Null & " " & intAction
'MsgBox ""
intReturn = intReturn + objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strUsername, Null, intAction)
Else
WScript.Echo strDomain & " " & strPassword & " " & strUsername & " " & strOU & " " & intAction
'MsgBox ""
intReturn = intReturn + objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strUserName, strOU, intAction)
End If
Wscript.Echo "returncode=" & intReturn
If intReturn = "2224" Then
'ok something silly is going on, JoinDomain method is reporting that the machine account already exists!
intAction = JOIN_DOMAIN
If strDefOU = "true" Then
WScript.Echo strDomain & " " & strPassword & " " & strUsername & " " & Null & " " & intAction
'MsgBox ""
intReturn = intReturn + objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strUsername, Null, intAction)
Else
WScript.Echo strDomain & " " & strPassword & " " & strUsername & " " & strOU & " " & intAction
'MsgBox ""
intReturn = intReturn + objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strUserName, strOU, intAction)
End If
End If
Wscript.Echo "returncode=" & intReturn
Set objComputer = Nothing
'SERIAL NUMBER UPDATE........
'update the machine account details with the serial number information....
'Wscript.Quit intReturn
'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
End Sub
I Hear, I Forget
I See, I Remember
I Do, I Understand
Ronald McDonald