我们有一个文件pipe理系统,通过一个networking共享访问一个NTFS文件系统上的文件有数百万个。 一个服务帐户需要所有这些文件的完全许可,应用程序代理需要使用此服务帐户进行访问。
在数据迁移期间,发生了一些情况,现在权限不一致。
我一直在尝试在PowerShell中编写一个脚本来确定哪些文件没有适当的ACE,但是get-acl有点痛苦。
我尝试了各种组合类似于:
get-childitem -recurse | get-acl | select -expandproperty access | where { $_.$_.IdentityReference -notcontains $principal
其中$ Principal是需要domain\user格式许可的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
请在运行生产之前在文件的较小子集上进行testing;-)
如果要确保添加新的显式ACE(即使该帐户可能具有已inheritance的权限),请筛选出inheritance的访问规则,如下所示:
$ACEs = $ACL.GetAccessRules($true, $false ,[System.Security.Principal.NTAccount])
注意第二个布尔参数现在是$false表明不应该返回inheritance的规则