0
votes

I have Windows Server 2016 Datacenter (64 bit) as a File Server (contains several Shared folder & subfolders).

I want to make a list OR export user Folder Structure along with permissions ( Read, Modify, Full .. etc..)

I tried with below PS script but I am getting an error message with I have mentioned after the script.

Powershell

$FolderPath = dir -Directory -Path "E:\Project Folders\#Folder_Name" -Recurse -Force 
$Report = @() 
Foreach ($Folder in $FolderPath) {     
    $Acl = Get-Acl -Path $Folder.FullName     
    foreach ($Access in $acl.Access)
    {
        $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
        $Report += New-Object -TypeName PSObject -Property $Properties
    }
} 
$Report | Export-Csv -path "C:\Folder Permissions\Folder Name.csv" 

Error:

dir : Access to the path 'E:\Project Folders**Folder Path**\New folder' is denied. At C:\Users\Administrator\Documents\PS Script**File Name**.ps1:1 char:15 + ... olderPath = dir -Directory -Path "E:\Project Folders**Folder Name**" -Re ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (E:\Project Fold...ngar\New folder:String) [Get-Child Item], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Please help me out!

Thanks in Advance

1
You don't have access to one of the folders. Did you copy/paste the error message exactly as is? Does it really contain **Folder Path** , **Folder Name** and **File Name**. - Lieven Keersmaekers
The error, PermissionDenied says that the account that runs the script does not have permission to whatever the folder path is. Is it being invoked by privileged account? - vonPryz
I have not copied the exact path and file name, I have modified it. - Kishore B L
I have logged in with Administrator. - Kishore B L

1 Answers

0
votes

As noted by the other comments.

This is not a PowerShell error/issue, it is a permissions one. The same thing can/will happen if you say you did this use case on the Windows folder tree.

Since you know this will happen, either fix the permissions on the tree you are working on or do this.

Get-ChildItem -Directory -Path 'C:\Windows\System32' -Recurse -ErrorAction SilentlyContinue

or if you want to just stop when a path fails.

# Treat non-terminating erros as terminating
$RootFolderUnc = 'C:\Windows\System32'

Try {Get-ChildItem -Directory -Path $RootFolderUnc -Recurse -ErrorAction Stop}
Catch [System.UnauthorizedAccessException] 
{
    Write-Warning -Message "$env:USERNAME. You do not have permissions to view this path."
    $_.Exception.Message
}