Forum Discussion
suren424
Oct 11, 2020Copper Contributor
Getting a parent's parent for a folder using power shell
Hi, I am using a powershell script to enable inheritance for the folders created in NTFS share. In this script, i am getting the folder created for the user by passing a dynamic value and then ...
- 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.
farismalaeb
Oct 11, 2020Steel Contributor
Hi,
So just for me to understand a point, so do you need to get the parent parent folder incase the parent length is 1?
if so, I updated your code to be
$allFolders=Get-ItemProperty -Path $the_shares
if (($allFolders.Parent.Name).Length -eq 1){
Write-Host "My Length is Only 1, "
Write-Host "Add your condition here :)"
Else{
Write-Host "I am longer than 1 :("
Write-Host "Write another condition here :)"
}
$Permission=get-acl -Path $the_shares
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $the_shares -AclObject $Permission
- suren424Oct 12, 2020Copper Contributor
Thank youfarismalaeb
Yes, if the parent length is 1, i need to get it's parent folder. Below is the explanation with an example :
Example : $the_shares = ('C:\Share2\a\test', 'C:\Share1\abc1\')
Case 1: 'C:\Share2\a\test'
The parent is 'a' and the length=1. So, i need to get it's parent, which is 'Share2' and set the permission on 'test' folder to inherit from 'Share2'.
How can i get the parent parent folder ?
Case 2: 'C:\Share1\abc1'
Here, the parent is 'Share1'. So, i would just need set the permission on 'abc1' folder to inherit from 'Share1'.
$the_shares = ('C:\Share2\a\test', 'C:\Share1\abc1') $allFolders=Get-ItemProperty -Path $the_shares if (($allFolders.Parent.Name).Length -eq 1){ Write-Host "My Length is Only 1," Write-Host "The Parent is " -ForegroundColor Green -NoNewline $allFolders.Parent.Name Write-Host "Add your condition here :)" Else{ Write-Host "I am longer than 1 :(" Write-Host "Write another condition here :)" $Permission=get-acl -Path $the_shares $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $the_shares -AclObject $Permission } }
Thanks in advance.
- farismalaebOct 12, 2020Steel Contributor
Case 1: 'C:\Share2\a\test'
The parent is 'a' and the length=1. So, i need to get it's parent, which is 'Share2' and set the permission on 'test' folder to inherit from 'Share2'.
How can i get the parent parent folder ?
Just a quick tip, test folder cannot get its permission from Share2, it will get its permission only from a unless you chose to copy the permission, test will only get its permission from a.
So to make sure that the script work, a must inherit its permission from share2 first and so on test
- suren424Oct 12, 2020Copper Contributor
Thanksfarismalaeb .
I tested the script few more times and yes it is not inheriting the permissions from parents parent.
So, will the below logic work ?
If the parent length is one, set the permission on parent to inherit from it's parent and then set the homefolder to inherit from it's parent.
Example :
HomeFolder : C:\OIMShare2\b\test\
Here the 'test' parent would be 'b'. so, inherit the permission from OIMShare2 to 'b' and after that set the permissions on 'test' to inherit from 'b'.
$the_shares = 'C:\OIMShare2\b\test\' $allFolders=Get-ItemProperty -Path $the_shares if (($allFolders.Parent.Name).Length -eq 1){ Write-Host "My Length is Only 1" Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Parent.Name $Permission=get-acl -Path $allfolders.Parent.Parent.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allfolders.FullName -AclObject $Permission } Else{ Write-Host "The Parent Length is longer than 1" Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name $allFolders.Parent.Name $Permission=get-acl -Path $allfolders.Parent.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allfolders.FullName -AclObject $Permission }
Thanks in advance
- Wesley BakerOct 12, 2020Copper Contributor
Hi suren424,
Try this:
#$the_shares = ('C:\Share2\a\test') $the_shares = ('C:\Share1\abc1') $allFolders=Get-ItemProperty -Path $the_shares if (($allFolders.Parent.Name).Length -eq 1){ Write-Host "The Parent Length is Only 1" Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Parent.Name $Permission=get-acl -Path $allfolders.Parent.Parent.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allfolders.FullName -AclObject $Permission } Else{ Write-Host "The Parent Length is longer than 1" Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name $Permission=get-acl -Path $allfolders.Parent.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allfolders.FullName -AclObject $Permission }
- suren424Oct 14, 2020Copper Contributor
While going through the script, i had a question. How is the script getting the permission from the parent and inheriting the same. I couldn't figure it out. Am i giving the $the_shares variable in the wrong position?
Please advise.
I tried the below 2 scripts, both seem to give the same result. couldn't get the difference.
Script 1:
$the_shares = 'C:\OIMShare3\test1' $allFolders=Get-ItemProperty -Path $the_shares Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name $allFolders.Parent.Name $Permission=get-acl -Path $the_shares $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $the_shares -AclObject $Permission
Script 2:
$the_shares = 'C:\OIMShare3\test2' $allFolders=Get-ItemProperty -Path $the_shares Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name $allFolders.Parent.Name $Permission=get-acl -Path $allFolders.Parent.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $allFolders.FullName -AclObject $Permission
Thanks in advance.