从Windows Server获取打印机列表

时间:2010-02-10 19:13:07

标签: windows printing autoit

我想查询以编程方式发布一组打印机的Windows服务器并找出

  • 远程计算机上可用的打印机(名称和说明),无需在本地安装打印机
  • 分别为打印机驱动程序的名称

我希望能够以某种脚本语言(如AutoIT)处理结果。我有哪些选择?

1 个答案:

答案 0 :(得分:2)

这应该有所帮助(尽管它是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

您可以在此page找到更多信息。

如果您真的想在AutoIt中使用它,可以试试这个forum thread,它碰巧有一些上述代码的翻译。

基本上,如果您在搜索字段的autoit论坛上输入'printers',您将获得大量有关它的信息。比我粘贴它好得多: - )

如评论中所述,如果您没有AD,则应使用Scriptomatic生成AutoIt WMI代码,例如:

; 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

WMI中有更多关于pritners的类:

Win32_PrinterDriver

Win32_PrinterDriverDll

Win32_PrinterSetting

Win32_PrinterShare

Win32_PrintJob

Win32_Printer

Win32_PrinterConfiguration

另一个例子:

; 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