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!

How do I search on GUID? 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
We have just implemented AD and I have written a routine (in vba) that retrieves all of the users. For several reasons, we need to dump the information to a SQL table. This SQL table will not only be populated via AD but will also update AD with info. Consequently, to do a lookup, I need to use the objectGUID. But I don't know how to query AD by the objectGUID. My understanding is that objectGUID is a 16 byte array. I have attempted to convert the objectGUID to a string and search that way, but with no luck. Here's an example of my query which returns 0 records.

Note that objectGUID returns the following values:
202, 194, 223, 192, 158, 46, 91, 74, 129, 163, 225, 249, 165, 161, 234, 195

Some code I got off the internet converted this array to this string: {192223194202-46158-7491-163129-225249165161234195}

What am I doing wrong?

Code:
    Dim objConnection As Object
    Dim objCommand As Object
    Dim objRecordSet As Object
    
    On Error GoTo ErrHandler
    
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Provider = ("ADsDSOObject")
    objConnection.Open

    Set objCommand = CreateObject("ADODB.Command")

    objCommand.ActiveConnection = objConnection
    objCommand.Properties("Page Size") = 2000
    
    objCommand.CommandText = "SELECT displayName, distinguishedName, sAMAccountName, Department, Company, TelephoneNumber,L,Mail,Manager,objectGUID FROM 'LDAP://ou=MyCompany Users," & RootDSE & "' Where objectCategory='User' and displayName='*' and GUID = '{192223194202-46158-7491-163129-225249165161234195}'"
    
    Set objRecordSet = objCommand.Execute
 
Try this.
[tt]
'this is your input
aguid=array(202, 194, 223, 192, 158, 46, 91, 74, 129, 163, 225, 249, 165, 161, 234, 195)
xguid=""
for i=0 to ubound(aguid)
xguid=xguid & "\" & hex(aguid(i))
next
[/tt]
Then replace the command by this.

[tt] objCommand.CommandText = "SELECT displayName, distinguishedName, sAMAccountName, Department, Company, TelephoneNumber,L,Mail,Manager,objectGUID FROM 'LDAP://ou=MyCompany Users," & RootDSE & "' Where objectCategory='User' and displayName='*' and GUID = '[blue]" & xguid & "[/blue]'"
[/tt]
- tsuji
 
Worked like a charm. Thanks alot. I spent a couple hours yesterday searching the internet for a solution. Everything pointed to a string with dashes. Only saw one that referenced the format you suggested. Don't know why I didn't try it (maybe it was late in the day). Anyway, thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top