WQL Query with LIKE statement against Active Directory

Discussion in 'Windows Server' started by GerardSamuel, Sep 11, 2008.

  1. GerardSamuel

    GerardSamuel Guest

    If this topic is not in the right newsgroup, could you please suggest where
    else I can post. Thanks

    I'm attempting to put together a VBScript that searches Active Directory for
    a specific group of computer objects.
    I found this script, that I'm trying to modify so it my needs.
    In the script, if I use the LIKE statement to find any computer objects that
    start with BBX, the script fails when it trys to execute with ->
    C:\temp\z.vbs(13, 1) Provider: One or more errors occurred during processing
    of command.

    Does anyone have any idea why I'm getting this error, and how to fix, or can
    suggest another way for me to retrieve a group of computer objects that
    starts with a common characters with VBScript/WMI.




    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 Name from 'LDAP://DC=bbx,DC=com' where Name
    LIKE 'BBX%' AND objectClass='computer'"

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

    Do Until objRecordSet.EOF
    Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
    GerardSamuel, Sep 11, 2008
    1. Advertisements

  2. Your script doesn't use WMI (or WQL). I cannot find confirmation, but I
    doubt AD supports the LIKE operator. I don't recall ever seeing it used to
    query AD. You can, however, use wildcards. For example:

    objCommand.CommandText = "SELECT Name FROM 'LDAP://DC=bbx,DC=com' " _
    & "WHERE Name='BBX*' AND objectCategory='computer'"

    All SQL syntax queries (like yours) are converted to LDAP syntax by the
    provider, so it would make more sense to use:

    objCommand.CommandText =

    Notice that I use objectCategory instead of objectClass, because that
    attribute is indexed and single valued.
    Richard Mueller [MVP], Sep 11, 2008
    1. Advertisements

  3. GerardSamuel

    GerardSamuel Guest

    Your suggestion worked out great.
    Thanks for the tip
    GerardSamuel, Sep 11, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.