列出所有文件夹,其中用户(管理员除外)允许完全访问权限

时间:2016-03-06 10:16:58

标签: powershell acl fileserver

在我们公司的文件服务器上,我们几年前曾经拥有过多的权限。 意思是,存在用户倾向于拥有完全权限的文件夹。这是一件坏事(用户玩权利,锁定系统(以及使用它进行备份)并且只允许自己访问。

我的目标:

按文件夹扫描文件服务器文件夹(文件太多)并输出

  • 文件夹完整路径
  • 安全身份参考

如果有人除了域管理员或系统之外还有其他功能。

输出可以正常:

Path, ACL
E:\share\projectfolder, Domain\10JohnDoe
E:\share\commonfolder, Domain\Everyone
...

这就是我所拥有的,但还远远不够:

##define variable
$path = "E:\Share"
## begin script

foreach ($file in Get-Childitem $path -Recurse -Directory) {
    if (Get-Acl $file.FullName |
        select -ExpandProperty Access |
        where {$_.IdentityReference -notlike "AT\Domain Admins" -and
            $_.IdentityReference -notlike "NT AUTHORITY\SYSTEM" -and
            $_.AccessControlType -like "Allow" -and
            $_.FileSystemRights -like "FullControl"}
    ) {
        Write-Host $file.FullName >> e:\check_acl.txt
        Get-Acl $file.FullName |
            select -ExpandProperty Access |
            where {$_.IdentityReference -notlike "AT\Domain Admins" -and
                $_.IdentityReference -notlike "NT AUTHORITY\SYSTEM" -and
                $_.AccessControlType -like "Allow" -and
                $_.FileSystemRights -like "FullControl"
            } >> e:\check_acl.txt
    }
}

但我想,我无法得到输出(进入文件!)。

1 个答案:

答案 0 :(得分:1)

Write-Host仅向控制台显示文本,无法保存。 Get-Acl >> check_acl.txt将写出整个“对象”,而不仅仅是身份引用。你想要的是创建一个带有路径和ACL(identityreference)属性的自定义对象,并将其导出到csv。

我还简化了您的身份排除,并将if-test更改为foreach-loop,因此您无需再运行整个Get-ACL行。

试试这个:

##define variable
$path = "E:\Share"
$ExcludeUsers = 'AT\Domain Admins','NT AUTHORITY\SYSTEM','AT\AT-IT','AT\01STREW','AT\01BRUND','AT\01KNAFP','AT\01BECKC'
## begin script

#Create regex-pattern to match all excluded users
$ExcludeUsersRegex = ($ExcludeUsers | % { [regex]::Escape($_) }) -join '|'


Get-Childitem $path -Recurse -Directory | ForEach-Object {
    $file = $_

    Get-Acl -Path $file.FullName |
    Select-Object -ExpandProperty Access |
    Where-Object {$_.IdentityReference -notmatch $ExcludeUsersRegex -and $_.AccessControlType -eq "Allow" -and $_.FileSystemRights -eq "FullControl"} |
    ForEach-Object {
        #Foreach ACL
        New-Object psobject -Property @{
            Path = $file.FullName
            ACL = $_.IdentityReference
        }
    }
} | Select-Object -Property Path, ACL | Export-Csv e:\check_acl.csv -NoTypeInformation

示例输出:

"Path","ACL"
"C:\Users\frode\Desktop\TEST\YES","CONTOSO\frode"
"C:\Users\frode\Desktop\TEST\YES","CONTOSO\FrodesOtherAccount"