Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Domain for the current PC to determine its owing OU(Domain)

Status
Not open for further replies.

Pickachu

Programmer
May 17, 2007
7
GB
I have the following code to search the domain called mx for the current computer name, I seem to be having problems with the syntax at the line indicated by the * it gives me an error saying "The search filter cannot be recognised" can anyone see what i have done wrong? Also, it seems to be taking an age to exit the DO WHILE LOOP, as soon as it finds one record i want it to exit as it will only ever find one record with a matching name! Right?

Please help!
___________________________________________________________


Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://dc=MX,dc=int' WHERE objectCategory='computer' and name = '" & strComputer & "'"

Set objRecordSet = objCommand.Execute

*objRecordSet.MoveFirst

Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
objRecordSet.MoveNext
Loop

Set objComputer = GetObject("LDAP://" & strDN)

objComputer.Put "Description" , strMAC & "," & strIP & "," & strSTN
objComputer.SetInfo
 
Firstly apologies as i posted the wrong code, the code i posted was an earlier version, the actual code i have working (in a fashion is below, it runs without any syntax errors but it doesnt seem to be giving me the desired results as described in my earlier text.
________________________________________________________

Public strComputer, strDomain, wshNet, strRecord

Set wshNet = CreateObject("WScript.Network")
strDomain = wshNet.UserDomain
strComputer = wshNet.ComputerName
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 5000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

On Error Resume Next
objCommand.CommandText = _
"Select ADsPath AS [RecordCount] From 'LDAP://dc="& strDomain' Where objectCategory='Computer' " & "And Name=' " & strComputer & "'"
objRecordSet.MoveFirst

MsgBox RecordCount
MsgBox "Record Found
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top