'Adding Variables to Powershell Get-Acl output

I am wanting to add the output of a variable to a Powershell cmdlet output

I have the code

Get-ChildItem D:\Powershell* -Recurse | Get-Acl

which returns three properties:

Path                                Owner                          Access                                                                       
----                                -----                          ------
MyFile.ps1                          MyDomain\MyUser                MyDomain\AnotherUser Allow  Modify, Synchronize...  

      

I only want to see Path and Access and I want to add the output $env:ComputerName to each result:

ComputerName      Path                                Access                                                                       
----              ----                                -----                          
MyServerName      MyFile.ps1                          MyDomain\AnotherUser Allow  Modify, Synchronize...  

I have tried

Get-ChildItem D:\Powershell* -Recurse | Get-Acl | Select-Object @{name="Computername"; expression={$env:Computername}}, Path, Access

Which is close, but I am only see the type of the Access Object:

Computername   Path                                                                    Access                           
------------   ----                                                                    ------                           
MyServerName   Microsoft.PowerShell.Core\FileSystem::D:\Powershell Scripts\MyFile.ps1  {System.Security.AccessControl...

I note the path is fully qualified also but that isn't a particular problem

How do I output the access infomation rather than just the object type?



Solution 1:[1]

As Santiago commented, the .Access property is an array of objects, so to get workable output you can for instance save as CSV, you need to loop over these properties.

Perhaps something like this:

$result = Get-ChildItem -Path 'D:\Powershell*' -Recurse -File | ForEach-Object {
    $file = $_.Name  # or $_.FullName if you rather have that
    $comp = $env:COMPUTERNAME
    foreach ($access in (Get-Acl -Path $_.FullName).Access) {
        # output the combined object
        [PsCustomObject]@{
            File         = $file
            ComputerName = $comp
            User         = $access.IdentityReference
            Permissions  = $access.FileSystemRights
            Type         = $access.AccessControlType
            Inherited    = $access.IsInherited
        }
    }
}

$result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Theo