PowerShell GetAccessControl returns 不一致的结果

PowerShell GetAccessControl returns inconsistent results

我知道标题可能有点误导,但如果不输入 10 行文本就不可能写得更好,所以我开始了:

我正在尝试列出我的硬盘驱动器的权限,我发现以下方法非常有效:

$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,我会做的是首先更改为我硬盘上的临时文件夹,然后更改为软盘驱动器,然后将文件复制到 c: 驱动器.它看起来像这样:

C:\> cd temp
C:\temp> cd a:
A:\> copy *.* c:

这会将所有文件从 a: 驱动器复制到我在 c: 驱动器上的最后一个文件夹。

此处同样适用,因为当您使用 c: 作为参考时,它正在查看(很可能)您正在执行命令的当前文件夹。

如果您不立即展开 'Access' 属性,这将变得更加明显,因为基础对象会告诉您您正在查看权限的文件夹的名称对于.