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 $PermissionIn 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 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
}
Thank you very much Wesley Baker
The script is working. But if $the_shares has more than 1, it is throwing error. Do i need to use for loop ?
If yes, i am bit confused on what to use in the for loop.could you please help me on this one too.
$the_shares = ('C:\OIMShare2\a\test\', 'C:\OIMShare2\test\')
Thanks in advance
- Wesley BakerOct 12, 2020Copper Contributor
suren424 Sure, Here is how I would handle that:
function setPermissions([string]$theshare) { $allFolders=Get-ItemProperty -Path $theshare 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 } } #Method 1 - Hardcoded array Write-Host "Method 1 - Hardcoded array" -ForegroundColor Green $the_shares = ('C:\Share2\a\test','C:\Share1\abc1') foreach ($f in $the_shares) { setPermissions $f } #Method 2 - Iterate a folder and process all sub folders Write-Host "Method 2 - Iterate a folder and process all sub folders" -ForegroundColor Green Get-ChildItem -Path "C:\share1" | foreach{setPermissions $_.FullName}