Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

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

$
0
0

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 osimport timeimport pyad.adqueryimport win32printimport socketAD_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])            breakdef 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 segundosif __name__ == "__main__":    main()

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>