如何在散列中获取Powershell ACL信息以便很好地打印到文件中

问题描述 投票:0回答:2

我正在开发一个脚本来比较两个不同文件位置之间的访问权限。我的问题类似于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方法捕获该文件或文件夹的Access详细信息,将其存储为哈希表中的值,其中键是文件或文件夹标题。但是,哈希打印到的文件不会打印确切的细节,而是打印,我认为是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详细信息打印到文件中?

powershell hash hashtable
2个回答
2
投票

您正在尝试将对象输出到flatfile(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更改了对象类型,通常只用于在控制台中显示内容。


0
投票

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

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

$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或print format-table。每个访问规则将是一行。

© www.soinside.com 2019 - 2024. All rights reserved.