Welcome, guest | Sign In | My Account | Store | Cart

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[0]:
        dcname=dc[0][0]['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.

3 comments

Tim Golden 21 years, 6 months ago  # | flag

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.

win32net.NetUserGetInfo (win32net.NetGetAnyDCName (), win32api.GetUserName (), 1)
<pre>

</pre>

Wolfgang Strobl (author) 19 years, 11 months ago  # | flag

>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.

Mike Sarahan 15 years, 1 month ago  # | flag

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?

def UserGetInfo():
    try:
        dc=win32net.NetServerEnum(None,100,win32netcon.SV_TYPE_DOMAIN_CTRL)
    except:
        dc=[]
    user=win32api.GetUserName()
    if len(dc) is not 0:
        dcname=dc[0][0]['name']
        return win32net.NetUserGetInfo("\\\\"+dcname,user,1)
    else:
        return win32net.NetUserGetInfo(None,user,1)