【问题标题】:How to get Powershell ACL information in a hash to print nicely to a file如何在哈希中获取 Powershell ACL 信息以很好地打印到文件中
【发布时间】:2017-08-31 17:22:58
【问题描述】:

我正在开发一个脚本来比较两个不同文件位置之间的访问权限。我的问题类似于this question about System.Collections,因为我正在将信息记录到哈希表中,但它没有以可读的方式打印到文件中。

这是我的代码

Get-ChildItem -path $location1 -recurse | ForEach{$original_file_permissions[$_.name] = (Get-ACL $_.fullname).Access; Write-Host Parsing $_.fullname}
$original_file_permissions.getenumerator() | Format-Table | Out-File "U:\file_folder\sub_folder\original_file_permissions.txt

我的代码解析文件位置,使用 Get-ACL 方法捕获该文件或文件夹的访问详细信息,将其作为值存储在哈希表中,其中键是文件或文件夹标题。但是,哈希打印到的文件不会打印确切的细节,而是打印,我相信,一个 System 类?我不太确定。

这是文件的样子

sys.a90                        {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule}                                                                                                                                                
pickering-app_beta_02.hex      {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule}                                                                                                                                                
release_notes.txt              {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule}                                                                                                                                                
126037AA                       {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule} 

但是,当我在 PowerShell 终端中运行相同的命令并访问一个密钥时,我得到了我正在寻找的 ACL 详细信息。

PS C:\file_folder\sub_folder> $hash["126037AA"]


FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : user123
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : admin
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None                          
...

为什么会发生这种情况?以及如何使哈希中的 ACL 详细信息打印到文件中?

【问题讨论】:

  • 您需要访问您尝试打印的成员,否则它会将对象类型打印到文件中。
  • 好的,它正在打印对象类型。 .GetEnumerator() 不会访问哈希表的每个成员吗?
  • 不,和输入$Hash基本一样,只是不能访问.GetEnumerator().Key的成员

标签: powershell hash hashtable


【解决方案1】:

您正在尝试将对象输出到平面文件(txt 文件),因此 acl 对象被写入为一种类型。也就是说out-file不知道acl对象怎么写,所以我们需要把对象转换成字符串,然后输出文件。

$original_file_permissions.getenumerator() | 
  Foreach-Object { 
   '{0}{1}' -f $_.key, ($_.Value | out-string)   
  } | out-file c:\temp\acl.txt 

$original_file_permissions.getenumerator() | 
  Foreach-Object { 
    $_.Value | Add-Member Noteproperty Name $_.key -PassThru -Force      
  } | Format-Table | Out-File c:\temp\acl.txt

您可以将 acl 信息写为 csv,但您需要做更多的工作。

还注意到我删除了format-table - 这是因为格式表更改了对象类型,并且通常仅用于在控制台中显示内容。

【讨论】:

  • 非常感谢。另一个答案对我不起作用,因为我将 Format-Table 保留在语句中,认为这是必要的。奇怪的是它通常只在控制台中使用,我在很多其他答案中都看到过。
  • 欢迎...看来 out-file 理解来自格式表的对象,这意味着我们可以改进输出:)...查看编辑...但是格式表输出 format 与其他 cmdlet 不兼容的对象......但输出文件似乎工作
【解决方案2】:

System.Security.AccessControl.DirectorySecurity 有一个名为 AccesstostringScriptProperty。您可以使用它,但仍然存在一个问题,即您只有一个文件对象,但有一个或多个访问条目。这不适合您的文本输出。

我认为您必须深入到每个访问规则并添加文件名或您需要的任何信息。

$items = Get-ChildItem
foreach ($item in $items) {
    $acl = Get-Acl -Path $item.FullName  # Before edit this was $items.FullName
    foreach ($access in $acl.Access) {
        [PSCustomObject]@{
            Name              = $item.Name
            FullName          = $item.FullName
            FileSystemRights  = $access.FileSystemRights
            AccessControlType = $access.AccessControlType
            IdentityReference = $access.IdentityReference            
        }
    }
}

您可以轻松地将该对象作为 csv 或打印格式表输出到文本文件中。每个访问规则将是一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多