2012-05-03 26 views
3

我正在编写一个脚本,它应该为文件夹及其所有内容设置文件系统访问权限。Powershell:在FileSystemAccessRule中结合ContainerInherit和ObjectInherit?

要影响所有内容,包括子文件夹和文件,应根据.NET文档将ContainerInherit和ObjectInherit结合起来。但我无法得到这个工作,我不确定语法。

示例代码:

$ar = new-object System.Security.AccessControl.FileSystemAccessRule(New-Object System.Security.Principal.NTAccount($user),FullControl,ContainerInherit,InheritOnly,Allow) 

这会工作,所以会使用ObjectInherit而已,但我怎样才能将它们组合起来?使用引号和逗号,像这样"ContainerInherit,ObjectInherit"将不起作用,因为它显然不允许混合字符串和非字符串参数。

我也试过使用-and运算符,但这只是给了我一个错误。将枚举赋值给变量($inherit = ContainerInherit,ObjectInherit)也不起作用。

那么,有关如何做到这一点的任何提示?

回答

7

可以使用-bor(合并它们类似于|在其他语言中)。如其他答案中所示,使用逗号从字符串解析它也可以。

我也更正了你的示例语法,这个示例应该可以工作。

$if=[Security.AccessControl.InheritanceFlags] 
$fsr=[Security.AccessControl.FileSystemRights] 
$pf=[Security.AccessControl.PropagationFlags] 
$flags = [Security.AccessControl.InheritanceFlags]($if::ContainerInherit -bor $if::ObjectInherit) 

$ar = new-object Security.AccessControl.FileSystemAccessRule ((New-Object System.Security.Principal.NTAccount($user)),$fsr::FullControl, $flags, $pf::InheritOnly, "Allow") 

但即使是简单的是仅使用字符串:

new-object Security.AccessControl.FileSystemAccessRule ($user, "FullControl", "ContainerInherit,ObjectInherit", "InheritOnly", "Allow") 
+0

谢谢,我以为我已经尝试过使用字符串,但是出现了一些其他问题,我的代码让我觉得字符串只是不起作用。 –

2

我不是在电脑前,但试试这个:

[System.Security.AccessControl.InheritanceFlags] 'ContainerInherit,ObjectInherit'

+0

+验证。非常酷,PowerShell可以投出字符串! –