繁体   English   中英

如何使用powershell获取共享文件夹级别的权限

[英]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"

在上面的脚本中,我能够获得我所有的文件夹结构的权限,但我想使用自定义参数,比如如果我只想文件夹级别 3,它应该让我得到如下输出

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

不喜欢

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

如果您使用的是 PowerShell 5.0,则可以结合使用-Recurse-Depth参数:

[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

如果这不是一个选项,您可以尝试以下操作:

[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

请参阅限制 Get-ChildItem 递归深度

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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