?
Solved

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

Posted on 2012-04-11
2
Medium Priority
?
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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