簡體   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