Solved

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

Posted on 2012-04-11
2
479 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 500 total points
Comment Utility
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
Comment Utility
Thanks kemot1000!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now