Break on
$printerlog = "c:\somefolder\printers.ini"
;Region Get all installed network printers.
;Set the key that should be enumerated.
$key = "HKCU\Printers\Connections"
;Enumerate network printers key.
$oldprinters = ArrayEnumKey($key)
;Do stuff for each value
For Each $value in $oldprinters
;Split the value on the comma.
$value = Split($value, ",")
;Put the value back together so that is represents the correct path to the printer.
$value1 = "\\" + $value[2] + "\" + $value[3]
;Add the created value to a variable to be written to the printer log file.
$oldprinter = $oldprinter + "~" + $value1
Next
;Write all old printers to the printer log file.
$rc = WriteProfileString($printerlog, "Printers", "OldPrinters", $oldprinter)
;endregion
;Region Add printers
;Map printer printer01
;Set printer name.
$printer = "\\Server01\printer01"
;Check group membership.
If InGroup("SomeGroup01")
;Check if the printer is already installed.
If Not PriMapState($printer)
;Printer is not installed. Add printer.
$rc = AddPrinterConnection($printer)
EndIf
;Read all so far installed printers from the printer log file.
$connected = ReadProfileString($printerlog, "Printers", "NewPrinters")
;Add the printer name to a variable to be written to the printer log file.
$connected = $connected + "~" + $printer
;Write the new printers value to the printers log file.
$rc = WriteProfileString($printerlog, "Printers", "NewPrinters", $connected)
EndIf
;Set printer name.
;Map printer printer02
$printer = "\\Server01\printer02"
;Check group membership.
If InGroup("SomeGroup02")
;Check if the printer is already installed.
If Not PriMapState($printer)
;Printer is not installed. Add printer.
$rc = AddPrinterConnection($printer)
EndIf
;Read all so far installed printers from the printer log file.
$connected = ReadProfileString($printerlog, "Printers", "NewPrinters")
;Add the printer name to a variable to be written to the printer log file.
$connected = $connected + "~" + $printer
;Write the new printers value to the printers log file.
$rc = WriteProfileString($printerlog, "Printers", "NewPrinters", $connected)
EndIf
;endregion
;Region Delete unneeded printers
;Read all old network printers from the printer log file.
$oldprinters = ReadProfileString($printerlog, "Printers", "OldPrinters")
;Check if there are any old printers in the list.
If $oldprinters <> ""
;Split the old printers on the ~ character
$oldprinters = Split($oldprinters, "~")
EndIf
;Check each element of the array.
For $i = 0 to UBound($oldprinters)
;Remove extra ~ characters.
$oldprinters[$i] = Join(Split($oldprinters[$i], "~"), "")
Next
;Read all new printers from the printer log file.
$newprinters = ReadProfileString($printerlog, "Printers", "NewPrinters")
;Check if there are any new printers in the list.
If $newprinters <> ""
;Split the new printers on the ~ character
$newprinters = Split($newprinters, "~")
EndIf
;Check each element of the array.
For $i = 0 to UBound($newprinters)
;Remove extra ~ characters.
$newprinters[$i] = Join(Split($newprinters[$i], "~"), "")
Next
;Compare the list of old and new printers.
$tobedeletedprinters = ArrayComp($newprinters, $oldprinters)
;Check if there are any printers that should be deleted.
If UBound($tobedeletedprinters) > -1
For Each $tobedeletedprinter in $tobedeletedprinters
;Delete each printer that should be deleted.
$rc = DelPrinterConnection($tobedeletedprinter)
Next
EndIf
;endregion