ACL Permissions powershell script erroring out and not providing results in created files

Here is our script:
Function Get-ACE {            
Param (            
        [parameter(Mandatory=$true)]            
        [string]            
        [ValidateScript({Test-Path -Path $_})]            
        $Path            
)            
           
    $ErrorLog = @()            
           
    Write-Progress -Activity "Collecting folders" -Status $Path `
        -PercentComplete 0            
    $folders = @()            
    $folders += Get-Item $Path | Select-Object -ExpandProperty FullName            
 $subfolders = Get-Childitem $Path -Recurse -ErrorVariable +ErrorLog `
        -ErrorAction SilentlyContinue |            
        Where-Object {$_.PSIsContainer -eq $true} |            
        Select-Object -ExpandProperty FullName            
    Write-Progress -Activity "Collecting folders" -Status $Path `
        -PercentComplete 100            
           
    # We don't want to add a null object to the list if there are no subfolders            
    If ($subfolders) {$folders += $subfolders}            
    $i = 0            
    $FolderCount = $folders.count            
           
    ForEach ($folder in $folders) {            
           
        Write-Progress -Activity "Scanning folders" -CurrentOperation $folder `
            -Status $Path -PercentComplete ($i/$FolderCount*100)            
        $i++            
           
        # Get-ACL cannot report some errors out to the ErrorVariable.            
        # Therefore we have to capture this error using other means.            
        Try {            
            $acl = Get-ACL -LiteralPath $folder -ErrorAction Continue            
        }            
        Catch {            
            $ErrorLog += New-Object PSObject `
                -Property @{CategoryInfo=$_.CategoryInfo;TargetObject=$folder}            
        }            
           
        $acl.access |            
            Where-Object {$_.IsInherited -eq $false} |            
            Select-Object `
                @{name='Root';expression={$path}}, `
                @{name='Path';expression={$folder}}, `
                IdentityReference, FileSystemRights, IsInherited, `
                InheritanceFlags, PropagationFlags            
           
    }            
               
    $ErrorLog |            
        Select-Object CategoryInfo, TargetObject |            
        Export-Csv ".\Errors_$($Path.Replace('\','_').Replace(':','_')).csv" `
            -NoTypeInformation            
           
}            
           
# Call the function for each path in the text file            
Get-Content .\paths.txt |            
    ForEach-Object {            
        If (Test-Path -Path $_) {            
            Get-ACE -Path $_ |            
                Export-CSV `
                    -Path ".\ACEs_$($_.Replace('\','_').Replace(':','_')).csv" `
                    -NoTypeInformation            
        } Else {            
            Write-Warning "Invalid path: $_"            
        }            
    }  

Here is an example of our error code.
Export-CSV : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the
directory name must be less than 248 characters.
At C:\Users\c51136a\Desktop\Power shell Stuff\Test1.ps1:65 char:17
+                 Export-CSV `
+                 ~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], PathTooLongException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
 
Export-CSV : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the
directory name must be less than 248 characters.
At C:\Users\c51136a\Desktop\Power shell Stuff\Test1.ps1:65 char:17
+                 Export-CSV `
+                 ~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], PathTooLongException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
Member_2_6244776Systems EngineerAsked:
Who is Participating?
 
footechConnect With a Mentor Commented:
I think the error is pretty clear.  The path (including the filename) that you're providing for the .CSV file for the Export-CSV cmdlet is too long.  Since you're writing the files in the current directory, it may be enough for you to run the script from a shorter path (like C:\temp), or you could code in the output directory specifically.  Try this for the last bit.
# Call the function for each path in the text file            
foreach ( $line in (Get-Content .\paths.txt) )
{
    If (Test-Path -Path $line) {            
        Get-ACE -Path $line |            
            Export-CSV `
                    -Path "c:\temp\ACEs_$($line.Replace('\','_').Replace(':','_')).csv" `
                    -NoTypeInformation            
    } Else {            
        Write-Warning "Invalid path: $line"            
    }            
}

Open in new window


*Edit to avoid variable name reuse.
0
 
PberSolutions ArchitectCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: footech (https:#a42384804)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.