Powershell Get Acl的巧妙使用

Powershell Get Acl的巧妙使用,powershell,Powershell,使用Get-ACLcmdlet时,它会返回一个要处理的对象 Path Owner Access ---- ----- ------

使用
Get-ACL
cmdlet时,它会返回一个要处理的对象

Path  Owner               Access                                                                                            
----  -----               ------                                                                                            
Test2 Owner               NT AUTHORITY\Authenticated Users Allow  Modify, Synchronize                                       
                          NT AUTHORITY\SYSTEM Allow  FullControl  
使用类似于
(Get ACL D:\test2)的方法。Access
提供了更好的输出-每个权限一个项目:

FileSystemRights  : Modify, Synchronize
AccessControlType : Allow
IdentityReference : NT AUTHORITY\Authenticated Users
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None
由于脚本其余部分的工作方式,这是在执行
getacl
操作后需要呈现的内容。但是,我的脚本的其余部分嵌套在一个
中,每个
都脱离了这个
获取ACL
,当使用上面的示例时,您不能调用
$ACL.AddAccessRule
,因为这个调用似乎只在常规的
$ACL=Get ACL“Some Path”
下工作,而不是
$ACL=(Get ACL“Some Path”)。Access

我看到的错误是:

Method invocation failed because [System.Security.AccessControl.FileSystemAccessRule] does not contain a method named 'AddAccessRule'.

有没有一种方法可以解决这个问题,在示例2中我看到了这些信息,但仍然可以调用
.AddAccessRule
,而不必运行另一个
Get ACL
,这样做?

使用类似的方法:

$acl = Get-Acl "Some Path"
$access = $acl.Access

# Do stuff with $access
$acl.AddAccessRule