正文

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

文章摘要

这篇文章介绍了如何在PowerShell中管理文件系统的访问权限。主要步骤包括: 1. 创建文件夹并获取其当前访问控制列表(ACL)。 2. 添加权限给当前用户和所有管理员,包括读写权限以及是否继承文件系统的权限。 3. 设置不继承访问权限,以确保修改后的文件权限不会自动应用到子目录中。 文章重点描述了如何通过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