在Windows文件共享上设置权限

dav*_*ave 5 powershell

我有以下代码,应该通过文件共享中的文件夹,然后将所有权限转换为读取权限。但是,存在一个问题:它不会替换仅添加到他们那里的权限。其次,如果文件夹没有继承权限,则会显示错误信息

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
    }
}
Run Code Online (Sandbox Code Playgroud)

Roh*_*rds 5

最大的问题不是您的代码,而是Set-Acl Cmdlet / FileSystem提供程序组合。调用Set-Acl时,正在尝试写入整个安全描述符。如果您没有提升权限(或未授予您的管理员帐户SeRestorePrivilege的权限),那么它将无法正常工作。但是,如果您被提升,则有可能破坏正在修改的文件/文件夹上的SACL

因此,我将不惜一切代价避免使用Set-Acl,直到修复了我上面链接的错误为止。相反,您可以使用可用于文件和文件夹对象的SetAccessControl()方法:

(Get-Item c:\path\to\folder).SetAccessControl()
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您将不再看到SeSecurityPrivilege错误。但是,您仍然会遇到以下两个问题:

  1. 您正在为文件夹中包含的所有ACE创建新的ACE。我认为您想要做的是寻找未被继承的“允许” ACE。如果您有任何“拒绝” ACE,您将最终获得新的“允许” ACE来授予“读取”访问权限,我敢打赌您不想这样做。同样,如果您包含继承的ACE,则将为每个继承的ACE添加一个新的显式ACE,除非打破继承,否则您将无法删除继承的ACE。
  2. 您没有复制现有的继承和传播标志,也没有使用文件夹的默认设置。

我认为这段代码的修改后的版本应该可以满足您的需求:

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 }
Run Code Online (Sandbox Code Playgroud)