PowerShell添加和重置NTFS权限


不管你是要给文件添加一条新的NTFS规则,还是关闭继承和增加新规则,你都可以使用下面脚本中演示的技巧,甚至把它当成一个配置模板。
脚本创建了一个测试文件,接着为当前用户定义了一个新的访问规则。新规则允许读写访问,添加到已存在的安全表述符上,此外它还关闭了继承。

# create a sample file to apply security rules to
$Path = "$env:temp\examplefile.txt"
$null = New-Item -Path $Path -ItemType File -ErrorAction SilentlyContinue

# use current user or replace with another user name
$username = "$env:USERDOMAIN\$env:USERNAME"

# define the new access rights
$colRights = [System.Security.AccessControl.FileSystemRights]'Read, Write' 
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 
$objType =[System.Security.AccessControl.AccessControlType]::Allow 
$objUser = New-Object System.Security.Principal.NTAccount($username) 

# create new access control entry
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
    ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) 

# get existing access control list for a file or folder
$objACL = Get-Acl -Path $Path 

# add rule
$objACL.AddAccessRule($objACE) 

# disable inheritance (if needed)
$objACL.SetAccessRuleProtection($true, $false)

# apply changed access control list to file
Set-Acl -Path $Path -AclObject $objACL

# show file in the File Explorer
explorer.exe "/SELECT,$Path"

一旦操作完毕,脚本还会在资源管理器中选择并打开它。你现在可以鼠标右键属性->安全来查看新的设置。

找出有效的访问权限,在ISE编辑器用下面这行:

[System.Security.AccessControl.FileSystemRights]::

它会打开上下文菜单,并自动列出所有有效设置。

原文地址:Adding and Resetting NTFS Permissions

本文链接: https://www.pstips.net/adding-and-resetting-ntfs-permissions.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注