Powershell Error

John Kelly 166 Reputation points
2024-08-14T20:49:44.7333333+00:00

Hi

I am running the following script to get folders and their permissions on file shares. This returns all the folders and their permissions in a csv file.

$OutFile = "C:\Temp\Permissions.csv" # Insert folder path where you want to save your file and its name

$RootPath = "\wha-fp-1\Share\Housing Management" # Insert your share directory path

$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"

# $Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags,FilesystemRights"

$FileExist = Test-Path $OutFile

If ($FileExist -eq $True) {Del $OutFile}

Add-Content -Value $Header -Path $OutFile

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

foreach ($Folder in $Folders){

$ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access }

Foreach ($ACL in $ACLs){

$OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags

# If you need detailed file system rights in your report, add the following at the end of previous line:

# + "," + ($ACL.FileSystemRights -replace ',','/' )

Add-Content -Value $OutInfo -Path $OutFile

}}

When i change the file path to "wha-fp-1\Share\Development" and run the script it fails with errors and stops running. I checked and the folders exist on the share drive so not sure why its failing unless the script doesnt like the length of the UNC path but i am not sure and that's why i need help.

Powershell Error

Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Seekell, Roger 66 Reputation points
    2024-09-10T16:58:29.0333333+00:00

    Hi, John. Thank you for your question.

    That does look like you are hitting the 256 character limit on paths in PowerShell. It should only throw an error on the ones longer than 256 characters, and your variable $folders will probably have some folders in it.

    This is a great article that helped me overcome the same problem. https://igorpuhalo.wordpress.com/2019/08/29/overcoming-long-path-problem-in-powershell/

    To handle file paths over 256 characters, you would make two changes. The one is to change $RootPath to start with \?\UNC\wha-fp-1, and the second would be to add -LiteralPath, so:

    dir -LiteralPath $RootPath -recurse

    Please try it out and let me know!

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.