WQL Query with LIKE statement against Active Directory

  • Thread starter Thread starter GerardSamuel
  • Start date Start date
G

GerardSamuel

Guest
Hello,
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.

Thanks

-----

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.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
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
Loop
 
Re: WQL Query with LIKE statement against Active Directory


"GerardSamuel" <GerardSamuel@community.nospam> wrote in message
news:OITU%23fBFJHA.768@TK2MSFTNGP05.phx.gbl...
> Hello,
> 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.
>
> Thanks
>
> -----
>
> 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.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
> objRecordSet.MoveFirst
>
> Do Until objRecordSet.EOF
> Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
> objRecordSet.MoveNext
> Loop


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 =
"<LDAP://dc=bbx,dc=com>;(&(objectCategory=computer)(Name=BBX*));Name;subtree"

Notice that I use objectCategory instead of objectClass, because that
attribute is indexed and single valued.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
 
Re: WQL Query with LIKE statement against Active Directory

"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:%23pzCy2BFJHA.3840@TK2MSFTNGP05.phx.gbl...
>
> "GerardSamuel" <GerardSamuel@community.nospam> wrote in message
> news:OITU%23fBFJHA.768@TK2MSFTNGP05.phx.gbl...
>> Hello,
>> 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.
>>
>> Thanks
>>
>> -----
>>
>> 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.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
>> objRecordSet.MoveFirst
>>
>> Do Until objRecordSet.EOF
>> Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
>> objRecordSet.MoveNext
>> Loop

>
> 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 =
> "<LDAP://dc=bbx,dc=com>;(&(objectCategory=computer)(Name=BBX*));Name;subtree"
>
> Notice that I use objectCategory instead of objectClass, because that
> attribute is indexed and single valued.
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>



Your suggestion worked out great.
Thanks for the tip
 
Back
Top