'Set Windows ACL with powershell "Special permission"

I'm trying to recreate a function with powershell. It creates a folder and sets the ACL. In the security settings it is labeled as special permission. In the advanced settings I can see 2 entrys for the same user:

In the advanced settings this applies to "this folder only"
FileSystemRights  : Modify, Synchronize AccessControlType : Allow
IdentityReference : XXXX 
IsInherited       : False 
InheritanceFlags  : ContainerInherit, ObjectInherit 
PropagationFlags  : InheritOnly
     
In advanced settings "Subfolders and files" 
FileSystemRights  : CreateFiles, AppendData, ReadAndExecute, Synchronize 
AccessControlType: Allow 
IdentityReference : XXXX 
IsInherited       : False
InheritanceFlags  : None 
PropagationFlags  : None

Code:

$permissionUser = "LVRINTERN\" + $UserID

$Rights = "CreateFiles, AppendData, ReadAndExecute, Synchronize" #Comma seperated list.
$InheritSettings = "None" #Controls how permissions are inherited by children
$PropogationSettings = "None"
$RuleType = "Allow" #Allow or Deny.
#attribute in eine liste packen und regel erzeugen
$perm = $permissionUser, $Rights, $InheritSettings, $PropogationSettings, $RuleType
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $perm
    
$Rights = "Modify, Synchronize" #Comma seperated list.
$InheritSettings = "Containerinherit, ObjectInherit" #Controls how permissions are inherited by children
$PropogationSettings = [System.Security.AccessControl.PropagationFlags]::InheritOnly #Usually set to none but can setup rules that only apply to children.
$RuleType = "Allow" #Allow or Deny.
    
$perm = $permissionUser, $Rights, $InheritSettings, $PropogationSettings, $RuleType
$rule2 = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $perm
            
$acl = Get-ACL $LPath
#add rule to var
$acl.SetAccessRule($rule)
$acl.SetAccessRule($rule2)
#set var on object
Set-ACL -Path $LPath -AclObject $acl

I'm not getting the right results. What am I doing wrong here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source