views:

337

answers:

1

I'd like to query a Windows server that is publishing a set of printers programatically and find out

  • what printers are available on the remote machine (name and description) without installing the printers locally
  • the name of the printer driver respectively

I'd like to be able to process the result in some scripting language like AutoIT. Which options do I have?

+1  A: 

Hello,

This should help (although it's VBS):

Const ADS_SCOPE_SUBTREE = 2

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 from " _     
& " 'LDAP://DC=fabrikam,DC=com'  where objectClass='printQueue'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
Wscript.Echo "Printer Name: " & objRecordSet.Fields("printerName").Value
Wscript.Echo "Server Name: " & objRecordSet.Fields("serverName").Value
objRecordSet.MoveNext
Loop

More information you can find on this page.

If you really want it in AutoIt you can try this forum thread which happens to have some translation of the code above.

Basically if you type in 'printers' on autoit forum in search field you will get lots of information about it. Far better then me pasting it here :-)

As mentioned in comment if you have no AD you should use Scriptomatic to generate AutoIt WMI code for example:

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PrinterDriver", "WQL", _
                                      $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
  $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
  $Output = $Output & "ConfigFile: " & $objItem.ConfigFile & @CRLF
  $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
  $Output = $Output & "DataFile: " & $objItem.DataFile & @CRLF
  $Output = $Output & "DefaultDataType: " & $objItem.DefaultDataType & @CRLF
  $strDependentFiles = $objItem.DependentFiles(0)
  $Output = $Output & "DependentFiles: " & $strDependentFiles & @CRLF
  $Output = $Output & "Description: " & $objItem.Description & @CRLF
  $Output = $Output & "DriverPath: " & $objItem.DriverPath & @CRLF
  $Output = $Output & "FilePath: " & $objItem.FilePath & @CRLF
  $Output = $Output & "HelpFile: " & $objItem.HelpFile & @CRLF
  $Output = $Output & "InfName: " & $objItem.InfName & @CRLF
  $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
  $Output = $Output & "MonitorName: " & $objItem.MonitorName & @CRLF
  $Output = $Output & "Name: " & $objItem.Name & @CRLF
  $Output = $Output & "OEMUrl: " & $objItem.OEMUrl & @CRLF
  $Output = $Output & "Started: " & $objItem.Started & @CRLF
  $Output = $Output & "StartMode: " & $objItem.StartMode & @CRLF
  $Output = $Output & "Status: " & $objItem.Status & @CRLF
  $Output = $Output & "SupportedPlatform: " & $objItem.SupportedPlatform & @CRLF
  $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
  $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
  $Output = $Output & "Version: " & $objItem.Version & @CRLF
  if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
  $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PrinterDriver" )
Endif


Func WMIDateStringToDate($dtmDate)

Return (StringMid($dtmDate, 5, 2) & "/" & _
StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

There's lot more classes in WMI for pritners:

Win32_PrinterDriver

Win32_PrinterDriverDll

Win32_PrinterSetting

Win32_PrinterShare

Win32_PrintJob

Win32_Printer

Win32_PrinterConfiguration

Another example:

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer", "WQL", _
                                      $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
  $Output = $Output & "Attributes: " & $objItem.Attributes & @CRLF
  $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
  $strAvailableJobSheets = $objItem.AvailableJobSheets(0)
  $Output = $Output & "AvailableJobSheets: " & $strAvailableJobSheets & @CRLF
  $Output = $Output & "AveragePagesPerMinute: " & $objItem.AveragePagesPerMinute & @CRLF
  $strCapabilities = $objItem.Capabilities(0)
  $Output = $Output & "Capabilities: " & $strCapabilities & @CRLF
  $strCapabilityDescriptions = $objItem.CapabilityDescriptions(0)
  $Output = $Output & "CapabilityDescriptions: " & $strCapabilityDescriptions & @CRLF
  $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
  $strCharSetsSupported = $objItem.CharSetsSupported(0)
  $Output = $Output & "CharSetsSupported: " & $strCharSetsSupported & @CRLF
  $Output = $Output & "Comment: " & $objItem.Comment & @CRLF
  $Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF
  $Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF
  $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
  $strCurrentCapabilities = $objItem.CurrentCapabilities(0)
  $Output = $Output & "CurrentCapabilities: " & $strCurrentCapabilities & @CRLF
  $Output = $Output & "CurrentCharSet: " & $objItem.CurrentCharSet & @CRLF
  $Output = $Output & "CurrentLanguage: " & $objItem.CurrentLanguage & @CRLF
  $Output = $Output & "CurrentMimeType: " & $objItem.CurrentMimeType & @CRLF
  $Output = $Output & "CurrentNaturalLanguage: " & $objItem.CurrentNaturalLanguage & @CRLF
  $Output = $Output & "CurrentPaperType: " & $objItem.CurrentPaperType & @CRLF
  $Output = $Output & "Default: " & $objItem.Default & @CRLF
  $strDefaultCapabilities = $objItem.DefaultCapabilities(0)
  $Output = $Output & "DefaultCapabilities: " & $strDefaultCapabilities & @CRLF
  $Output = $Output & "DefaultCopies: " & $objItem.DefaultCopies & @CRLF
  $Output = $Output & "DefaultLanguage: " & $objItem.DefaultLanguage & @CRLF
  $Output = $Output & "DefaultMimeType: " & $objItem.DefaultMimeType & @CRLF
  $Output = $Output & "DefaultNumberUp: " & $objItem.DefaultNumberUp & @CRLF
  $Output = $Output & "DefaultPaperType: " & $objItem.DefaultPaperType & @CRLF
  $Output = $Output & "DefaultPriority: " & $objItem.DefaultPriority & @CRLF
  $Output = $Output & "Description: " & $objItem.Description & @CRLF
  $Output = $Output & "DetectedErrorState: " & $objItem.DetectedErrorState & @CRLF
  $Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
  $Output = $Output & "Direct: " & $objItem.Direct & @CRLF
  $Output = $Output & "DoCompleteFirst: " & $objItem.DoCompleteFirst & @CRLF
  $Output = $Output & "DriverName: " & $objItem.DriverName & @CRLF
  $Output = $Output & "EnableBIDI: " & $objItem.EnableBIDI & @CRLF
  $Output = $Output & "EnableDevQueryPrint: " & $objItem.EnableDevQueryPrint & @CRLF
  $Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF
  $Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF
  $strErrorInformation = $objItem.ErrorInformation(0)
  $Output = $Output & "ErrorInformation: " & $strErrorInformation & @CRLF
  $Output = $Output & "ExtendedDetectedErrorState: " & $objItem.ExtendedDetectedErrorState & @CRLF
  $Output = $Output & "ExtendedPrinterStatus: " & $objItem.ExtendedPrinterStatus & @CRLF
  $Output = $Output & "Hidden: " & $objItem.Hidden & @CRLF
  $Output = $Output & "HorizontalResolution: " & $objItem.HorizontalResolution & @CRLF
  $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
  $Output = $Output & "JobCountSinceLastReset: " & $objItem.JobCountSinceLastReset & @CRLF
  $Output = $Output & "KeepPrintedJobs: " & $objItem.KeepPrintedJobs & @CRLF
  $strLanguagesSupported = $objItem.LanguagesSupported(0)
  $Output = $Output & "LanguagesSupported: " & $strLanguagesSupported & @CRLF
  $Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF
  $Output = $Output & "Local: " & $objItem.Local & @CRLF
  $Output = $Output & "Location: " & $objItem.Location & @CRLF
  $Output = $Output & "MarkingTechnology: " & $objItem.MarkingTechnology & @CRLF
  $Output = $Output & "MaxCopies: " & $objItem.MaxCopies & @CRLF
  $Output = $Output & "MaxNumberUp: " & $objItem.MaxNumberUp & @CRLF
  $Output = $Output & "MaxSizeSupported: " & $objItem.MaxSizeSupported & @CRLF
  $strMimeTypesSupported = $objItem.MimeTypesSupported(0)
  $Output = $Output & "MimeTypesSupported: " & $strMimeTypesSupported & @CRLF
  $Output = $Output & "Name: " & $objItem.Name & @CRLF
  $strNaturalLanguagesSupported = $objItem.NaturalLanguagesSupported(0)
  $Output = $Output & "NaturalLanguagesSupported: " & $strNaturalLanguagesSupported & @CRLF
  $Output = $Output & "Network: " & $objItem.Network & @CRLF
  $strPaperSizesSupported = $objItem.PaperSizesSupported(0)
  $Output = $Output & "PaperSizesSupported: " & $strPaperSizesSupported & @CRLF
  $strPaperTypesAvailable = $objItem.PaperTypesAvailable(0)
  $Output = $Output & "PaperTypesAvailable: " & $strPaperTypesAvailable & @CRLF
  $Output = $Output & "Parameters: " & $objItem.Parameters & @CRLF
  $Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF
  $Output = $Output & "PortName: " & $objItem.PortName & @CRLF
  $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities(0)
  $Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
  $Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
  $strPrinterPaperNames = $objItem.PrinterPaperNames(0)
  $Output = $Output & "PrinterPaperNames: " & $strPrinterPaperNames & @CRLF
  $Output = $Output & "PrinterState: " & $objItem.PrinterState & @CRLF
  $Output = $Output & "PrinterStatus: " & $objItem.PrinterStatus & @CRLF
  $Output = $Output & "PrintJobDataType: " & $objItem.PrintJobDataType & @CRLF
  $Output = $Output & "PrintProcessor: " & $objItem.PrintProcessor & @CRLF
  $Output = $Output & "Priority: " & $objItem.Priority & @CRLF
  $Output = $Output & "Published: " & $objItem.Published & @CRLF
  $Output = $Output & "Queued: " & $objItem.Queued & @CRLF
  $Output = $Output & "RawOnly: " & $objItem.RawOnly & @CRLF
  $Output = $Output & "SeparatorFile: " & $objItem.SeparatorFile & @CRLF
  $Output = $Output & "ServerName: " & $objItem.ServerName & @CRLF
  $Output = $Output & "Shared: " & $objItem.Shared & @CRLF
  $Output = $Output & "ShareName: " & $objItem.ShareName & @CRLF
  $Output = $Output & "SpoolEnabled: " & $objItem.SpoolEnabled & @CRLF
  $Output = $Output & "StartTime: " & WMIDateStringToDate($objItem.StartTime) & @CRLF
  $Output = $Output & "Status: " & $objItem.Status & @CRLF
  $Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF
  $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
  $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
  $Output = $Output & "TimeOfLastReset: " & WMIDateStringToDate($objItem.TimeOfLastReset) & @CRLF
  $Output = $Output & "UntilTime: " & WMIDateStringToDate($objItem.UntilTime) & @CRLF
  $Output = $Output & "VerticalResolution: " & $objItem.VerticalResolution & @CRLF
  $Output = $Output & "WorkOffline: " & $objItem.WorkOffline & @CRLF
  if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
  $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Printer" )
Endif
MadBoy
I'm not sure whether the printers are published to AD (seems to be a prerequisite), but I'll give it a try.
vwegert
If they are not published in AD you can do it using WMI, on AutoIt v3 forum there's Scriptomatic tool (translation of Microsoft Scriptomatic which produces VBS) which produces AutoIt code. You just need to find class to list printers.
MadBoy