creating a package to delete user profiles

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
adgm11
Messages: 45
Registration: Apr 12, 2022 - 07:00

August 30, 2022 - 1:53 PM

Good morning
I have a PowerShell command to delete user profiles except for admin accounts. However, after the task completes correctly and deletes all the specified profiles, there are errors related to "Remove-CimInstance access denied".
The package is this one:

Code: Select all

def install():
    run_powershell('Set-ExecutionPolicy Unrestricted')
    run_powershell('$AccountsToKeep = @("admin","administrator","Public","default")')
    run_powershell("Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -notin $AccountsToKeep } | Remove-CimInstance")

Is there a way to convert this command to Python? Or can we eliminate PowerShell errors?
User avatar
dcardon
WAPT Expert
Messages: 1929
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

August 30, 2022 - 2:21 PM

As it's written, the different PowerShell commands will be executed individually without preserving the context between them. So it won't work. You can try putting everything in a ps1 file.
Otherwise, the best solution would indeed be to rewrite it in Python; it will be much cleaner.
Regards,
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