我正在开发一个脚本来比较两个不同文件位置之间的访问权限。我的问题类似于this question about System.Collections,因为我将信息记录到哈希表中,但它不是以可读的方式打印到文件中。
这是我的代码
config.assets.compile = true
我的代码解析文件位置,使用Get-ACL方法捕获该文件或文件夹的Access详细信息,并将其存储为哈希表中的值,其中键是文件或文件夹标题。但是,哈希打印到的文件不会打印确切的细节,而是打印,我认为是System类?我不太确定。
这是文件的样子
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
但是,当我在PowerShell终端中运行相同的命令并访问密钥时,我会获得我正在寻找的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}
为什么会发生这种情况?我怎样才能使哈希中的ACL详细信息打印到文件中?
答案 0 :(得分: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更改了对象类型,通常只用于在控制台中显示内容。
答案 1 :(得分:0)
System.Security.AccessControl.DirectorySecurity
有ScriptProperty
名为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或print format-table。每个访问规则都是一行。