Link to home
Start Free TrialLog in
Avatar of supportmail
supportmailFlag for United States of America

asked on

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

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!
ASKER CERTIFIED SOLUTION
Avatar of kemot1000
kemot1000
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of supportmail

ASKER

Thanks kemot1000!