; function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)

; OK testate prossime due righe
ChkAddPrinter("FILE:","Generic / Text Only","","generica",,0)   
ChkAddPrinter("192.168.0.100","KONICA MINOLTA C360SeriesPCL","K:\hardware\stampanti\fotocopiatrice-konica-bizhub220c.192.168.0.100\Drivers\PCL\IT\Win_x86\KOAZ8J__.INF","",,0)   
;ChkAddPrinter("LPT1:","HP LaserJet 2300L PCL 6","\\Serverone\manutenzione\hardware\stampanti\HP2300\PCL6\hpc2300c.inf","HP LaserJet 2300L PCL 6",,0)   

Function ChkAddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)
    
    $rval = AddPrinter($Port,$PrinterType,$DriverInf,$PrinterDesc, $remotepc,$Default)
    
    if $rval = 0
        $msg="Aggiunto stampante" + $PrinterType
    else
        $msg = "Errore:" + $rval " nell'aggiunta della stampante " + $PrinterType
    endif
    MESSAGEBOX  ($msg , "addprinter", 0, 10)
    exit $rval
endfunction


;Function:   
;    AddPrinter()   
;   
;Author:   
;    Allen Powell (Al_Po)   
;  
;Contributors:  
;    Glenn Barnas  
;   
;Version:   
;    1.2.0   
;   
;Version History   
;    1.2.0 2007/02/22 Added Support for LPR Printers, Removed Un-needed dimmed vars from previous versions  
;    1.1.1 2006/12/16 Fixed Code when adding network printers   
;    1.1.0 2006/11/17 Support for Novarsinstrings, All Vars Dimmed, Cleaned up code   
;    1.0.0 2003/05/07 Original   
;   
;Action:   
;    Add IP,LPT, or COM Printers locally or remotely.  Also, add PrintServer connections locally.   
;    (keywords:  Script, WMI, vbscript, batch, logon, login)  
;   
;Syntax:   
;    AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)   
;   
;Parameters:   
;       $Port: (required)     
;             IPAddress(RAW)   (xxx.xxx.xxx.xxx)   
;             IPAddress(LPR)   (xxx.xxx.xxx.xxx:LPRQueueName)  
;             LPT              (LPT1:)   
;             COM              (COM1:)   
;             PrintServer      (\\server\printer)   
;       $PrinterType: (required)   
;             Descriptor of Printer within INF file   
;             Note:  If using a PrintServer, $PrinterType value is ignored.   
;       $DriverInf: (Optional)   
;             Location and name of INF file containing print drivers   
;             Note:  If omitted, attempts to use default Windows Print Drivers   
;       $PrinterDesc: (Optional)   
;             Description of Printer   
;             Note:  If ommitted, $PrinterDesc = PrinterType   
;       $RemotePC: (Optional)   
;             PC to install Printer/Drivers on.  If omitted defaults to localpc.   
;              Note:  Ignored if $Port is a PrintServer   
;       $Default: (Optional)   
;             0 = Do not Set as Default Printer (Default)   
;             1 = Set as Default Printer   
;                         
;Returns:   
;        0 - Successful   
;        1 - Failed   
;        If Failed check @error   
;          or see below for custom errors   
;         -4 - Error connecting to RemotePC   
;         -3 - Could not open/find INF file   
;         -2 - Could not find $PrinterType in INF file     
;         -1 - Unrecognized Port   
;   
;Dependencies:   
;     Windows 2000 Professional and up   
;       rundll32 printui.dll,PrintUIEntry   
;   
;Examples:   
;$=AddPrinter("192.168.1.1","Xerox DocumentCenter 555 PCL6","\\Server\PrinterDrives\Xerox\dc555pcl\dc555x.inf","Xerox Document Centre 55  5 PCL6 in Office",REMOTEPC,1)   
;$=Addprinter("LPT1","HP LaserJet 8100 Series PS",,"HP8100 in IS(test)")   
;$=AddPrinter("192.168.1.1","HP LaserJet 8100 Series PS",,"HP8100 in IS(test)",,1)   
;$=AddPrinter('\\server\printer','_')   
;Source:   
 
 
 

function AddPrinter($Port,$PrinterType,optional $DriverInf,optional $PrinterDesc,optional $remotepc,optional $Default)
    Dim $found,$IPPrinterKey,$PrinterCMD,$ffh,$,$line,$objservice,$printserver,$LPRQueue
    $AddPrinter=1
    if $Driverinf=""
      $DriverInf="%windir%\inf\ntprint.inf"
    endif
    if exist($DriverInf)=0
      Exit -3
    endif
    shell '"%comspec%" /c type "' + $driverinf + '">"%temp%\addprinter.txt"'
    $ffh=freefilehandle()
    if open($ffh,"%temp%\addprinter.txt",0)=0
      $found=0
      $line=readline($ffh)      
      while @error=0 and $found=0
        $found=instr($line,$PrinterType)
        $line=readline($ffh)
      loop
      $=close($ffh)
      if $found=0
        exit -2
      else
        if $PrinterDesc=""
          $PrinterDesc=$PrinterType
        endif
        if $remotepc=""
          $remotepc='\\'+ @wksta
        else 
          if left($remotepc,2)<>"\\"
            $remotepc='\\' + $remotepc
          endif
        endif
        select 
          case left($Port,2)="\\"
            $printserver=1
            $printerdesc=$port
          case instr($Port,".")
            if instr($Port,":")
              $LPRQueue=split($port,":")[1]
              $Port=split($port,":")[0]
            endif
            $IPPrinterKey = $remotepc + '\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_' + $Port
            if keyexist($IPPrinterKey)=0
              If $LPRQueue ; LPR  
                if WriteValue($IPPrinterKey,"Protocol","00000002","REG_DWORD") exit @error endif
                if WriteValue($IPPrinterKey,"Version","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"Double Spool","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"Queue",$LPRQueue, "REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"HostName","", "REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"IPAddress",$Port,"REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"HWAddress","","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"PortNumber","00000203","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Community","public","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Enabled","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Index","00000001","REG_DWORD")  exit @error endif
              Else ; RAW  
                if WriteValue($IPPrinterKey,"Protocol","00000001","REG_DWORD") exit @error endif
                if WriteValue($IPPrinterKey,"Version","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"HostName","", "REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"IPAddress",$Port,"REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"HWAddress","","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"PortNumber","00009100","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Community","public","REG_SZ")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Enabled","00000001","REG_DWORD")  exit @error endif
                if WriteValue($IPPrinterKey,"SNMP Index","00000001","REG_DWORD")  exit @error endif
              endif
            endif
            $objservice=Getobject("WinNT://" + substr($remotepc,3) + "/Spooler,Service")
            if @error
              exit -4
            endif
            $objservice.stop
            do
              sleep 0.2
            until $objservice.status=1   ;stopped   
            $objservice.start
            $port='IP_' + $port
          case ucase(left($Port,3))="LPT" or ucase(left($Port,3))="COM"
            if right($port,1)<>":"
              $Port=$port+":"
            endif
            if val(left(right($port,2),1))=0
              exit -1 
            endif
          case ucase(left($Port,4))="FILE"
            if right($port,1)<>":"
              $Port=$port+":"
            endif
          case 1
            exit -1
        endselect
        if $Printserver
          if addprinterconnection($port)
            exit @error    
          endif
        else
          $printercmd='rundll32 printui.dll,PrintUIEntry /if /b "' + $printerdesc + '" /c "' + $remotepc + '" ' + 
                      '/f "' + $driverinf + '" /r "' + $port + '" /m "' + $Printertype + '" /z /u '
          Shell $printercmd
        endif
        if $default
          if SetDefaultPrinter($PrinterDesc)
            exit @error
          endif
        endif 
        $AddPrinter=0
      endif
    else
      Exit -3
    endif
endfunction