【发布时间】:2015-12-05 22:35:52
【问题描述】:
我有一个非常大的文件夹列表,我需要从每个文件夹中删除一个 ACL。我没有手动完成,而是尝试编写一个脚本来在很短的时间内完成它,但我遇到了一些麻烦。
这是我目前所拥有的:
$filepath = "C:\ALCTEST"
$user = "domain\username"
$folders = @((get-item $filePath))
$folders += Get-ChildItem $filePath -Recurse |
where { $_.PSIsContainer -ne $false }
##Need to do this in order to remove item 0 from the array, otherwise
##item 0 is the parent folder
$newfolders = $folders[1,2 + 3..($folders.length - 1)]
foreach ($folder in $newfolders) {
$acl = Get-Acl -Path $folder.FullName
foreach ($access in $acl.access) {
foreach ($value in $access.IdentityReference.Value) {
if ($value -eq $user) {
$acl.RemoveAccessRule($access) | Out-Null
}
}
}
Set-Acl -Path $folder -AclObject $acl
}
根据我在调试期间看到的情况,我认为一切正常,直到我尝试将 ACL 设置回文件夹。当它到达那条线时,我得到了错误
找不到路径“C:\Windows\system32\01”,因为它不存在。
在 ACLTEST 的父文件夹内有七个文件夹,名为“01”...“07”,带有用于测试的各种 ACL。
我不知道从这里去哪里。我正在阅读this Scripting Guy article,这对我有很大帮助,但他的脚本似乎专注于手动输入文件夹路径,对于需要更改的数百个文件夹,我绝对不能这样做。
感谢任何帮助。我是 PowerShell 脚本的新手,所以如果您发现除了我在上面脚本中提到的错误之外的任何错误,我很想听听。
【问题讨论】:
标签: powershell powershell-2.0 acl powershell-3.0