problem executing the session_setup() function

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
Answer
brice73
Messages: 42
Registration: February 13, 2023 - 8:05 AM

September 29, 2025 - 5:30 PM

Good morning,

I have a package that needs to delete two values ​​from a registry key (if those values ​​exist) within the session_setup function. Here is the code:

Code: Select all

def session_setup():

    nom_PC=get_computername().lower()

    if "pm" in nom_PC:
        if reg_value_exists(HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System','Wallpaper') :
            registry_delete(HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System','Wallpaper')

        if reg_value_exists(HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System','WallpaperStyle'):   
            registry_delete(HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System','WallpaperStyle')
Once the package is installed and when a user for whom both values ​​in the key exist logs on, these two values ​​are not deleted.

If I open a command prompt and manually run the session-setup function for this package, I get the following error:

Code: Select all

Microsoft Windows [version 10.0.26100.6584]
(c) Microsoft Corporation. Tous droits réservés.

U:\>wapt-get session-setup col73-pi-fonds-ecran
Using config file: C:\Program Files (x86)\wapt\wapt-get.ini
Configuring col73-pi-fonds-ecran ...
Running session_setup for package col73-pi-fonds-ecran(=0-11) and user test
2025-09-29 17:12:08,371 CRITICAL Fatal error in session_setup function: AttributeError: __enter__:
Traceback (most recent call last):
  File "C:\Program Files (x86)\wapt\waptpackage.py", line 3004, in call_setup_hook
    hookdata = hook_func()
  File "<string>", line 43, in session_setup
  File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 3862, in registry_delete
    with reg_openkey_noredir(root, path, sam=KEY_WRITE) as key:
AttributeError: __enter__

2025-09-29 17:12:08,387 CRITICAL session_setup failed for package col73-pi-fonds-ecran(=0-11) and user test
Done
However, if I open a command prompt with administrator privileges and run the session-setup function for this package, it executes successfully (I verify that the 2 values ​​in the registry key are indeed deleted):

Code: Select all

Microsoft Windows [version 10.0.26100.6584]
(c) Microsoft Corporation. Tous droits réservés.

C:\Windows\System32>wapt-get session-setup col73-pi-fonds-ecran
Using config file: C:\Program Files (x86)\wapt\wapt-get.ini
Configuring col73-pi-fonds-ecran ...
Running session_setup for package col73-pi-fonds-ecran(=0-11) and user test
Done
Where could the problem be coming from and is there a way to make the session-setup() function run automatically for each user when they log in?
WAPT Enterprise 2.6.1.17765
WAPT server under Debian 13
Administration/package creation under Windows 11/10
User avatar
sfonteneau
WAPT Expert
Messages: 2318
Registered: July 10, 2014 - 11:52 PM
Contact :

September 30, 2025 - 12:45

Hello

, it seems you can't because the ACLs set by default to "Policies" don't allow the user to modify this key.

The session-setup runs with user privileges and therefore doesn't have the necessary permissions.
Attachments
Capture.PNG
Capture.PNG (37.49 KB) Viewed 16020 times
brice73
Messages: 42
Registration: February 13, 2023 - 8:05 AM

October 7, 2025 - 7:29 PM

Hello Simon,

thank you for your reply. Although it's a bit illogical, I thought session_setup() ran with system account privileges... I found another way to do it (using LGPO.exe).

You can mark the thread as resolved.

Regards,
WAPT Enterprise 2.6.1.17765
WAPT server under Debian 13
Administration/package creation under Windows 11/10
User avatar
dcardon
WAPT Expert
Messages: 1929
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

October 8, 2025 - 9:39 AM

Hi Brice,
brice73 wrote: Oct 7, 2025 - 7:29 PM
Thank you for your reply. Although it's not very logical, I thought session_setup() ran with system account privileges... I found another way to do it (using LGPO.exe).

You can mark the thread as resolved.
Thank you for your feedback. If you have the setup.py file handy, please feel free to share it. If you've asked this question, it's likely others will have the same one.

Note: lgpo.exe is free but cannot be redistributed (restrictive and explicit Microsoft proprietary license on the subject), so it cannot be put on the store, but it can still serve as an example for others.

Sincerely,

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
brice73
Messages: 42
Registration: February 13, 2023 - 8:05 AM

October 10, 2025 - 10:12

Hello Denis,

Why not, it didn't seem useful to me (the setup.py is also very short).

A short file (in PDF format) from Microsoft on the use of LGPO.exe is quite explicit. Several packages in our infrastructure use LGPO.exe to configure local GPOs (in addition to GPOs enabled at the domain level in Active Directory).

To disable a user GPO, I initially thought it was better and simpler to use session_setup(), for users for whom the GPO had already been activated because it was causing the creation of keys in HKCU, which it seemed sufficient to delete a priori.

So, I ended up reusing LGPO to disable the user GPO in question for all users. Below is the setup.py file with some comments on its use (but the Microsoft documentation is much more complete).

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *

r"""Ce paquet a comme dépendance col73-pi-debloat-windows-peda car il faut auparavant désactiver windows à la une (windows spotlight) qui modifie automatiquement régulièrement les fonds d'écran
"""

def install():

    nom_PC=get_computername().lower()

    #forçage du fonds d'écran seulement pour les postes élèves
    if not "pm" in nom_PC:

        #fixe le fond d'écran (celui par défaut de Windows)
        #GPO Configuration Utilisateur > Modèles d'administration > Bureau > papier peint du bureau > activé)
        #chemin du fonds d'écran appliqué : C:\Windows\Web\Wallpaper\Windows\img0.jpg
        print('poste élève -> paramétrage du fonds d\'écran via gpo locale')
        run_notfatal(r'.\GPO\LGPO.exe /q /u .\GPO\activate_gpo_u_fonds_ecran.pol')
        print('fonds d\'écran paramétré')

        print('forçage des maj gpo')
        run_powershell(r'Start-Process PowerShell -Verb runAs -Wait -ArgumentList "gpupdate /target:user /force"')
        print('forçage des maj gpo effectué')

    else:
        print('poste professeur -> pas de paramétrage du fonds d\'écran')

        #pour l'annulation du forçage déjà effectué sur les postes professeurs dans une version antérieure du paquet
        #GPO Configuration Utilisateur > Modèles d'administration > Bureau > papier peint du bureau > désactivé
        #exporter le fichier de configuration en mode texte avec LGPO.exe, ne conserver que les lignes de désactivation de la gpo (éditer le fichier avec notepad)
        #puis exporter le fichier .txt en fichier .pol toujours avec LGPO.exe -> on obtiens le fichier de désactivation à appliquer

        run_notfatal(r'.\GPO\LGPO.exe /q /u .\GPO\desactivate_gpo_u_fonds_ecran.pol')

        print('forçage des maj gpo')
        run_powershell(r'Start-Process PowerShell -Verb runAs -Wait -ArgumentList "gpupdate /target:user /force"')
        print('forçage des maj gpo effectué')

You can mark the thread as resolved. Thank you

Sincerely,
WAPT Enterprise 2.6.1.17765
WAPT server under Debian 13
Administration/package creation under Windows 11/10
Answer