forked from pywinauto/pywinauto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_administrators.py
70 lines (57 loc) · 2.22 KB
/
print_administrators.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Script to display all the user groups and all the users in Administrators group
Requirements: Python 2.7 or 3.4, pyWin32, pywinauto 0.5.4+
download the repo: https://github.com/pywinauto/pywinauto
place the script to the repo root folder
"""
from subprocess import Popen
from pywinauto import application
from pywinauto.timings import always_wait_until_passes
class AccessDeniedError(Exception):
"""Raise when current user is not an administrator."""
def __init__(self, arg):
self.args = arg
class NoExistGroupError(Exception):
"""Raise when group Administrators is not exist."""
def __init__(self, arg):
self.args = arg
def is_user_an_admin():
""" Check user admin """
import os
if os.name == 'nt':
try:
# only windows users with admin privileges can read the C:\windows\temp
os.listdir(os.sep.join([os.environ.get('SystemRoot', 'C:\\windows'), 'temp']))
except Exception:
return False
else:
return True
else:
return 'SUDO_USER' in os.environ and os.geteuid() == 0
if is_user_an_admin():
Popen(['compmgmt.msc'], shell=True)
@always_wait_until_passes(4, 2, application.ProcessNotFoundError)
def connect_to_mmc():
""" Returns connect app to instance """
return application.Application().connect(path="mmc.exe")
app = connect_to_mmc()
tree = app.mmc_main_frame.tree_view
groups = tree.get_item(r'\Computer Management (Local)\System Tools\Local Users and Groups\Groups')
groups.click()
items = app.mmc_main_frame.list_view.items()
print('\nGroups:')
for i in range(0, len(items), 2):
print(items[i].text())
try:
administrators = app.mmc_main_frame.list_view.get_item('Administrators')
administrators.click(double=True) # double click opens group properties
app.dialog.wait('exists')
items = app.dialog.list_view.items()
print('\nAdministrators:')
for element in items:
print(element.text())
except ValueError:
print('\nThere is no Administrators group')
app.kill()
else:
print('\nYou are not an administrator')