Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在散列中获取Powershell ACL信息以很好地打印到文件中_Powershell_Hash_Hashtable - Fatal编程技术网

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

如何在散列中获取Powershell ACL信息以很好地打印到文件中,powershell,hash,hashtable,Powershell,Hash,Hashtable,我正在开发一个脚本来比较两个不同文件位置之间的访问权限。我的问题与此类似,因为我正在将信息记录到哈希表中,但它不是以可读的方式打印到文件中 这是我的密码 Get-ChildItem -path $location1 -recurse | ForEach{$original_file_permissions[$_.name] = (Get-ACL $_.fullname).Access; Write-Host Parsing $_.fullname} $original_file_permissi

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

这是我的密码

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方法捕获该文件或文件夹的访问详细信息,并将其作为值存储在哈希表中,其中键是文件或文件夹标题。但是,散列打印到的文件并没有打印确切的细节,我相信它会打印一个系统类?我不太确定

下面是该文件的外观

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

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

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

$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或打印格式表输出到文本文件中。每个访问规则都是一行。

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

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

$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或打印格式表输出到文本文件中。每个访问规则都是一行。

您试图将一个对象输出到一个平面文件(txt文件),因此acl对象将作为类型写入。换句话说,out文件不知道如何写入acl对象,所以我们需要将对象转换为字符串,然后输出文件

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

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


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

您试图将对象输出到平面文件(txt文件),因此acl对象被作为类型写入。换句话说,out文件不知道如何写入acl对象,所以我们需要将对象转换为字符串,然后输出文件

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

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


另外请注意,我删除了
格式表
——这是因为格式表更改了对象类型,通常仅用于在控制台中显示内容。

您需要访问您试图打印的成员,否则它会将对象类型打印到文件中。好的,它正在打印对象类型。.GetEnumerator()是否不访问哈希表的每个成员?不,基本上与键入
$hash
相同,只是您不能通过
访问成员。Key
您需要访问您试图打印的成员,否则它会将对象类型打印到文件中。好的,它正在打印对象类型。.GetEnumerator()是否不访问哈希表的每个成员?不,基本上与键入
$hash
相同,只是您不能通过
.GetEnumerator()访问成员。Key
非常感谢。另一个答案对我不起作用,因为我在声明中保留了格式表,认为这是必要的。奇怪的是,它通常只在控制台中使用,我在很多其他答案中都看到了。欢迎您…out file似乎理解来自format table的对象,这意味着我们可以改进输出:)…请查看编辑…但是是的format table outputs
format
对象与其他cmdlet不兼容…但是out file似乎可以工作非常感谢您。另一个答案对我不起作用,因为我在声明中保留了格式表,认为这是必要的。奇怪的是,它通常只在控制台中使用,我在很多其他答案中都看到了。欢迎使用…out file似乎理解来自format table的对象,这意味着我们可以改进输出:)…查看编辑…但是是的format table outputs
format
对象与其他cmdlet不兼容…但是out file似乎可以工作