user3772927
user3772927

Reputation: 27

Get ACL subfolders using array

I have a script that checks ACL permissions in folders and writes these to a CSV file. I want the output when inherited to come out like this:

Allow  Full Control, this folder, subfolders and files (Inherited)

At the moment its only outputting this:

Allow FullControl,  (Inherited)

Can someone advise me please I just want the missing text to display:

$RootPath = "C:\"

$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}

$isInherited = @{
    $true  = 'Inherited'
    $false = 'Not Inherited'
}

$inheritance = @{
    0 = 'files only'
    1 = 'this folder and subfolders'
    2 = 'this folder and files'
    3 = 'subfolders and files'
}

$fldr = $Folder.FullName

$Folders | % {
    $fldr = $_.FullName
    Get-Acl $fldr | select -Expand Access |
        select @{n='Account';e={$_.IdentityReference}},
               @{n='Ace String';e={"{0} {1}, {2} ({3})" -f $_.AccessControlType,
                 $_.FileSystemRights, $inheritance[$_.InheritanceFlags],
                 $isInherited[$_.IsInherited]}},
               @{n='Object Path';e={$fldr}} |
        ConvertTo-Csv -NoTypeInformation |
        Select -Skip 1 | % {$_ -replace '"', ""} |
        Out-File $OutFile -Force -Encoding ascii -Append
}

Upvotes: 1

Views: 1583

Answers (1)

Raf
Raf

Reputation: 10097

On inspecting System.Security.AccessControl.InheritanceFlags object:

   TypeName: System.Security.AccessControl.InheritanceFlags

Name        MemberType Definition
----        ---------- ----------
CompareTo   Method     int CompareTo(System.Object target)
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode()
ToString    Method     string ToString(), string ToString(string format, System.IFormatProvider provider), string To...
value__     Property   System.Int32 value__ {get;set;}

it appears the object has value__ property. So, change line

$_.FileSystemRights, $inheritance[$_.InheritanceFlags],

to

$_.FileSystemRights, $inheritance[$_.InheritanceFlags.value__],

This will correctly pass parameter of type [int] to $inheritance hash.

Upvotes: 2

Related Questions