本文介绍了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将不起作用,因为它似乎不允许混合字符串和非字符串参数。



我还尝试使用和< / code>运算符,但这只是给我一个错误。将枚举分配给变量( $ inherit = ContainerInherit,ObjectInherit )将不起作用。



所以,任何有关如何执行此操作的提示?

解决方案

可以使用-bor(类似于其他语言)来合并它们。



我还更正了您的示例语法,此示例应该可以正常工作。

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

$ ar =新对象安全性。 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)


I'm writing a script that should set filesystem access rights for a folder and all of it's contents.

To affect all content, both subfolders and files, one should combine ContainerInherit and ObjectInherit according to the .NET documentation. But I can't get that to work and I'm not sure about the syntax.

Sample code:

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

That'll work and so would using ObjectInherit only, but how can I combine them? Using quotation marks and a comma, like this "ContainerInherit,ObjectInherit" won't work, since it's appearantly not allowed to mix string and non-string argument.

I've also tried using the -and operator, but that just gives me an error. Assigning the enums to a variable ($inherit = ContainerInherit,ObjectInherit) won't work either.

So, any tips on how to do this?

解决方案

You can merge them using -bor (analogous to | in other languages). Parsing it from a string using a comma as shown in the other answer also works.

I also corrected your sample syntax and this sample should work.

$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")

But even simpler is to use strings only:

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

这篇关于Powershell:在FileSystemAccessRule中结合ContainerInherit和ObjectInherit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:18