AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 589467
Accepted
MDMarra
MDMarra
Asked: 2014-04-17 06:42:47 +0800 CST2014-04-17 06:42:47 +0800 CST 2014-04-17 06:42:47 +0800 CST

使用 PowerShell 评估 NTFS ACL 上的当前 ACE

  • 772

我们有一个文档管理系统,它在 NTFS 文件系统上通过网络共享访问数百万个文件。单个服务帐户需要对所有这些文件和应用程序代理使用此服务帐户进行访问的完全权限。

在数据迁移期间,发生了一些事情,权限现在不一致。

我一直在尝试在 PowerShell 中编写一个脚本来识别哪些文件没有适当的 ACE,但get-acl有点……痛苦。

我尝试了各种类似的组合:

get-childitem -recurse | get-acl | select -expandproperty access | 
where { $_.$_.IdentityReference -notcontains $principal

其中 $Principal 是需要domain\user格式权限的用户。

一定有办法做到这一点,对吧?它是什么?我想将其保留在 PowerShell 中,并且尽可能不icacls使用cacls。

windows
  • 1 1 个回答
  • 2621 Views

1 个回答

  • Voted
  1. Best Answer
    Mathias R. Jessen
    2014-04-17T06:57:38+08:002014-04-17T06:57:38+08:00

    您可以这样做(将其分解为更多语句有助于提高可读性):

    # Go to the directory and find the files
    Push-Location "C:\MDMarrasFiles"
    $Files = Get-ChildItem -Recurse
    
    # Create an IdentityReference and a FullControl FileSystemAccessRule for said identity
    $Principal = New-Object System.Security.Principal.NTAccount("DOMAIN\user")
    $FullControlACERule = New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList ($Principal,"FullControl","Allow")
    
    # Go through the files
    foreach($File in $Files)
    {
        # Get the current ACL on the file
        $ACL = Get-ACL $File
    
        # Extract the ACEs, both explicit on the file and inherited 
        $ACEs = $ACL.GetAccessRules($true,$true,[System.Security.Principal.NTAccount])
    
        # Filter the ACEs to extract those giving FullControl to your target user
        $ACEsMatching = $ACEs |Where {`
            $_.FileSystemRights -eq "FullControl" -and `
            $_.IdentityReference -eq $objUser -and `
            $_.AccessControlType -eq "Allow"` 
        }
    
        # Test if there where no such ACE to be found
        if($ACEsMatching.Count -eq 0)
        {
            # Add the FullControl Rule to the current ACL
            $ACL.AddAccessRule($FullControlACERule)
    
            # Write the new ACL back to the file
            Set-ACL $File -AclObject $ACL 
        }
    }
    Pop-Location
    

    请在生产中运行之前对较小的文件子集进行测试;-)

    如果您想确保添加新的显式 ACE,即使帐户可能已经继承了权限,请过滤掉继承的访问规则,如下所示:

    $ACEs = $ACL.GetAccessRules($true, $false ,[System.Security.Principal.NTAccount])
    

    请注意第二个布尔参数现在如何$false指示不应返回继承的规则

    • 8

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果 Windows 服务崩溃,如何自动重新启动它?

  • 无法安排任务(访问被拒绝)

  • 物理机重启时自动重启虚拟机(VMWare)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve