如何在哈希中获取PowerShell ACL信息,以便将其很好地打印到文件中



我正在开发一个脚本,以比较两个不同文件位置之间的访问权限。我的问题类似于有关系统的问题。收集,因为我将信息记录到哈希表,但它不是以可读的方式打印到文件的。

这是我的代码

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_foldersub_folderoriginal_file_permissions.txt

我的代码使用get-acl方法来解析文件位置,以捕获该文件或文件夹的访问详细信息,将其存储在哈希表中,其中键是文件或文件夹标题。但是,哈希打印要以不打印确切详细信息的文件,我相信它是系统类的打印吗?我不确定。

这是文件的外观

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_foldersub_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详细信息是文件的打印?

您正在尝试将对象输出到flatfile(TXT文件),以便将ACL对象写为类型。换句话说,外文件不知道如何编写ACL对象,因此我们需要将对象转换为字符串然后输出文件。

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

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

您可以将ACL信息写为CSV,但您需要进行更多工作。

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

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

我认为您将必须遵循每个访问规则,并添加文件名或所需的信息。

$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或打印格式表。每个访问规则将是一行。

相关内容

最新更新