在 Windows 2016 Server 中设置“拒绝”文件和文件夹删除权限

问题描述 投票:0回答:1

我试图拒绝或仅保留特定组中的用户删除共享文件夹中的文件和子文件夹的能力。每当我将高级权限设置为拒绝文件和子文件夹删除和/或将所有权限设置为活动时,除了删除文件和子文件夹的能力之外(我未选中这两个权限),我的用户将无法打开文件、对任何文件进行更改或以其他方式访问文件和子文件夹。我尝试过共享和ntfs权限设置的不同组合,我尝试过继承,删除继承,我尝试过一切,除了用锤子修复它。如果您有任何建议或需要更多信息来帮助理解我做错或没做的事情,请告诉我。

file-permissions user-permissions windows-server-2016
1个回答
0
投票

我不确定您是否正在寻找一个程序或自动化。无论如何,我将分享一个可能帮助您实现这一目标的脚本

  1. 获取当前访问控制设置
  2. 设置允许读取和执行权限的访问规则
  3. 设置拒绝删除权限的访问规则
  4. 将规则添加到访问控制列表
  5. 应用新的访问控制设置
using System;
using System.IO;
using System.Security.AccessControl;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\Path\To\Your\SharedFolder";
        string username = "YourUsername";

        DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);

        DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();

        FileSystemAccessRule allowRule = new FileSystemAccessRule(
            username,
            FileSystemRights.ReadAndExecute | FileSystemRights.ListDirectory | FileSystemRights.Read,
            InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
            PropagationFlags.None,
            AccessControlType.Allow);

        // Specify the access rule that denies delete permissions
        FileSystemAccessRule denyRule = new FileSystemAccessRule(
            username,
            FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles,
            InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
            PropagationFlags.None,
            AccessControlType.Deny);

        directorySecurity.AddAccessRule(allowRule);
        directorySecurity.AddAccessRule(denyRule);

        directoryInfo.SetAccessControl(directorySecurity);

        Console.WriteLine("Permissions defined successfully.");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.