Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBScript: Search AD For Specific Printer By Host Name (Quotes Issue?)

Posted on 2012-04-11
2
Medium Priority
?
491 Views
Last Modified: 2012-04-11
I have a script that I modified to search AD for a specific printer and then return values. I can get the script to work if I place in the script the specific name of the printer and execute the script but if I want to use a variable with an input box it does not seem to work. I am wondering if it has to do with quotes (single/double). Can anyone provide some insight?

Here is the code that doesnt work.
'==========================================================================
'
' NAME: VB-Search.vbs
'
' REVISION:
'
' AUTHOR:
'
' DATE  : 4/9/2012 09:37:11
'
' COMMENT:
'       Search AD for a specific printer.
'       
'
'==========================================================================
Dim strPrinter
Const ADS_SCOPE_SUBTREE = 2

strPrinter = InputBox("Type the printer name", strPrinter)

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 printerName, serverName, driverName, location, printColor from " & " 'LDAP://DC=Domain,DC=COM'  where objectClass='printQueue' AND printerName=' & strPrinter & '"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    WScript.Echo("Printer Location: " & objRecordSet.Fields("location").Value)
    WScript.Echo("Server Name: " & objRecordSet.Fields("serverName").Value)
    WScript.Echo("Printer Name: " & objRecordSet.Fields("printerName").Value)
    WScript.Echo("Printer Driver: " & objRecordSet.Fields("driverName").Value)
    WScript.Echo("Color Printer: " & objRecordSet.Fields("printColor").Value)
    objRecordSet.MoveNext
Loop
'==========================================================================







Code that does work!
'==========================================================================
'
' NAME: VB-PrinterSearch.vbs
'
' REVISION:
'
' AUTHOR:
'
' DATE  : 4/9/2012 09:37:11
'
' COMMENT:
'       Search for a specific printer in AD.
'       
'
'==========================================================================

Dim strPrinter
Const ADS_SCOPE_SUBTREE = 2

'strPrinter = InputBox("Type the printer name", strPrinter)

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 printerName, serverName, driverName, location, printColor from " & " 'LDAP://DC=DC,DC=COM'  where objectClass='printQueue' AND printerName='SpecificPrinterName'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    WScript.Echo("Printer Location: " & objRecordSet.Fields("location").Value)
    WScript.Echo("Server Name: " & objRecordSet.Fields("serverName").Value)
    WScript.Echo("Printer Name: " & objRecordSet.Fields("printerName").Value)
    WScript.Echo("Printer Driver: " & objRecordSet.Fields("driverName").Value)
    WScript.Echo("Color Printer: " & objRecordSet.Fields("printColor").Value)
    objRecordSet.MoveNext
Loop
'==========================================================================

Any help is greatly apprecaited!
0
Comment
Question by:supportmail
2 Comments
 
LVL 4

Accepted Solution

by:
kemot1000 earned 2000 total points
ID: 37834805
try this:

objCommand.CommandText = "Select printerName, serverName, driverName, location, printColor from " & " 'LDAP://DC=DC,DC=COM'  where objectClass='printQueue' AND printerName='" & strPrinter & "'"

Open in new window

0
 

Author Closing Comment

by:supportmail
ID: 37834903
Thanks kemot1000!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question