Powershell目录文件夹管理权限的继承和指定方法(windowspowershell在哪个目录下)这样也行?

随心笔谈2年前发布 admin
189 0 0

文章摘要

这篇文章详细介绍了如何在Windows系统中使用PowerShell脚本来设置特定文件夹的属性,以确保其仅由当前用户或管理员访问。其主要内容包括: 1. 创建文件夹`c:PermissionNoInheritance`,并使用`New-Item`命令指定属性不继承父目录权限。 2. 获取文件夹的现有访问控制列表(ACL)。 3. 添加新的读写权限,允许当前用户和所有用户访问文件夹。 4. 禁用文件夹对子目录和文件的继承权限。 5. 最后重新设置ACL,确保更新后的权限设置生效。 文章的核心内容在于通过PowerShell命令实现细粒度的文件权限管理,以防止权限向子目录或文件传播。


# create folder
$Path=’c:\PermissionNoInheritance’
$null=New-Item -Path $Path -ItemType Directory -ErrorAction SilentlyContinue

# get current permissions
$acl=Get-Acl -Path $path

# add a new permission for current user
$permission=$env:username, ‘Read,Modify’, ‘ContainerInherit, ObjectInherit’, ‘None’, ‘Allow’
$rule=New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
$acl.SetAccessRule($rule)

# add a new permission for Administrators
$permission=’Administrators’, ‘FullControl’, ‘ContainerInherit, ObjectInherit’, ‘None’, ‘Allow’
$rule=New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
$acl.SetAccessRule($rule)

# disable inheritance
$acl.SetAccessRuleProtection($true, $false)

# set new permissions
$acl | Set-Acl -Path $path

© 版权声明

相关文章