Repository navigation
Expand file tree
/
Copy pathlist_admin.py
More file actions
52 lines (44 loc) · 1.91 KB
/
Copy pathlist_admin.py
File metadata and controls
52 lines (44 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# only working locally. Not getting that list off remote computers.
import argparse
import win32com.client
import getpass
def get_local_admins(computer=None, username=None, password=None):
if computer:
computer = f"\\\\{computer}"
else:
computer = "."
try:
group_name = "Administrators"
if username and password:
locator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
print(f"Connecting to {computer} with username {username}")
connection = locator.ConnectServer(computer, "root\\cimv2", username, password)
connection.Security_.ImpersonationLevel = 3
group = connection.ExecQuery(f"SELECT * FROM Win32_Group WHERE Name='{group_name}'")
members = []
for g in group:
for member in g.Associators_("Win32_GroupUser"):
members.append(member.Name)
else:
obj_group = win32com.client.Dispatch("AdsNameSpaces").GetObject("", f"WinNT://{computer}/{group_name},group")
members = [member.Name for member in obj_group.Members()]
return members
except Exception as e:
return f"An error occurred: {e}"
def main():
parser = argparse.ArgumentParser(description='List users in the local admin group.')
parser.add_argument('computer', nargs='?', default=None, help='Computer name to check. If not provided, checks the local computer.')
parser.add_argument('--username', type=str, help='Username for remote computer.')
args = parser.parse_args()
password = None
if args.username:
password = getpass.getpass(prompt='Enter password: ')
admins = get_local_admins(args.computer, args.username, password)
if isinstance(admins, list):
print("Local Administrators:")
for admin in admins:
print(admin)
else:
print(admins)
if __name__ == "__main__":
main()