Page 1 of 1

[SOLVED] Checking if a PC is on a domain

Published: June 16, 2020 - 12:21
by gaelds
Good morning,
I would like to test in a package whether a PC is joined to my domain or in a workgroup. I had this function below, but it's not suitable because whether the PC is in a workgroup with an IP address assigned by DHCP (from the domain) or joined to the PEDAGO.lan domain, socket.getfqdn() returns the same name (e.g., PCNAME.PEDAGO.lan).

Code: Select all

def domain():
    if ("PEDAGO.lan" in socket.getfqdn()):
        dom="PEDAGO"
    else:
        print('PC en workgroup')
        dom="WG"
    return dom

Re: Checking if a PC is on a domain

Published: June 16, 2020 - 2:59 PM
by sfonteneau
You can try that!

Code: Select all

get_domain_fromregistry()

Re: Checking if a PC is on a domain

Published: June 16, 2020 - 3:18 PM
by gaelds
Thanks, but it's no better... On a Win10 PC in a workgroup, with DHCP enabled, the command returns PEDAGO.lan instead of "Workgroup" or nothing.

Re: Checking if a PC is on a domain

Published: June 16, 2020 - 6:12 PM
by sfonteneau

Code: Select all

import win32security
win32security.DsGetDcName()['DomainName']
or if the test is offline:

Code: Select all

registry_readstring(HKEY_LOCAL_MACHINE,r'SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History','NetworkName',None)

Re: Checking if a PC is on a domain

Published: June 17, 2020 - 08:42
by gaelds
Thanks a lot! The registry test works perfectly; the first one crashes if the PC is in a workgroup.

Have a good day,
Gaël.

Re: Checking if a PC is on a domain

Published: June 17, 2020 - 9:52 PM
by sfonteneau

Re: [SOLVED] Checking if a PC is on a domain

Published: June 19, 2020 - 4:05 PM
by gaelds
Yes, it's strange...
But even with the registry test, I'm getting some incorrect detections on PCs that were on the domain but are no longer. I suppose the registry value isn't updated when they leave the domain.

Re: [SOLVED] Checking if a PC is on a domain

Published: June 19, 2020 - 6:05 PM
by sfonteneau
Yes, indeed