PowerShell GetAccessControl返回不一致的结果
我知道标题可能有点误导,但如果不键入10行文字,就不可能写出更好的标题,因此我决定: 我试图列出硬盘的权限,我发现以下方法非常有效:PowerShell GetAccessControl返回不一致的结果,powershell,acl,Powershell,Acl,我知道标题可能有点误导,但如果不键入10行文字,就不可能写出更好的标题,因此我决定: 我试图列出硬盘的权限,我发现以下方法非常有效: $drives = get-wmiobject win32_volume | ? { ($_.DriveType -eq 3) -and ($_.DriveLetter -ne $null) }; ForEach($drive in $drives) {$drive.DriveLetter; ((Get-Item $drive.Name).GetAccessCon
$drives = get-wmiobject win32_volume | ? { ($_.DriveType -eq 3) -and ($_.DriveLetter -ne $null) }; ForEach($drive in $drives) {$drive.DriveLetter; ((Get-Item $drive.Name).GetAccessControl('Access')).Access}
在这里,我将解释脚本的每个部分的作用:
将所有硬盘驱动器条目存储到$drives中
$drives = get-wmiobject win32_volume | ? { ($_.DriveType -eq 3) -and
($_.DriveLetter -ne $null) }
循环浏览每个硬盘驱动器实例并打印硬盘驱动器名称
ForEach($drive in $drives) {$drive.DriveLetter;
通过传递$drive.Name作为驱动器名参数,打印给定给每个驱动器的ACL权限
((Get-Item $drive.Name).GetAccessControl('Access')).Access}
今天我注意到一件很时髦的事。。。如果执行前面提到的命令,我会得到一个包含6个不同ACL项的条目,如下所示:
PS C:\Users\lopezcha> $drives = get-wmiobject win32_volume | ? { ($_.DriveType -eq 3) -and ($_.DriveLetter -ne $null) };
ForEach($drive in $drives) {$drive.DriveLetter; ((Get-Item $drive.Name).GetAccessControl('Access')).Access}
C:
FileSystemRights : Modify, Synchronize
AccessControlType : Allow
IdentityReference : NT AUTHORITY\Authenticated Users
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : InheritOnly
FileSystemRights : AppendData, Synchronize
AccessControlType : Allow
IdentityReference : NT AUTHORITY\Authenticated Users
IsInherited : False
InheritanceFlags : None
PropagationFlags : None
FileSystemRights : ReadAndExecute, 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
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : ReadAndExecute, Synchronize
AccessControlType : Allow
IdentityReference : BUILTIN\Users
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
但是,如果我直接将驱动器名作为文本传递,而不是使用$drive.Letter,那么我只会得到3个ACL项,而不是6个
PS C:\Users\lopezcha> ((Get-Item 'C:').GetAccessControl('Access')).Access
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : AMERICAS\lopezcha
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
有人知道为什么会发生这种行为吗
编辑:我发现了一些有趣的东西。。。如果我使用“C:”作为驱动器名,我会得到3个ACL,但是如果我使用“C:\”我会得到6个ACL。这部分回答了我的问题,但我仍然想知道为什么返回的权限量不同
$drive.DriveLetter=C:
$drive.Name=C:\这可能比您预期的要简单。这是因为提供者。术语
c:
指的是提供程序在指定驱动器上使用的当前文件夹位置。而c:\
指的是该驱动器的根文件夹
让我换一种方式来提供更好的参考。当我开始使用电脑时,屏幕上只有绿色和黑色,我们经常使用软盘。现在,如果我想将一些文件从软盘复制到c:\temp,我要做的是首先更改硬盘上的temp文件夹,然后更改到软盘驱动器,然后将文件复制到c:驱动器。看起来是这样的:
C:\> cd temp
C:\temp> cd a:
A:\> copy *.* c:
这会将所有文件从a:驱动器复制到我在c:驱动器上的最后一个文件夹
同样的情况也适用于这里,当您将我们c:
作为您的引用时,它正在查看(很可能)您正在执行命令的当前文件夹
如果您不只是立即扩展“Access”属性,这将变得更加明显,因为基本对象会告诉您正在查看权限的文件夹的名称。您是100%正确的。我使用DOS和开发软件已经有好几年了,在此之前我从未听说C:实际上是对您正在执行命令的文件夹的引用。为了证明你的观点,我测试了从“C:\Users\lopezcha”获取运行命令的文件夹的权限,我获得了与使用“C:”完全相同的权限。谢谢你的课,我把你的回答标记为这个问题的答案。在2020年的PS版本5中,
getaccesscontrol()
不需要参数。