Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to write a script in powershell, that will find folders recursivly and based on the folder name replace the ACL on the folder.

I have folders already with the correct permissions. These I use as template folders. Folder1 Folder2 - Subfolder1 - Subfolder2

There are literally thousands of folders, all with the same names, and i would like to apply the same set of permissions to all of the like named folders. I have worked on tackling this a few different ways but I havent found a good way to handle the sub-folders and recursion.

Here is a sample of the first way I tried to tackle this.

$Prop = Get-Acl "Template\Subfolder1"
$Engd = Get-Acl "Template\Subfolder2"

foreach ($file in $(Get-ChildItem Folder2 -recurse)) {
  If ($_ = "Subfolder1") {
    set-acl $_ $Prop
    }
  If ($_ = "Subfolder2") {
    set-acl $_ $Engd
    }
}

The further I get along with trying to write this the more I realize its going to be harder and harder. I have hundreds of folders named similar to Folder2 Folder3..... FolderN, all with the same folder structure and folder names inside. I just want something simple that will match a folder name and based on the name assign a certain predifined ACL.

Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Untested but how about something like this:

$acls = @{};
Get-ChildItem Template |
    Where-Object { $_.PSIsContainer } |
    ForEach-Object {
    	$acls[$_.Name] = Get-Acl $_.FullName ;
    }

Get-ChildItem Folder2 -Recurse |
    Where-Object { $_.PSIsContainer -and $acls.ContainsKey($_.Name) } |
    ForEach-Object {
    	Set-Acl $_.FullName $acls[$_.Name] ;
    }
share|improve this answer
    
First I added -recurse after Template on the second line, because my folder structure is 3 lvls deep. Then I ran it. I get this error. Get-Acl : Cannot find path 'Subfolder 1' because it does not exist. At C:\scripts\aclt.ps1:5 char:33 + $acls[$_.Name] = Get-Acl <<<< $_ ; I think it is because when you use Get-Acl it does not use the full path of the current folder its working with. But I dont know. –  user10711 Jul 2 '09 at 22:14
    
If in your template folder Parent1 and Parent2 both have a subfolder called Child1 but with different ACLs then the script will need to be improved to check the parent hierarchy to ensure the correct ACL is applied. –  Jason Stangroome Jul 2 '09 at 22:36
    
Yea no. None of the subfolders in the template folder share the name name. By the way I get the same error for every folder in my template folder. The only thing that changes is the name of the folder it appears to be pulling the acl from. Then an error when it trys to apply it. –  user10711 Jul 2 '09 at 22:41
    
I modified it slightly. Added a write-host $_.FullName right before the $acls[$_.Name] = Get-Acl $_.FullName and it printed off the name of the current folder. Which seems correct, but when it tries to grab the ACL it doesnt seem to work. –  user10711 Jul 2 '09 at 22:57
    
Works like a charm, after changing it to Set-Acl $_.Fullname –  user10711 Jul 2 '09 at 23:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.