ria*_*hc3 2 powershell inheritance
我试图在Powershell中创建一个新文件夹,但我不希望它继承任何NTFS安全权限并手动添加2个用户:创建者和我自己的管理员帐户.
我有这个:
$FolderPath = "\\srv\path"
New-Item -ItemType directory -Path $FolderPath
$acl = Get-Acl "\\srv\path"
$acl.SetAccessRuleProtection($True, $False)
$acl.Access | %{$acl.RemoveAccessRule($_)} # I remove all security
$acl.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME) # I set the current user as owner
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule('myadminaccount','FullControl','Allow') # I set my admin account as also having access
$acl.AddAccessRule($rule)
Set-Acl $FolderPath $acl | Out-Null
Run Code Online (Sandbox Code Playgroud)
它不起作用,它继承父级的安全权限.
我将其更改为下面的注释,但它不允许用户为HE创建的文件夹设置ACL.在根文件夹(路径)上,他有更改权限权限...
这是收到的访问错误
他应该使用上面的代码创建的文件夹有什么权限?所有者应该能够自由地修改它.
为用户添加了完全控制共享权限,现在我知道该进程没有"SeSecurityPrivilege".当我添加$ acl.SetAccessRuleProtection($ True,$ False)行时会发生这种情况
我怎样才能让它发挥作用?
使用该SetAccessRuleProtection()方法从继承规则中排除ACL:
$acl.SetAccessRuleProtection($true,$false)
Run Code Online (Sandbox Code Playgroud)
第二个参数(preserveInheritance)在设置时也会删除现有的继承规则false,只留下系统默认的ACE.
如果在应用继承保护时遇到问题,请确保在设置访问规则保护之前使用所有权信息更新ACL:
$acl = Get-Acl "\\srv\path"
# SetOwner
$acl.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME)
# Write updated ownership info back
Set-Acl $FolderPath $acl | Out-Null
# SetAccessRuleProtection
$acl.SetAccessRuleProtection($True, $False)
# Write updated ACL back
Set-Acl $FolderPath $acl | Out-Null
Run Code Online (Sandbox Code Playgroud)
关于“SeSecurityPrivilege”的错误是文件系统对象的 Set-Acl错误。我建议不要对文件和文件夹使用 Set-Acl,而是使用可用于文件和文件夹对象的 SetAccessControl() 方法。您可以将代码修改为如下所示:
$FolderPath = "\\srv\path"
New-Item -ItemType directory -Path $FolderPath
$acl = Get-Acl $FolderPath
$acl.SetAccessRuleProtection($True, $False)
$acl.Access | % { $acl.RemoveAccessRule($_) } # I remove all security
# Not needed:
# $acl.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME) # I set the current user as owner
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule('myadminaccount', 'FullControl', 'Allow') # I set my admin account as also having access
$acl.AddAccessRule($rule)
(Get-Item $FolderPath).SetAccessControl($acl)
Run Code Online (Sandbox Code Playgroud)
还有一件事:如果要应用于文件夹,您可能希望将 $rule 更改为以下内容:
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
'myadminaccount',
'FullControl',
'ObjectInherit, ContainerInherit',
'None',
'Allow'
)
Run Code Online (Sandbox Code Playgroud)