;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       PrinterList() 
;; 
;;ACTION         Creates an array/list of Printers, and optionally their ports installed, 
;;               on a local or remote computer.   
;; 
;;AUTHOR         Allen Powell  
;; 
;;VERSION        1.3.1 - 2006/05/10 
;; 
;;HISTORY        1.0.0 - 2003/06/26 - Original 
;;               1.1.0 - 2004/08/09 - undimmed variable fix 
;;               1.2.0 - 2005/07/19 - optimized code 
;;               1.3.0 - 2005/10/21 - added options to distinguish local or remote printers 
;;               1.3.1 - 2006/05/10 - fixed error detecting Print Servers sharing IP Printers 
;; 
;;SYNTAX         Printerlist([remotepc] [, displaymode])  
;; 
;;PARAMETERS     RemotePC - OPTIONAL - String 
;;               - Remote Computer name. If omitted defaults to local computer. 
;; 
;;               displaymode - OPTIONAL - Integer 
;;               - Values that alter the returned data: 
;;               0 - show all printers, don't display port info (Default)   
;;               1 - show all printers, display port info   
;;               2 - show local printers, don't display port info   
;;               3 - show local printers, display port info   
;;               4 - show remote printers, don't display port info   
;;               5 - show remote printers, display port info  
;; 
;;REMARKS         
;; 
;;RETURNS        Array of Printers 
;; 
;;DEPENDENCIES   WMI 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12 
;; 
;;EXAMPLES       $aPrinters = printerlist() 
;;               For Each $Printer In $aPrinters 
;;                 $Printer ? 
;;               Next 
;   
Function PrinterList(optional $remotepc, optional $displaymode)
  dim $service,$printer,$printers,$printerdesc[0],$counter,$portname,$printername
  if $remotepc=""
    $remotepc="."
  endif
  $Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2")
  if @error
    exit @error
  endif
  $Printers=$service.execquery ('select * from Win32_Printer')
  for each $printer in $printers
    redim preserve $printerdesc[$counter]
    if $displaymode & 1
      $portname = "," + $printer.portname
    endif
    select
      case $displaymode & 4 ;remote printers  
        if left($printer.portname,2)="\\" or left($printer.name,2)=="\\"
          $printername=$printer.name
        endif
      case $displaymode & 2 ;local printers  
        if left($printer.portname,2)<>"\\" and left($printer.name,2)<>"\\"
          $printername=$printer.name
        endif
      case 1 ; all printers  
        $printername=$printer.name
    endselect
    if $printername<>""
      $printerdesc[$counter]=$printername + $portname
      $counter=$counter + 1
      $printername=""
    endif   
  next
  $PrinterList=$printerdesc
endfunction