在PowerShell中列出特定用户的文件夹权限

时间:2019-04-17 05:38:53

标签: powershell permissions active-directory powershell-v2.0 acl

我是Powershell的新手,我需要知道如何列出特定用户的文件夹的所有权限。这是我到目前为止发现的:

$ReferenceAccountName = 'DOMAIN\Username' 
[string[]]$SearchDirectories
= @('X:\SomeDirectory', 'F:\AnotherDirectory')

foreach ($RootDir in $SearchDirectories) {
    $DirACL = Get-Acl -Path $RootDir
    foreach ($ACL in $DirACL.Access){
        if ($ACL.IdentityReference -like $ReferenceAccountName){
            Write-Output $RootDir
        }
    }
    foreach ($Directory in (Get-ChildItem -Path $RootDir -Recurse | `
                            Where-Object -FilterScript {$_.Attributes `
                            -contains 'Directory'})){
        $DirACL = Get-Acl -Path $Directory.FullName
        foreach ($ACL in $DirACL.Access){
            if ($ACL.IdentityReference -like $ReferenceAccountName){
                Write-Output $Directory.FullName
            }
        }
    } }

但是,使用Write-Output命令不会返回任何值。将其更改为Write-Host不能正常工作。我是否缺少一些关键部分?

我正在使用Powershell V2.0

1 个答案:

答案 0 :(得分:0)

If I understood your question correctly, this should make it:

$user = "domain\username"
$path = "C:\Users\daisies"
$list = Get-ChildItem $path -Recurse | Where-Object {(Get-Acl $_.FullName).Access | Where-Object {$_.IdentityReference -eq $user} }
Write-Output $list

This outputs a list with your permission, LastWriteTime and filename.