【发布时间】: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