如何在散列中获取 Powershell ACL 信息以很好地打印到文件
How to get Powershell ACL information in a hash to print nicely to a file
我正在开发一个脚本来比较两个不同文件位置之间的访问权限。我的问题与 this question about System.Collections 类似,因为我将信息记录到散列 table,但它没有以可读的方式打印到文件。
这是我的代码
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 方法捕获该文件或文件夹的访问详细信息,将其作为值存储在散列 table 中,其中键是文件或文件夹标题。但是,哈希打印到的文件没有打印出确切的细节,而是打印出,我相信,一个系统 class?我不太确定。
这是文件的样子
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
有一个 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 或打印格式的文本文件-table。每个访问规则都是一行。
您正在尝试将对象输出到平面文件(txt 文件),因此 acl 对象被写为一种类型。也就是说out-file不知道acl对象怎么写,所以我们需要把对象转成字符串,然后输出file。
$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
- 这是因为格式-table 更改了对象类型并且通常仅用于在控制台中显示内容。
我正在开发一个脚本来比较两个不同文件位置之间的访问权限。我的问题与 this question about System.Collections 类似,因为我将信息记录到散列 table,但它没有以可读的方式打印到文件。
这是我的代码
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 方法捕获该文件或文件夹的访问详细信息,将其作为值存储在散列 table 中,其中键是文件或文件夹标题。但是,哈希打印到的文件没有打印出确切的细节,而是打印出,我相信,一个系统 class?我不太确定。
这是文件的样子
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
有一个 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 或打印格式的文本文件-table。每个访问规则都是一行。
您正在尝试将对象输出到平面文件(txt 文件),因此 acl 对象被写为一种类型。也就是说out-file不知道acl对象怎么写,所以我们需要把对象转成字符串,然后输出file。
$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
- 这是因为格式-table 更改了对象类型并且通常仅用于在控制台中显示内容。