tis-printer-import

20.1.1-6
Printer Import - To prepare this package, just run update-package, the package will suggest you a recommended process. You can run this update-package from the waptconsole if running as local administrator
0 downloads
Download
View on luti logo
tis-printer-import icon

  Description 

  • package : tis-printer-import
  • version : 20.1.1-6
  • architecture : all
  • categories : System and network,Drivers,Configuration
  • maintainer : WAPT Team,Tranquil IT,Simon FONTENEAU,Jimmy PELÉ
  • description : Printer Import - To prepare this package, just run update-package, the package will suggest you a recommended process. You can run this update-package from the waptconsole if running as local administrator
  • locale : all
  • target_os : windows(>=10.0)
  • min_wapt_version : 2.2
  • sources :
  • installed_size :
  • impacted_process :
  • description_fr :
  • description_pl :
  • description_de :
  • description_es :
  • description_pt :
  • description_it :
  • description_nl :
  • description_ru :
  • editor :
  • licence :
  • signature_date : 2022-05-23T22:03:51.011694

  Setup.py 

# -*- coding: utf-8 -*-
from setuphelpers import *
import waptguihelper
import json
import sys
import win32con

r"""
Usable WAPT package functions: install(), uninstall(), session_setup(), audit(), update_package()

https://docs.microsoft.com/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/jj134237(v=ws.11)


PrintBrm -B|R|Q [-S <server>] -F <file> [-D <directory>] [-O FORCE] [-P ALL|ORIG] [-NOBIN] [-LPR2TCP] [-C <config file>] [-NOACL] [-?]
-B               Backup the server to the specified file
-R               Restore the configuration in the file to the server
-Q               Query the server or the backup file
-S <server name> Target server
-F <file name>   Target backup File
-D <directory>   Unpack the backup file to (with -R) or repack a backup file from (with -B) the given directory
-O FORCE         Force overwriting of existing objects
-P ALL|ORIG      Publish all printers in directory, or publish printers that were published originally
-NOBIN           Omit the binaries from the backup
-LPR2TCP         Convert LPR ports to Standard TCP/IP ports on restore
-C <file name>   Use the specified configuration file for BRM
-NOACL           Remove ACLs from print queues on restore
-?               Display this help

https://docs.microsoft.com/powershell/module/printmanagement/get-printer

Name                           ComputerName    Type         DriverName                PortName        Shared   Published  DeviceType
----                           ------------    ----         ----------                --------        ------   ---------  ----------
Printer1                                       Local        HP Universal Printing PC… IP_192.168.0.1  False    False      Print
Microsoft Print to PDF                         Local        Microsoft Print To PDF    PORTPROMPT:     False    False      Print

To prepare this package, just run update-package, the package will suggest you a recommended process. You can run this update-package from the waptconsole if running as local administrator


"""
# Declaring global variables - Warnings: 1) WAPT context is only available in package functions; 2) Global variables are not persistent between calls
printbrm_path = makepath("%windir%", r"System32\spool\tools\PrintBrm.exe")
windows_temp_dir = makepath(systemdrive, "Windows", "Temp")
printers_export_file = "export.printerExport"
printers_export_path = makepath(windows_temp_dir, printers_export_file)
printers_backup_file = "backup.printerExport"
printers_backup_path = makepath(windows_temp_dir, printers_backup_file)
printers_manual_backup_file = "manual_backup.printerExport"
printers_manual_backup_path = makepath(windows_temp_dir, printers_manual_backup_file)
to_import_printers_list = ["Printer1"]  # To fill manually if you want the package to being able to uninstall imported printers
force_config = True  # Forces overwriting of existing objects.


def install():
    # Force config is requested
    if force_config:
        printbrm_option = "-O FORCE"
    else:
        printbrm_option = ""

    # Installing printers with drivers and configuration
    print(run(r'"%s" -r -f %s %s' % (printbrm_path, printers_export_file, printbrm_option)))


def uninstall():
    for printer_name in to_import_printers_list:
        remove_printer(printer_name)


def update_package():
    # Admins only
    if not running_as_admin():
        error("This update_package must be run as local administrator")

    # When to backup
    console_warning = messagebox(
        control.package,
        "Do you want to use the backup before Print Management (printmanagement.msc)? By pressing no the backup will just occur after Print Management",
        win32con.MB_YESNOCANCEL,
    )
    if console_warning == 2:
        error("Cancelled")
    elif console_warning == 6:
        # Manual Backup if choisen
        if isfile(printers_manual_backup_path):
            remove_file(printers_manual_backup_path)
        print(run(r'"%s" -b -f %s' % (printbrm_path, printers_manual_backup_path)))
        user_backup_path = makepath(user_desktop, printers_manual_backup_file)
        filecopyto(printers_manual_backup_path, user_backup_path)
        print("Manual backup located at: %s" % user_backup_path)
        # Delete file to prevent futur further interaction
        # remove_file(user_backup_path)
    # print("Place breakpoint here and copy above code for manual backup")

    # Print Management
    run('printmanagement.msc')

    # Get printers_list
    printers_list = []
    for u in json.loads(list_local_printers()):
        for p in u:
            if p:
                if not type(p) == int:
                    if not p.split(",")[0] in printers_list:
                        printers_list.append(p.split(",")[0])

    # Asking printer to export
    # select_printer = waptguihelper.combo_dialog("Select Printer to Export", "Selected Printer", printers_list)

    # Formatting dict
    printers_dicts = run_powershell("Get-Printer")
    printers_dict_list = []
    key_list = ["Name", "DriverName", "PortName", "PrintProcessor", "PrintProcessor", "Published", "Shared"]
    for p in printers_dicts:
        for key in list(p.keys()):
            if key not in key_list:
                del p[key]
        printers_dict_list.append(p)

    selected_printers = json.loads(
        waptguihelper.grid_dialog(
            "Select printers by deleting printer that you won't import",
            json.dumps(printers_dict_list),
        )
    )

    # Backup
    if not isfile(printers_manual_backup_path):
        if isfile(printers_backup_path):
            remove_file(printers_backup_path)
        print(run(r'"%s" -b -f %s' % (printbrm_path, printers_backup_path)))

    # Preparing to export
    printers_to_export = []
    for d in selected_printers:
        printers_to_export.append(d["Name"])
    for p in printers_list:
        if p not in printers_to_export:
            remove_printer(p)
    clean_spooler()

    # Export
    if isfile(printers_export_path):
        remove_file(printers_export_path)
    print(run(r'"%s" -b -f %s' % (printbrm_path, printers_export_path)))
    if isfile(printers_export_file):
        remove_file(printers_export_file)
    filecopyto(printers_export_path, printers_export_file)

    # Restore
    if isfile(printers_manual_backup_path):
        printers_backup_path = printers_manual_backup_path
    print(run(r'"%s" -r -f %s -o' % (printbrm_path, printers_backup_path)))
    print(
        "If the restoration of the previously installed printers (on this PC) failed, you may want to recover the backup file located: %s"
        % printers_backup_path
    )


def clean_spooler():
    run("net stop spooler")
    run(r"DEL /F /S /Q %systemroot%\System32\spool\PRINTERS\*")
    run("net start spooler")

										

  Changelog 



20.1.1-6
update-package from console is now the same as from editor

20.1-5
Now a manual backup can be done

20.0-0
Initial package

									
  manifest.sha256