Now in our scenario we have Folder "Client" that contains Clientfolders like "Client_A" "Client_B" and so on.
All Folders in the Folder "Client" have Users named the same as their Folder (Client_A, Client_B) where the Directory is set to the according Folder
We have another Group "Support"
All members of this group should have Access to the Subfolders from "Client" but should not be able to modify or create other Folders in the Root of the Folder Client.
I have therefor set Subfolder Permissions in the Support Group.
This works as intended.
Now every Clientuser has a expiredate and the script checks if the user is expired and deletes the User and the corresponding Folder.
So if Client_A is expired the script deletes the user and the Folder "Client_A"
This all works realy good
What i cannot get to work is that the script also removes the Subfolderpermission in the Group "Support"
Here is the Script that works currently:
Code: Select all
local mydomain = "Domainname"
local rootdirtmp = "D:/ftp/Client/"
local exclude_folders = { "Exclude1", "Exclude2" }
-- Hilfsfunktion, um zu überprüfen, ob ein Element in einer Tabelle vorhanden ist
local function table_contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
-- Funktion, um die Liste der gültigen Benutzer zu erhalten
local function get_valid_user_folders()
local valid_user_folders = {}
local strUserlist = c_GetUserList(mydomain)
local userlist = Split(strUserlist, "\n")
for _, username in pairs(userlist) do
local user = c_GetUser(mydomain, username)
if user then
table.insert(valid_user_folders, username)
end
end
return valid_user_folders
end
-- Funktion, um alle Ordner im Verzeichnis zu erhalten (mit PowerShell, UTF-8 sichergestellt)
local function get_all_folders_in_directory(directory)
local folders = {}
local p = io.popen('powershell.exe -Command "[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-ChildItem -Path \'' .. directory .. '\' -Directory | ForEach-Object { $_.Name }"')
for folder in p:lines() do
table.insert(folders, folder)
end
p:close()
return folders
end
-- Hauptfunktion, um Ordner zu löschen, die nicht für gültige Benutzer sind und nicht in der Ausschlussliste stehen
local function delete_invalid_folders()
local valid_user_folders = get_valid_user_folders()
-- Hole die Liste aller Ordner im rootdirtmp-Verzeichnis
local all_folders = get_all_folders_in_directory(rootdirtmp)
for _, folder_name in pairs(all_folders) do
-- Überprüfe, ob der Ordner weder auf der Ausschlussliste steht noch zu einem gültigen Benutzer gehört
if not table_contains(exclude_folders, folder_name) then
if not table_contains(valid_user_folders, folder_name) then
-- Verwende PowerShell-Befehl über os.execute(), um Ordner zu löschen, auch bei Sonderzeichen
local delete_command = 'powershell.exe -Command "[Console]::OutputEncoding = [Text.Encoding]::UTF8; Remove-Item -Recurse -Force \'' .. rootdirtmp .. folder_name .. '\'"'
os.execute(delete_command) -- Lösche den Ordner rekursiv und ohne Bestätigung
end
end
end
end
-- Starte das Löschen
delete_invalid_folders()