【问题标题】:How to get the share folder level permission using powershell如何使用powershell获取共享文件夹级别权限
【发布时间】:2021-09-05 09:27:10
【问题描述】:
#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

【问题讨论】:

  • 请注意,这与typescript无关。我已经删除了那个标签。

标签: windows powershell powershell-4.0


【解决方案1】:

如果您使用的是 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

Limit Get-ChildItem recursion depth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2022-11-22
    • 2019-07-07
    相关资源
    最近更新 更多