Is there any way to set a printer as default according with Active Directory Policy Security Group and PC hostname?

22 Views Asked by At

I'm having an issue at the company where I work. In our print server, there are several printers that we've separated by Cost Center. There are multiple printers linked to the same IP address, where a specific security group in Active Directory automatically maps to the user.

My issue is as follows: many users in the company, for example, a doctor, are always in different sections of the company, such as: Stations, Operating Room, Medical Office. They have a great difficulty; each time they move to a new location, they cannot go to the PC options and set a particular printer as default on the PC.

What I wanted was to create a Python code that stays running continuously, and when the user logs into the PC on the domain, the code identifies their login, checks the hostname of the PC they are logged into, and their security group membership. For example: user "igorcarmona" who is part of the security group "IMP_TIC_COLOR" logged into the PC "TIC0068" will have printer X set as default.

Servidor de impressão

Here is a draft of a code, but I am having difficulties building it. Has anyone been through this and can help me?

I already tried to do this:

import os
import time
import pyad.adquery
import win32print
import socket

AD_SERVER_IP = "192.168.0.193"
PRINT_SERVER_IP = "192.168.0.230"

def check_user_login():
    query = pyad.adquery.ADQuery()
    query.execute_query(
        type=AD_SERVER_IP,
        attributes=["memberOf"],
        where_clause="objectClass = 'user' AND sAMAccountName = '" + os.getlogin() + "'"
    )
    result = query.get_single_result()
    if result:
        groups = result['memberOf']
        return groups
    return []

def get_host_name():
    return socket.gethostname()

def set_default_printer(printer_name):
    printer_info = win32print.EnumPrinters(win32print.PRINTER_ENUM_CONNECTIONS, None, 1)
    for printer in printer_info:
        if printer_name in printer[2]:
            win32print.SetDefaultPrinter(printer[2])
            print("Default printer set to:", printer[2])
            break

def main():
    while True:
        groups = check_user_login()
        hostname = get_host_name()
        if groups:
            for group in groups:
                print(group)
                if "IMP_TIC_COLOR" in group and hostname.startswith("TIC"):
                    set_default_printer("IMP_TIC_COLOR")

        time.sleep(30)  # Verifica a cada 30 segundos

if __name__ == "__main__":
    main()
1

There are 1 best solutions below

0
ErkinD39 On

You can use item-level targeting in Preferences Section of the Group Policy Object (GPO).

"Within a single Group Policy object (GPO), you can include multiple preference items, each customized for selected users or computers and each targeted to apply settings only to the relevant users or computers"

Ref: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/dn789189(v=ws.11)

So you can apply user preferences to set default printers based on the computer name they logon to.

"A Computer Name targeting item allows a preference item to be applied to computers or users only if the computer's name matches the specified computer name in the targeting item."

Please note that using item level targeting with computer name selection does not imply a computer policy, still this should be a single GPO applied to users including several preference settings to set different printers as default for domain users based on the logged on computer name.