I modified the script (below) to add the appropriate group based on the folder name (to the folderexamined).
I had to enable inheritance to add the parent folder groups to the sub-folders and sub-files.
Without the attribute "ContainerInherit", "ObjectInherit" doesn't do that.
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::"ContainerInherit", "ObjectInherit"
Unfortunately I get the error below:
Exception calling "AddAccessRule" with "1" argument(s): "No flags can be set. Parameter name: inheritanceFlags"
However in the test environment the script seems working.
NOTE:
- if I set the flag as "NON"E the script ofcourse works
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::
- If I use the user/group SID (instead of the name) i have the error below:
New-Object : Cannot find an overload for "FileSystemAccessRule" and the argument count: "5".
+ ... AccessRule((New-Object System.Security.AccessControl.FileSystemAccess
>> How can I go over this error?
Thanks
------------SCRIPT------------
$fileList = Get-ChildItem -Recurse -Path "D:\test"
foreach ($lists in $fileList) {
$fileName = $Lists.name
$Path=$Lists.Fullname
$ACL = Get-Acl -Path $Path
$GroupID = Get-ADGroup -Filter “Name -like 'Local\_$fileName\*'" -Properties \* | select -property name
if (-not ($GroupID -eq $null)) {
foreach ($NameAD in $GroupID) {
$ADname = "DOMAIN\" + $NameAD
$modifiedADname = ($ADname -replace '@{name=', '').Trim("}")
#$ACL = Get-Acl -Path $Path
#$ACL.SetAccessRuleProtection($false,$true)
$ACL.SetAccessRuleProtection($true,$false)
$User1 = "Administrators"
$FileSystemAccessRights1 = [System.Security.AccessControl.FileSystemRights]"FullControl"
$FileSystemAccessRights2 = [System.Security.AccessControl.FileSystemRights]"Modify"
$FileSystemAccessRights3 = [System.Security.AccessControl.FileSystemRights]“Read”
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::"ContainerInherit", "ObjectInherit"
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$AccessControl = [System.Security.AccessControl.AccessControlType]::Allow
# if at the end of the group name is present the caracter "W"
if ($modifiedADname -match 'W$')
{
$ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule($modifiedADname, $FileSystemAccessRights2, $InheritanceFlag, $PropagationFlag, $AccessControl)))
$ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule($User1, $FileSystemAccessRights1, $InheritanceFlag, $PropagationFlag, $AccessControl)))
Set-Acl -Path $Path -AclObject $ACL
}
else {
Write-Output "----------------"
Write-Output $Path " The group doesn't exist"
Write-Output "----------------"
}
}