Solved

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

Posted on 2012-04-11
2
484 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 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

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