文章摘要
这篇文章介绍了如何在PowerShell中管理文件系统的访问权限。主要步骤包括: 1. 创建文件夹并获取其当前访问控制列表(ACL)。 2. 添加权限给当前用户和所有管理员,包括读写权限以及是否继承文件系统的权限。 3. 设置不继承访问权限,以确保修改后的文件权限不会自动应用到子目录中。 文章重点描述了如何通过PowerShell脚本实现对文件访问权限的精细控制。
# create folder
$Path=’c:\PermissionNoInheritance’
$null=New-Item -Path $Path -ItemType Directory -ErrorAction SilentlyContinue
# 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。



