Search code examples
powershellformattingacl

How do I get FileSystemRights in a certain format?


I wrote a little script to give me the access ACLs of a certain path and all its subdirectories and put it in a .txt, but I need it in another format to create a database with easier visibility and access.

Part of the output looks like this:

Fullname S            FileSystemRights S AccessControlType
----------            ------------------ ------------------
C:\temp  ; ReadAndExecute, Synchronize ;             Allow; 

So what I need the output to look like is something like this:

Fullname S FileSystemRights S AccessControlType
---------- ------------------ ------------------
C:\temp  ;   ReadAndExecute ;             Allow;
C:\temp  ;   Synchronize    ;             Allow;

As you can see I need the individual rights in individual lines and not stacked together.

What I've done so far looks is the following, maybe it helps (I left out unimportant stuff):

(Get-Acl $TestPath).Access |
    Format-Table -AutoSize -HideTableHeaders @{L="Fullname";E={$TestPath}},
        @{L="S";E={";"}}, FileSystemRights,
        @{L="S";E={";"}}, AccessControlType,
        @{L="S";E={";"}}, IdentityReference,
        @{L="S";E={";"}}, IsInherited,
        @{L="S";E={";"}}, InheritanceFlags,
        @{L="S";E={";"}}, PropagationFlags |
    Out-File $Out -Append -Width 500

function RecDirs {
    $d = $Args[0]
    $AktRec++
    $dirs = dir $d | where {$_.PsIsContainer}
    if ($AktRec -lt 3) {
        foreach($di in $dirs) {
            if ($di.FullName -ne $null) {
                (Get-Acl $di.Fullname).Access |
                    Format-Table -AutoSize -HideTableHeaders @{L="Fullname";E={$di.FullName}},
                        @{L="S";E={";"}}, FileSystemRights,
                        @{L="S";E={";"}}, AccessControlType,
                        @{L="S";E={";"}}, IdentityReference,
                        @{L="S";E={";"}}, IsInherited,
                        @{L="S";E={";"}}, InheritanceFlags,
                        @{L="S";E={";"}}, PropagationFlags |
                    Out-File $Out -Append -Width 500
            }
            RecDirs($di.Fullname)
        }
    }
}

RecDirs($TestPath)

Solution

  • Split the FileSystemRights property at commas and output one line per element. And you definitely want Export-Csv for writing the output file.

    (Get-Acl $di.Fullname).Access | ForEach-Object {
        foreach ($val in ($_.FileSystemRights -split ', ')) {
            $_ | Select-Object @{n='Fullname';e={$di.FullName}},
                @{n='FileSystemRights';e={$val}}, AccessControlType,
                IdentityReference, IsInherited, InheritanceFlags,
                PropagationFlags
        }
    } | Export-Csv $Out -NoType -Append