join computer to AD and move
- From: wcasse@xxxxxxxxx
- Date: Fri, 24 Aug 2007 11:14:36 -0700
i'm writing a script to join computers to the domain and move them to
the correct container. it works properly provided the computer name
does not already exist in AD. but if the computer account already
exists in AD then the computer is not moved to the correct container.
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!
\\" _
& strHostName & "\root\cimv2:Win32_ComputerSystem.Name='" &
strHostName & "'")
Return = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword,
strUserID, strOU, 3)
If Return = 2224 Then 'The computer account already exists in the
domain
Return = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword,
strUserID, strOU, 1)
End If
i'm thinking that i will need to move the account after the join, but
i will not know what container the computer is in. i would normally
use code like below to search AD, but it's not working (tenuous
connection to AD or domain credentials not passed?).
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
strFilter = "(&(objectCategory=computer)(objectClass=computer)(Name="
& strHostName & "))"
strQuery = "<LDAP://DC=mydomain,DC=com>;" & strFilter &
";distinguishedName;subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
'errors on the above line "table does not exist"
strSource = "LDAP://" & objRecordSet.Fields("distinguishedName")
set objDestOU = oLDAP.OpenDSObject(strOU,strUserID,strPassword,1)
objDestOU.MoveHere strSource, "CN=" & strHostName
any help would be appreciated. thanks!
.
- Follow-Ups:
- RE: join computer to AD and move
- From: T_Condit
- RE: join computer to AD and move
- Prev by Date: Access Denied
- Next by Date: RE: join computer to AD and move
- Previous by thread: Access Denied
- Next by thread: RE: join computer to AD and move
- Index(es):