[SOLVED] Adding user to local group

Questions about WAPT Packaging / Requests and help regarding Wapt packages.
Forum Rules
Community Forum Rules
* English support on www.reddit.com/r/wapt
* French community support is available on this forum
* Please prefix the topic title with [RESOLVED] if it is resolved.
* Please do not edit a topic that is tagged [RESOLVED]. Open a new topic referencing the old one.
* Specify the installed WAPT version, full version, and build number (2.2.1.11957 / 2.2.2.12337 / etc.) as well as the Enterprise/Discovery edition.
* Versions 1.8.2 and earlier are no longer supported. The only questions accepted regarding version 1.8.2 are related to upgrading to a supported version (2.1, 2.2, etc.).
* Specify the server OS (Linux/Windows) and version (Debian Buster/Bullseye - CentOS 7 - Windows Server 2012/2016/2019).
* Specify the OS of the administration/package creation machine and the machine with the problematic agent, if applicable (Windows 7/10/11/Debian 11/etc.).
* Avoid asking multiple questions when opening a topic, otherwise it may be ignored. If there are multiple topics, open separate topics, preferably one after the other and not all at the same time (i.e., do not spam the forum).
* Include code snippets, screenshots, and other images directly in the post. Links to Pastebin, Bitly, and other third-party sites will be systematically removed.
* As with any community forum, support is provided voluntarily by members. If you require commercial support, you can contact Tranquil IT's sales department at 02.40.97.57.55
Locked
User avatar
Mathieu
Messages: 91
Registration: August 18, 2016 - 10:24

November 9, 2022 - 12:13

Good morning,
I am having trouble with a command to add the user to a Local group in auditing.

Code: Select all

2022-11-08 09:44:33,484 CRITICAL Fatal error in audit function: TypeError: 'error' object is not subscriptable:
Traceback (most recent call last):
  File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 2264, in add_user_to_group
    win32net.NetLocalGroupAddMembers(None, group, 3, [user_group_info])
pywintypes.error: (1378, 'NetLocalGroupAddMembers', 'Le nom de compte spécifié est déjà membre du groupe.')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\wapt\waptpackage.py", line 2978, in call_setup_hook
    hookdata = hook_func()
  File "C:\waptdev\futur-Creation_Profil_2.0-60_windows_PREPROD-wapt\setup.py", line 79, in audit
    add_user_to_group(user, "Utilisateurs avec pouvoir")
  File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 2267, in add_user_to_group
    if e[0] != 1378:
TypeError: 'error' object is not subscriptable

Audit aborted due to exception: 'error' object is not subscriptable
I'm encountering an error when the user is already in the group; they don't meet the first condition, and it's trying to add them. Hence the error

Here is the relevant snippet of script:

Code: Select all

user = registry_readstring(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI", "LastLoggedOnUser")

if user in local_group_members("Utilisateurs avec pouvoir"):
        print("Utilisateur déjà dans le groupe Utilisateurs avec pouvoir")
elif user not in local_group_members("Utilisateurs avec pouvoir"):
        print("Utilisateurs pas de le groupe Power Users")
        add_user_to_group(user, "Utilisateurs avec pouvoir")
Thank you
- WAPT 2.2.3.12463 Enterprise
- Debian 9.9
- Windows 10 21H2 & Windows 11 22h2
User avatar
dcardon
WAPT Expert
Messages: 1929
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

November 9, 2022 - 5:46 PM

Hello Mathieu,

Ah yes, that's a bug we missed in the Python 2 to Python 3 transition... You can add the following function to your package to override your setup.py with the function definition below. It will be fixed in the next version.

Sincerely,

Denis

Code: Select all

def add_user_to_group(user, group):
    """Add membership to a local group for a user

    """
    user_group_info = dict(
        domainandname=user
    )
    try:
        win32net.NetLocalGroupAddMembers(None, group, 3, [user_group_info])
    except win32net.error as e:
        # pass if already member of the group
        if e.winerror != 1378:
            raise
        else:
            logger.debug('add_user_to_group %s %s : %s' % (user, group, ensure_unicode(e)))
Denis Cardon - Tranquil IT
Share your experiences on WAPT! Send us your blog and article URLs in the "Your Opinion of the forum, and we'll feature them on the WAPT
User avatar
dcardon
WAPT Expert
Messages: 1929
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

November 16, 2022 - 10:44 AM

I'm marking this issue as resolved.
Denis
Denis Cardon - Tranquil IT
Share your experiences on WAPT! Send us your blog and article URLs in the "Your Opinion of the forum, and we'll feature them on the WAPT
Locked