Forum Discussion
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 getting the parent folder and inheriting the permission to the new folder from parent. It is all working good. But i would need to get parent's parent in few of the cases.
Use Case :
New Folder : \\localhost\TestShare\s\test6
test6 : is the new folder i am creating.
My current script gives the parent folder as s and test6 inherits the permissions from 's' folder. But i would need to get the parent's parent (in this case it would be TestShare) if the length of the parent is one in few cases.
Is there a way to get it in my current script without interrupting the current logic.
Get-Date
$the_shares = args[0]
$allFolders=Get-ItemProperty -Path $the_shares
$allFolders.Parent.Name
$Permission=get-acl -Path $the_shares
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $the_shares -AclObject $Permission
Thanks in advance.
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.
17 Replies
- farismalaebSteel ContributorHi, Any update on the case, if its OK and no more input is require please select the best response
- suren424Copper Contributor
Thank youfarismalaeb and Wesley Baker for your inputs and guidance.
Appreciate all your help and support.
- farismalaebSteel 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- suren424Copper 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.
- farismalaebSteel 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