MDM*_*rra 7 windows powershell permissions ntfs
我们有一个文档管理系统,它在通过网络共享访问的 NTFS 文件系统上拥有数百万个文件。单个服务帐户需要对所有这些文件和应用程序代理使用此服务帐户进行访问的完全权限。
在数据迁移过程中,发生了一些事情,权限现在不一致。
我一直在尝试在 PowerShell 中编写一个脚本来确定哪些文件没有适当的 ACE,但get-acl有点……痛苦。
我尝试过类似的各种组合:
get-childitem -recurse | get-acl | select -expandproperty access |
where { $_.$_.IdentityReference -notcontains $principal
Run Code Online (Sandbox Code Playgroud)
其中 $Principal 是需要domain\user格式权限的用户。
一定有办法做到这一点,对吧?它是什么?我想保持它原产于PowerShell和不使用icacls或cacls如果可能的话。
您可以这样做(将其分解为更多语句有助于提高可读性):
# 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
Run Code Online (Sandbox Code Playgroud)
在生产中运行之前,请在较小的文件子集上进行测试;-)
如果您想确保添加新的显式 ACE,即使帐户可能已经继承了权限,请过滤掉继承的访问规则,如下所示:
$ACEs = $ACL.GetAccessRules($true, $false ,[System.Security.Principal.NTAccount])
Run Code Online (Sandbox Code Playgroud)
注意第二个布尔参数现在如何$false指示不应返回继承的规则