Calling win32net.NetUserGetInfo(None, win32api.GetUserName(), 1) works for users logged in to the local machine, only, but fails for domain users. The snippet below demonstrates how to query the domain controller if there is one.
Python, 13 lines
1 2 3 4 5 6 7 8 9 10 11 12 13
import win32api import win32net import win32netcon def UserGetInfo(): dc=win32net.NetServerEnum(None,100,win32netcon.SV_TYPE_DOMAIN_CTRL) user=win32api.GetUserName() if dc: dcname=dc['name'] return win32net.NetUserGetInfo("\\\\"+dcname,user,1) else: return win32net.NetUserGetInfo(None,user,1) if __name__=="__main__": print UserGetInfo()
This recipe obviously works on Windows NT+ only. In addition, it needs Mark Hammonds win32 extensions.
Alternatively, and a lot more concisely... There's no need to go quite so far to establish the domain controller. The following will do the job quite nicely, altho' I don't know how it will behave in a domainless setup.
>altho' I don't know how it will behave in a domainless setup. Well, I do know. It won't work. :-} My example does the proper fallback.
I had a couple of issues with this, and I wanted to post my modifications. They're at the bottom of my message. I use this script to keep track of user logon/logoff times. It's really slow on non-domain computers - about 10 seconds for the server enumeration. Is there any way to detect the non-domain status of a computer (i.e. check if it's in a workgroup), and make this go faster for the non-domain case?