Search code examples
pythonwindowsmembershipuser-accounts

python: How to enumerate local windows group membership


I need to test group membership of specified accounts.

Given Account 'X', is it a member of groups 'A' and 'B'

These are local windows accounts on a 2003 server, not a DC and it does not connect to a DC.

Here is the answer I formed after being pointed in the correct direction

import win32net
import platform
import getpass

#Get current hostname and username
sHostname = platform.uname()[1]
sUsername = getpass.getuser()

#Define account memberships to test as false
memberAdmin = False
memberORA_DBA = False

for groups in win32net.NetUserGetLocalGroups(sHostname,sUsername):
    #If membership present, set to true
    if groups == 'Administrators':
        print "member of admin"
        memberAdmin = True

    if groups == 'ORA_DBA':
        print "member of ORA_DBA"
        memberORA_DBA = True

#if all true pass, else fail
if (memberAdmin == True) and (memberORA_DBA == True):
    print "membership is good"
else:
    print "current account does not have the proper group membership"

Solution

  • You need to use Python Win32 Extensions to interact with Windows. I think some of the methods in win32net module would help you to get the information you need.