简体   繁体   中英

How to get the share folder level permission using powershell

#Cred= Get-Credential
$FolderPath = dir -Directory -Path "\\abc\dc\da" -Recurse -Force | where {$_.psiscontainer -eq $true}
$Report = @()
Foreach ($Folder in $FolderPath) {
    $Acl = Get-Acl -Path $Folder.FullName
   # $Name= Get-ChildItem $Folder -Recurse | where {!$_.PSIsContainer} |select-object 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:\Output\Test_16-06-2021.csv"

In the above script, I'm able to get all my folder structure with permission but I want to do with a custom parameter like if I want to only folder level 3 it should get me the output as below

\\abc\a\b\c
\\abc\a\c\d

not like

\\abc\a\b\c\d\text.txt
\\abc\a\c\d\e\f\g\demo.pdf

If you're using PowerShell 5.0, you can use the -Recurse and -Depth parameters combined:

[string]$rootFolder = Read-Host -Prompt "Enter root folder path (no trailing '\')"
[int]$recursionDepth = Read-Host -Prompt "Enter recursion depth"
[string]$outputCsv = Read-Host -Prompt "Enter output .csv file (full path)"
$folders = Get-ChildItem -Directory -Path $rootFolder -Recurse -Depth $recursionDepth -Force | Where-Object { $_.PSIsContainer -eq $true }
[array]$report = @()

foreach ($folder in $folders) {
    $acl = Get-Acl -Path $folder.FullName
    foreach ($access in $acl.access) {
        [hashtable]$properties = [ordered]@{
            'FolderName'       = $folder.FullName;
            'AD Group or User' = $access.IdentityReference;
            'Permissions'      = $access.FileSystemRights;
            'Inherited'        = $access.IsInherited 
        }
        $report += New-Object -TypeName PSObject -Property $properties
    }
}
Write-Host $report
$report | Export-Csv -Path $outputCsv

If that isn't an option, you can try the following:

[string]$rootFolder = Read-Host -Prompt "Enter root folder path (no trailing '\')"
[int]$recursionDepth = Read-Host -Prompt "Enter recursion depth"
[string]$outputCsv = Read-Host -Prompt "Enter output .csv file (full path)"
[string]$recursionStr = if ($recursionDepth -eq 0) { "\*" } else { ("\*" * ($recursionDepth + 1)) }
$folders = Get-ChildItem -Directory -Path "$rootFolder$recursionStr" -Force | Where-Object { $_.PSIsContainer -eq $true }
[array]$report = @()

foreach ($folder in $folders) {
    $acl = Get-Acl -Path $folder.FullName
    foreach ($access in $acl.access) {
        [hashtable]$properties = [ordered]@{
            'FolderName'       = $folder.FullName;
            'AD Group or User' = $access.IdentityReference;
            'Permissions'      = $access.FileSystemRights;
            'Inherited'        = $access.IsInherited
        }
        $report += New-Object -TypeName PSObject -Property $properties
    }
}
Write-Host $report
$report | Export-Csv -Path $outputCsv

See Limit Get-ChildItem recursion depth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM