1
votes

I am recursively getting a list of folders with their respective permissions in a powershell script, however when the recursive part happens my output string keeps printing the folder structure each time an example of this is:

I have a folder called C:\temp, within that folder are 2 empty folders C:\temp\folder1 and C:\temp\folder2. With my script the output would be:

I have left out the permissions for readability

C:\temp
C:\temp\folder1
C:\temp
C:\temp\folder2

I don't want this to happen I want a list of folders with their permissions and then if the permissions on a child folder are different then look at the get the child folders of that folder. This works apart from the string building which I think I need a fresh pair of eyes to look at it because I'm getting nowhere.

Appreciate the help in advance,

Sam

CODE:

Add-Type -AssemblyName System.Windows.Forms
Import-Module ActiveDirectory
$info = ""
$OutputString
$step = 0
function DisplayForm{

#Some GUI code
#$textBox takes in the base folder from the user

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $baseFolder = $textBox.Text

        $ParentProperties = (Get-Acl $baseFolder).Access| Select-Object -ExpandProperty IdentityReference
        $OutputString = $OutputString + $baseFolder + "`r`n" + $ParentProperties + "`r`n`r`n"
        $ChildFolders = Get-ChildItem $baseFolder | where {$_.Attributes -eq 'Directory'}

        FindPriorities($baseFolder)


        $info = "SAVED TO FOLDER"
        outputList

    }
}

function FindPriorities{
    param($fileName)
    $ChildFolders = Get-ChildItem $fileName | where {$_.Attributes -eq 'Directory'}
    $step = $step + 1
    $TempString = ""
    foreach ($folder in $ChildFolders){
        $child = $fileName + "\\" + $folder.name

    $ParentProperties = (Get-Acl $fileName).Access| Select-Object -ExpandProperty IdentityReference
    $ChildProperties = (Get-Acl $child).Access| Select-Object -ExpandProperty IdentityReference
    $parentString=""

    foreach ($p in $ParentProperties){
        $parentString= $parentString + $p
    }
    $childString=""
    foreach ($c in $childProperties){
        $childString = $childString + $c
    }

    if($childString -ne $parentString){
        $OutputString = $OutputString + $child + "`r`n" + $ChildProperties + "`r`n`r`n"
        FindPriorities ($child)
    }else{
        $OutputString = $OutputString + $child + "`r`n" + $ChildProperties + "`r`n`r`n"
    }

}
}

function outputList{
    $OutputString

}


DisplayForm
1
You have the same foreach code in DisplayForm that's in FindPriorities so its all being run twice. (foreach ($folder in $ChildFolders){ ... }). Your also calling DisplayForm again at the end of the function DisplayForm - Richard
Yes that's just for the sub folders in the base folder, then if it has one that has different permissions it goes into that and finds the sub folders - Sam Lucas
---folder1 -----------folder 2(Same permissions) -----------folder 3(Different Permissions) --------------------------------------folder 4...... and so on. I always want it to come back and check through the rest of the folders in the base folder that's why I have it like this - Sam Lucas
Could you not just give the root search folder ($baseFolder) to FindPriorities and let it deal with all the child folders. Then you wouldn't need the duplicate code in DisplayForm? - Richard
I could, I'll give that a go now, but wouldn't that then skip over any remaining folders in the base if one is found to have different permissions? - Sam Lucas

1 Answers

2
votes

I think I understood what you want to do.

Please give this snippet a try:

function Get-IdentityReference($path) {    
    Get-Acl $path |
        Select-Object -ExpandProperty Access |
        Select-Object -ExpandProperty IdentityReference    
}

function Extract-Permissions($baseFolder) {
    $folders = Get-ChildItem $baseFolder | Where-Object { $_.PSisContainer }
    $baseACL = Get-IdentityReference $baseFolder

    "$baseFolder : $baseACL"

    foreach($folder in $folders) {
        $folderACL = Get-IdentityReference $folder.FullName
        $childFolders = Get-ChildItem $folder.FullName | Where-Object { $_.PSisContainer }

        "$($folder.FullName) : $folderACL"

        foreach($childFolder in $childFolders) {
            $childACL = Get-IdentityReference $childFolder.FullName

            if(Compare-Object $childACL $folderACL) {
                Extract-Permissions $childFolder.FullName
            } else {
                "$($childFolder.FullName) : $childACL"
            }
        }
    }
}

$baseFolder = "$env:USERPROFILE\Desktop"

Extract-Permissions $baseFolder