Full name of Windows user name (in Domain) using Python
The more direct way to do this is to query Active Directory. You'd perform a lookup on the user, followed by getting the displayName
attribute. (This maps to the Full Name
displayed in Windows.)
You have two options here:
Using a Python AD library, e.g. pyad
This is very Windows-specific, and requires the pywin32
library. It relies on ADSI APIs, so will only work on Windows.
from pyad import aduser
user = aduser.ADUser.from_cn(username)
print user.get_attribute("displayName")
How you get the username is up to you. You can use getpass.getuser()
, os.environ["USERNAME"]
(Windows-only), etc.
Using a Python LDAP library, e.g. ldap3
This follows the standard LDAP protocol, with a pure Python implementation, so should work from any client OS.
Using raw LDAP queries is rather more involved than the ADSI abstractions. I suggest you read the documentation (which has decent tutorials) and search for more tutorials on interacting with Microsoft AD via ldap3.
Note that one possible issue is that searching by username (CN) alone might get you the wrong object. It's possible to have multiple objects with the same CN across multiple OUs. If you want to be more precise, you might want to use a unique identifier like the SID.