如何使用Powershell与FileSystemRights进行比较?

And*_*ker 4 windows filesystems powershell acl

我想检查给定的用户是否有权访问给定的文件夹-通过检查他们是否具有分配给他们的“修改”访问权限。

我认为针对该问题的PS为:

(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} |?{$_.filesystemrights.value -contains "Modify"} 
Run Code Online (Sandbox Code Playgroud)

但是最后的那部分没有用-我没有得到任何结果。但是我知道他们具有“修改”访问权限-如果我输入以下内容:

(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} | select -ExpandProperty filesystemrights
Run Code Online (Sandbox Code Playgroud)

然后我回来:

Modify, Synchronize
ReadAndExecute, Synchronize
Run Code Online (Sandbox Code Playgroud)

这是因为FileSystemRights属性是一个枚举吗?如果是这样,我该如何进行测试?

Bac*_*its 5

这是类型问题。 (Get-Acl .\myfolder).Access[].FileSystemRights是类型System.Security.AccessControl.FileSystemRights。它不是真正显示字符串。要使其成为字符串,只需使用ToString()方法:

(Get-Acl .\myfolder).Access | ?{$_.IdentityReference -eq "BUILTIN\Users"} |?{$_.filesystemrights.ToString() -contains "Modify"} 
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用按位比较方法。但是,当您要使用此功能时,很容易混淆:

($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Modify) -eq [System.Security.AccessControl.FileSystemRights]::Modify
Run Code Online (Sandbox Code Playgroud)

当您要使用此功能时:

($_.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::Modify) -eq $_.FileSystemRights
Run Code Online (Sandbox Code Playgroud)

它们具有不同的含义。例如,如果您具有“完全控制”,则前一个测试仍然适用。那是你要的吗?或者你想知道什么时候FileSystemRights字面上只是Modify

另外,[System.Security.AccessControl.FileSystemRights]是不完整的枚举。在我的环境中,我发现需要此表:

+-------------+------------------------------+------------------------------+
|    Value    |             Name             |            Alias             |
+-------------+------------------------------+------------------------------+
| -2147483648 | GENERIC_READ                 | GENERIC_READ                 |
|           1 | ReadData                     | ListDirectory                |
|           1 | ReadData                     | ReadData                     |
|           2 | CreateFiles                  | CreateFiles                  |
|           2 | CreateFiles                  | WriteData                    |
|           4 | AppendData                   | AppendData                   |
|           4 | AppendData                   | CreateDirectories            |
|           8 | ReadExtendedAttributes       | ReadExtendedAttributes       |
|          16 | WriteExtendedAttributes      | WriteExtendedAttributes      |
|          32 | ExecuteFile                  | ExecuteFile                  |
|          32 | ExecuteFile                  | Traverse                     |
|          64 | DeleteSubdirectoriesAndFiles | DeleteSubdirectoriesAndFiles |
|         128 | ReadAttributes               | ReadAttributes               |
|         256 | WriteAttributes              | WriteAttributes              |
|         278 | Write                        | Write                        |
|       65536 | Delete                       | Delete                       |
|      131072 | ReadPermissions              | ReadPermissions              |
|      131209 | Read                         | Read                         |
|      131241 | ReadAndExecute               | ReadAndExecute               |
|      197055 | Modify                       | Modify                       |
|      262144 | ChangePermissions            | ChangePermissions            |
|      524288 | TakeOwnership                | TakeOwnership                |
|     1048576 | Synchronize                  | Synchronize                  |
|     2032127 | FullControl                  | FullControl                  |
|   268435456 | GENERIC_ALL                  | GENERIC_ALL                  |
|   536870912 | GENERIC_EXECUTE              | GENERIC_EXECUTE              |
|  1073741824 | GENERIC_WRITE                | GENERIC_WRITE                |
+-------------+------------------------------+------------------------------+
Run Code Online (Sandbox Code Playgroud)

比较这些输出很有趣:

[System.Enum]::GetNames([System.Security.AccessControl.FileSystemRights]);
[System.Enum]::GetNames([System.Security.AccessControl.FileSystemRights]) | % { "$($_.ToString())`t`t$([System.Security.AccessControl.FileSystemRights]$_.ToString())`t`t$(([System.Security.AccessControl.FileSystemRights]$_).value__)";}
[System.Enum]::GetValues([System.Security.AccessControl.FileSystemRights]) | % { "$($_.ToString())`t`t$(($_).value__)";}
Run Code Online (Sandbox Code Playgroud)

GENERIC权限未在.Net类中枚举,但是如果枚举了足够的文件,您将看到该数值。

祝好运!