;Region Check printer logfile
;Set printer log file name
$printerlog = "C:\somefolder\Printers.ini"
;Check if the printer log file exists.
If Exist ($printerlog)
;Delete old printer log file.
Del $printerlog
;Create printer log file.
$rc = CreateFile($printerlog)
Else
;Create printer log file.
$rc = CreateFile($printerlog)
EndIf
;endregion
;Region Set variables
;Set name of the printerserver
$printerserver = "Server"
;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 writiine to the printer log file.
$oldprinter = $oldprinter + "~" + $value1
Next
;Write all old printers to the printer log file.
$rc = WriteProfileString($printerlog, "Printers", "OldPrinters", $oldprinter)
$currentprinters = Split($oldprinter, "~")
;endregion
;Region Add printers
;Set printer name.
$printer = "\\" + $printerserver + "\Printer"
;Check group membership.
If InGroup("SomeName-Default") Or InGroup("SomeName-Extra")
;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
;Region Set default printer
;Check group membership.
If InGroup("SomeName-Default")
$printer = "\\" + $printerserver + "\Printer"
;Check if the printer is already set as default.
If PriMapState($printer) <> "2"
;Set default printer.
$rc = SetDefaultPrinter($printer)
EndIf
EndIf
;Region Clean 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