我有一个很大的文件夹列表,我需要从每个文件夹中删除一个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。
我不确定从这里去哪里。我正在阅读这篇脚本专家的文章,该文章对我有很大帮助,但是他的脚本似乎专注于手动输入文件夹路径,对于需要更改的数百个文件夹,我绝对不能这样做。
任何帮助表示赞赏。我是PowerShell脚本的新手,因此,如果您看到除上面脚本中所指的错误以外的其他任何错误,我很乐意听到。
该-Path
参数Set-Acl
需要一个路径字符串,而不是一个DirectoryInfo
对象。传递后者时,只会Name
扩展其属性,因此Set-Acl
也在当前工作目录(显然是您的情况C:\Windows\system32
)中查找具有给定名称的对象。
更改
Set-Acl -Path $folder -AclObject $acl
进入
Set-Acl -Path $folder.FullName -AclObject $acl
问题就会消失。
话虽如此,您可能还需要进行其他一些修改。
$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)]
如果您不希望将父文件夹放入数组中,请先将其放在数组中。并且由于您显然拥有PowerShell v3,因此可以使用-Directory
参数代替where
过滤器。
$newfolders = Get-ChildItem $filePath -Recurse -Directory
foreach ($access in $acl.access) { foreach ($value in $access.IdentityReference.Value) { if ($value -eq $user) { $acl.RemoveAccessRule($access) | Out-Null } } }
每个访问规则只有一个标识引用,因此内部循环毫无意义。
foreach ($access in $acl.access) {
if ($access.IdentityReference.Value -eq $user) {
$acl.RemoveAccessRule($access) | Out-Null
}
}
您的代码可以简化为以下形式:
$filepath = 'C:\ALCTEST'
$user = 'domain\username'
$folders = Get-ChildItem $filePath -Recurse -Directory
foreach ($folder in $folders) {
$acl = Get-Acl -Path $folder.FullName
foreach ($access in $acl.Access) {
if ($access.IdentityReference.Value -eq $user) {
$acl.RemoveAccessRule($access) | Out-Null
}
}
Set-Acl -Path $folder.FullName -AclObject $acl
}
或(使用管道)如下:
$filepath = 'C:\ALCTEST'
$user = 'domain\username'
Get-ChildItem $filePath -Recurse -Directory | ForEach-Object {
$acl = Get-Acl -Path $_.FullName
$acl.Access | Where-Object {
$_.IdentityReference.Value -eq $user
} | ForEach-Object {
$acl.RemoveAccessRule($_) | Out-Null
}
Set-Acl -Path $_.FullName -AclObject $acl
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句