【发布时间】:2018-07-13 07:07:08
【问题描述】:
我制作了一个脚本,它必须遍历数千个 AD 用户主目录 一个一个,基本上每一个都做以下步骤:
- 取得文件夹的所有权
- 为域管理员组添加访问规则
- 归还文件夹的所有权
- 循环遍历所有子文件夹和文件,启用继承和删除 所有明确的权限
经过大量测试和问题解决后,脚本完美运行,除了一个让我头撞墙的问题。
脚本成功循环了大约 50-150 个文件夹(非常随机),然后导致以下错误:"the trust relationship between the primary domain and the trusted domain failed"
我构建了一个额外的循环,当此错误发生时将重试 30 次(每 30 秒)。但是,这无济于事,因为只要脚本运行,信任关系就会丢失。
最有趣的部分是,一旦我再次运行脚本,(从问题文件夹开始)该文件夹被处理而没有进一步的错误。该脚本再也不会卡在同一个文件夹中。但后来又发生了这种情况,比如 50 个文件夹之后。
这是一个巨大的不便,因为我需要处理至少 15,000 个用户文件夹,并且当 1 个失败时,我总是需要编译一个新的“要处理的文件夹”列表。
这里是基本的代码功能,我去掉了所有不必要的错误处理和重试循环以获得更好的可读性:
foreach ($folder in $homeFoldersFound) {
$accessControl = Get-Acl -LiteralPath $folder.FullName -ErrorAction Stop
#Current owner
$folderOwner = $accessControl.Owner
#Take ownership for the user running the script
$accessControl.SetOwner([System.Security.Principal.NTAccount]$currentUser)
#Access rule to add
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($groupToAdd,"FullControl","ContainerInherit,ObjectInherit", "None", "Allow")
$accessControl.AddAccessRule($accessRule)
#Purge current explicit permissions
$accessControl.SetAccessRuleProtection($true, $false)
#Apply ownership and access rules
set-acl -AclObject $accessControl -LiteralPath $folder.FullName -ErrorAction Stop | Out-Null
#Return the previous ownership and apply
$accessControl.SetOwner([System.Security.Principal.NTAccount]$folderOwner)
$accessControl.SetAccessRuleProtection($false, $false)
set-acl -AclObject $accessControl -LiteralPath $folderItem -ErrorAction Stop | Out-Null
#Loop through child items, enable inheritance & remove explicit permissions
foreach ($item in (Get-ChildItem -LiteralPath $folder.FullName -Recurse -ErrorAction Stop)) {
#More code
}
}
同样,代码不应该有任何问题,因为错误发生得如此随机,并在再次运行脚本时通过。关于可能导致此问题/如何解决此问题的任何想法?
感谢所有帮助!
【问题讨论】:
-
您是否在具有本地路径文件夹的机器上运行脚本?例如D:\sharefoldername\users\ ?当我在我的文件服务器中查询每个文件夹的权限时,我遇到了同样的问题,当我从文件服务器(2012 R2)运行它时我没有遇到问题。如果我想从我的机器上运行它,我会运行一个 get-childitem -path 目录,将这些结果导出到多个 txt 文件中,然后使用每个文本文件作为输入运行我的权限脚本。希望你明白我在说什么。
-
$homeFoldersFound是指通用域名还是特定域服务器?如果您指的是通用域名,我猜您与之交谈的服务器可能会突然更改并且尚未被复制。 -
$userFolder = "\\AD.l\path\to\folders"$homeFoldersFound = (Get-ChildItem -LiteralPath $userFolder -Directory -Force)这是一个 DFS 文件系统,我无法从本地计算机访问文件夹。如您所见,我需要使用以 '\\' 开头的共享路径 -
@iRon 您是否建议不要使用“\\domain.local\path\folders\”,例如,我应该使用“\\domainDC3\path\folders\”?
-
是的,这正是我会尝试的。
标签: powershell active-directory acl powershell-5.0