我有以下代码,应该通过 fileshare 中的文件夹,并将任何权限转换为读取权限。但是,有一个问题:它不会替换已经存在的权限,它只是添加到它们。其次,如果文件夹没有继承的权限,它会给出一个错误说
Set-Acl:进程不拥有此操作所需的“SeSecurityPrivilege”特权。
我已经检查了权限,我可以完全控制它们
function NotMigrated($SiteURL, $Folder) {
try {
$SiteString=[String]$SiteURL
$pos = $SiteString.LastIndexOf("/")
$Site = $SiteString.Substring($pos+1)
$parent=((get-item $Folder ).parent).Fullname
$AllFolders = Get-ChildItem -Recurse -Path $Folder |? {$_.psIsContainer -eq $True}
$FilesInRoot = Get-ChildItem -Path $Folder | ? {$_.psIsContainer -eq $False}
$acl= get-acl $Folder
foreach ($usr in $acl.access) {
$acl.RemoveAccessRule($usr)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($usr.IdentityReference,"Read","none","none","Allow")
$Acl.AddAccessRule($rule)
}
$acl | Set-Acl
} catch { continue }
#Loop through all folders (recursive) that exist within the folder supplied by the operator
foreach ($CurrentFolder in $AllFolders) {
#Set the FolderRelativePath by removing the path of the folder supplied by the operator from the fullname of the folder
$FolderRelativePath = ($CurrentFolder.FullName).Substring($Folder.Length)
$FileSource = $Folder + $FolderRelativePath
try {
$acl= get-acl $FileSource
foreach ($usr in $acl.access) {
$acl.RemoveAccessRule($usr)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($usr.IdentityReference,"Read","none","none","Allow")
$acl.AddAccessRule($rule)
}
$acl | Set-Acl
} catch { continue }
#For each file in the source folder being evaluated, call the UploadFile function to upload the file to the appropriate location
}
}
最大的问题不是你的代码,而是Set-Acl Cmdlet/FileSystem provider combination。当调用 Set-Acl 时,正在尝试写入整个安全描述符。如果你没有提升(或者如果你的管理员帐户没有被授予 SeRestorePrivilege),那是行不通的。但是,如果你被提升了,你有可能在你正在修改的文件 / 文件夹上destroying your SACL。
出于这个原因,我会不惜一切代价避免使用 Set-Acl,直到我链接到上面的错误被修复。相反,您可以使用 SetAccessControl()方法可用于文件和文件夹对象:
(Get-Item c:\path\to\folder).SetAccessControl()
一旦你这样做,你不应该再看到 SeSecurityPrivilege 错误。
您希望为文件夹中包含的所有 ACE 创建一个新的 ACE。我认为您要做的是查找未被继承的“允许”ACE。如果您有任何“拒绝”ACE,则最终将获得新的“允许”ACE 授予“读取”访问权限,我敢打赌您不想这样做。另外,如果您包含继承的 ACE,则最终将为每个继承的 ACE 删除一个新的显式 ACE,除非您可以将其删除...
您没有复制现有的继承和传播标志,也没有使用文件夹的默认值。
我认为这个修改版本的一段代码应该做你正在寻找的东西:
try {
$acl = get-acl $FileSource
# Only look for explicit Allow ACEs
foreach ($usr in ($acl.access | where { $_.IsInherited -eq $false -and $_.AccessControlType -eq 'Allow' })) {
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$usr.IdentityReference,
"Read",
$usr.InheritanceFlags,
$usr.PropagationFlags,
$usr.AccessControlType
)
# Calling SetAccessRule() is like calling Remove() then Add()
$acl.SetAccessRule($rule)
}
(Get-Item $FileSource).SetAccessControl($acl)
} catch { continue }
如果您首先获得文件的所有权,这可能会有所帮助,如果您尝试以下操作,它应该让您接受它,然后您可以更轻松地设置文件的权限。
$UsersToFix = (Get-Content C:\users\john\Desktop\fix.txt)
Function Fix-Rights(){
Param($Folder = "x")
If ($Folder -gt ""){
$MovedFolder = "C:\data\profiles\Student\$Folder"
Write-Host "Starting to repair rights for $Folder" -ForegroundColor GREEN -BackgroundColor BLACK
&takeown /F $MovedFolder /A /R /D Y
&icacls $MovedFolder /reset /T /C
&icacls $MovedFolder /setowner wc1\$Folder /T /C
&icacls $MovedFolder /grant wc1\$Folder':(OI)(CI)F'
&icacls $MovedFolder /inheritance:d
&icacls $MovedFolder /remove "creator owner"
Write-Host "Finished repairing rights for $Folder" -ForegroundColor GREEN -BackgroundColor BLACK
}
}
ForEach ($User in $UsersToFix){
Fix-Rights $User
}
我在Technet上找到了这个脚本
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(9条)