1

我正在编写一个脚本来从一个目录结构复制权限并将它们重新应用到另一个。我不能简单地使用icacls \\my\path\* /save file.acl /T,因为我需要在保存和恢复之间进行一些调整,使用 PowerShell 的(Get-ACL $path).Access输出更简单。

在这样做时,我试图将这个(即FileSystemRights)的输出映射到它们的等效icacls 权限;然后,我将用括号括起来以逗号分隔的权限列表,以通过 icacls 应用。

[string[]]$rights = $InputObject.FileSystemRights -split  '\s*,\s*' # note: FileSystemRights here is just a string rather than having been converted to ENUM, as I've just pulled it straight from CSV
switch ($rights) {
    'AppendData'                   {'AD'}
    #'ChangePermissions'           {'?'}
    #'CreateDirectories'           {'?'}
    'CreateFiles'                  {'WD'} # duplicate of WriteData
    'Delete'                       {'DE'} # or D?
    'DeleteSubdirectoriesAndFiles' {'DC'}
    'ExecuteFile'                  {'X'}
    'FullControl'                  {'F'}
    'ListDirectory'                {'RD'} #duplicate of read data
    'Modify'                       {'M'}
    'Read'                         {'R'} # or GR?
    'ReadAndExecute'               {'RX'}
    'ReadAttributes'               {'RA'}
    'ReadData'                     {'RD'}
    'ReadExtendedAttributes'       {'REA'}
    'ReadPermissions'              {'RC'}
    'Synchronize'                  {'S'}
    'TakeOwnership'                {'WO'}
    #'Traverse'                    {'?'} # or does this mean to specify the /T option?  Not really a permission
    'Write'                        {'W'} # or GW?
    'WriteAttributes'              {'WA'}
    'WriteData'                    {'WD'}
    'WriteExtendedAttributes'      {'WEA'}
    Default {Write-Warning "Could not find icacls permission for file system right: '$_'"} # Note: This may also occur if we get a numeric value for the rights instead of a comma separated list of enum names.  Once I've got the mapping I'll code a fix to handle that scenario too.
}

问题

我不确定上面的映射;在某些情况下,我无法计算出等效值是多少(例如ChangePermissions, CreateDirectories, Traverse),而在其他情况下有多种可能性(例如应该Read/Write映射到R/WGR/ GW;或者没有区别)?我检查了文档并搜索了网络,但对这些权限的大多数解释都没有比给出这些缩写的名称更详细。

附加上下文

当我说我需要进行一些调整时,我正在从旧域上的旧文件共享中导出权限,并将它们导入到新域中,因此我需要映射旧域用户的一些帐户到新域中使用的 SID(我们有一个 SIDHistory,但目标是干净利落地做事,而不是长期依赖这个)。新域中还有一些组与旧域中的组等效,但没有关系(即它们不是从旧域迁移的;尽管它们的功能基本相同。编辑输出ICACLS 保存的信息很复杂,因为结构非常小。这不是不可能的,但比我觉得舒服得多。

我需要将内容从 PS/.Net 输出映射回 icacls 格式的原因是因为目标共享位于 Azure 文件上(启用了 AADDS);所以Set-ACL不起作用,虽然icacls

我已经为继承部分创建了类似的逻辑:

[string[]]$if = $InputObject.InheritanceFlags -split '\s*,\s*'
[string[]]$pf = $InputObject.PropagationFlags -split '\s*,\s*'
switch ($if) {
    'ContainerInherit' {'(CI)'}
    'ObjectInherit' {'(OI)'}
}
switch ($pf) {
    'InheritOnly' {'(IO)'}
    'NoPropagateInherit' {'(NP)'}
}
if ($InputObject.IsInherited) {
    '(I)'
}

对于不熟悉 PowerShell 的 switch 语句的任何人;功能能够处理数组中的每个项目;因此不需要逻辑循环遍历$rights. 更多信息在这里

更新 2020-06-16 10:00

我刚刚意识到有一种简单的方法可以查看FileSystemRights包含重复项的位置。下面的代码标识了这些。这解决了我的问题Traverseand CreateDirectories

[Enum]::GetNames([System.Security.AccessControl.FileSystemRights]) | 
   sort | 
   %{[PSCustomObject]@{
        Name = $_ 
        FSR = ([System.Security.AccessControl.FileSystemRights]$_)
   }} | 
   ft -AutoSize

这表明我可以重用一些现有的值

Name                                                  FSR
----                                                  ---
AppendData                                     AppendData
ChangePermissions                       ChangePermissions
CreateDirectories                              AppendData <-- i.e. AD
CreateFiles                                   CreateFiles
Delete                                             Delete
DeleteSubdirectoriesAndFiles DeleteSubdirectoriesAndFiles
ExecuteFile                                   ExecuteFile
FullControl                                   FullControl
ListDirectory                                    ReadData <-- i.e. RD
Modify                                             Modify
Read                                                 Read
ReadAndExecute                             ReadAndExecute
ReadAttributes                             ReadAttributes
ReadData                                         ReadData
ReadExtendedAttributes             ReadExtendedAttributes
ReadPermissions                           ReadPermissions
Synchronize                                   Synchronize
TakeOwnership                               TakeOwnership
Traverse                                      ExecuteFile <-- i.e. X
Write                                               Write
WriteAttributes                           WriteAttributes
WriteData                                     CreateFiles <-- i.e. WD
WriteExtendedAttributes           WriteExtendedAttributes
4

1 回答 1

1

在研究如何转换我认为无效的数值时(即认为它们是由枚举值组成的;尽管由于某种原因在转换时没有解决[System.Security.AccessControl.FileSystemRights]268435456)我遇到了这篇文章,然后从中找到了这篇文章。

鉴于那里的信息,以及我在上一次更新中对重复值的发现,我现在将映射编写为:

[string[]]$rights = $InputObject.FileSystemRights -split  '\s*,\s*' # note: FileSystemRights here is just a string rather than having been converted to ENUM, as I've just pulled it straight from CSV
switch ($rights) {

    '-2147483648'                  {'GR'}
    '268435456'                    {'GA'}
    '536870912'                    {'GE'}
    '1073741824'                   {'GW'}
    'AppendData'                   {'AD'}
    'ChangePermissions'            {'WDAC'}
    'CreateDirectories'            {'AD'} # duplicate of AppendData
    'CreateFiles'                  {'WD'} # duplicate of WriteData
    'Delete'                       {'DE'} # or D?
    'DeleteSubdirectoriesAndFiles' {'DC'}
    'ExecuteFile'                  {'X'}
    'FullControl'                  {'F'}
    'ListDirectory'                {'RD'} # duplicate of read data
    'Modify'                       {'M'}
    'Read'                         {'R'}
    'ReadAndExecute'               {'RX'}
    'ReadAttributes'               {'RA'}
    'ReadData'                     {'RD'}
    'ReadExtendedAttributes'       {'REA'}
    'ReadPermissions'              {'RC'}
    'Synchronize'                  {'S'}
    'TakeOwnership'                {'WO'}
    'Traverse'                     {'X'} # duplicate of ExecuteFile
    'Write'                        {'W'}
    'WriteAttributes'              {'WA'}
    'WriteData'                    {'WD'}
    'WriteExtendedAttributes'      {'WEA'}
    Default {Write-Warning "Could not find icacls permission for file system right: '$_'"} # Note: This may also occur if we get a numeric value for the rights instead of a comma separated list of enum names.  Once I've got the mapping I'll code a fix to handle that scenario too.
}

为了将 ChangePermissions 解析为 WDAC,我刚刚使用Get-ACLSet-ACL仅将ChangePermissions访问权限分配给文件夹上的唯一用户,然后用于icacls查询该文件夹的权限。

于 2020-04-16T09:46:34.257 回答