Forum Discussion
Getting a parent's parent for a folder using power shell
- Oct 17, 2020
the script seems to be working,
I try it on my side and the result was correct, good job.
$Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allFolders.FullName -AclObject $Permission
In the first line and as the $Permission is holding the folder ACL information, there is one of the methods called SetAccessRuleProtection, you can find all other methods and properties by using $Permission | Get-Member.
The SetAccessRuleProtection accepts 2 input IsProtected and PreservedInheritance
SetAccessRuleProtection (bool isProtected, bool preserveInheritance);
IsProtected: Type is Bool ($True/$false) , $False= Enable Inheritance
PreservedInheritance : This parameter is actually ignored as the IsProtected is set to false
Thanks
-------------------------
Feel free to respond and request more information, if you find the answer that satisfy your need, Please make sure to mark it as Best Response and like the other.
Hi, I am back 🙂
$Permission=get-acl -Path $allFolders.Parent.FullName
will get the parent folder name, so for C:\Folder1\Folder2, the above script will get the name of Folder1.
$Permission=get-acl -Path $the_shares
Will get the folder you set in the variable
The Different between copying the permission and inheriting it is, let's assume that you got the following folder structure
C:\Folder1\Folder2\Folder3
Inheriting the permission will copy all the permission as set in Folder1 (for example) down below the tree (other family members)
So if Folder1 has permission for User1 full control, this permission will go down the other folders.
Copying the permission won't inherit the permission from the top, but if user1 have full control in Folder1, it doesn't mean that this user will have the permission on the below folder (Folder2), but the same user User1 may have permission on Folder3.
In your case, I guess you care about inheriting more than copying.
Why a person may go for Copying the permission,
lets assume that you have the following folder structure
C:\OfficeFolder\Departments\IT
C:\OfficeFolder\Departments\Accounting
C:\OfficeFolder\Departments\HR
User1 need to access the root of OfficeFolder and the IT folder only but should not access the accounting and HR, so technically, (inheritance is not enabled and you can copy the permission to the below folder with some modification. small example is the home folder.
anyway, I can write a script which will do the following if you are intersted.
you set the root folder and from there it will go down the tree, and if the folder length is 1 then it will enable inheritance, or else do nothing, seems good or its something else
sorry, maybe I forget as I was away 🙂
- suren424Oct 16, 2020Copper Contributor
Thank youfarismalaeb and Wesley Baker for the great explanations.
farismalaebYes, as of now my requirement is to just inherit permissions from the parent folder.
So, if the share is '\\MyShare\home\s\abcd\', the script should display the parent folder in the comments and set permissions on 'abcd' by inheriting from it's parent 's'. Will the below script work
$the_shares = '\\MyShare\home\s\abcd\' $allFolders=Get-ItemProperty -Path $the_shares Write-Host "Setting permissions on " $allfolders.Name " by inhertiing from " $allfolders.Parent.Name $allFolders.Parent.Name $Permission=get-acl -Path $allFolders.Parent.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allFolders.FullName -AclObject $Permission
I understood on this : It gets the acl of the parent folder : $Permission=get-acl -Path $allFolders.Parent.FullName
If you don't mind, can you explain me on the below 2 commands on what it does ?
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allFolders.FullName -AclObject $PermissionThanks in advance.
- farismalaebOct 17, 2020Steel Contributor
the script seems to be working,
I try it on my side and the result was correct, good job.
$Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allFolders.FullName -AclObject $Permission
In the first line and as the $Permission is holding the folder ACL information, there is one of the methods called SetAccessRuleProtection, you can find all other methods and properties by using $Permission | Get-Member.
The SetAccessRuleProtection accepts 2 input IsProtected and PreservedInheritance
SetAccessRuleProtection (bool isProtected, bool preserveInheritance);
IsProtected: Type is Bool ($True/$false) , $False= Enable Inheritance
PreservedInheritance : This parameter is actually ignored as the IsProtected is set to false
Thanks
-------------------------
Feel free to respond and request more information, if you find the answer that satisfy your need, Please make sure to mark it as Best Response and like the other.