【问题标题】:Export Get-ACL to CSV with Path使用路径将 Get-ACL 导出为 CSV
【发布时间】:2014-11-22 07:42:19
【问题描述】:

我正在使用 PowerShell 4 并创建了一个脚本,该脚本获取文本文件中列出的共享并将 ACL 信息输出到 CSV 文件。问题是我不知道如何让导出也包含文件夹路径。这是我的脚本:

$InputFile = "C:\Folders.txt"
$OutputFile = "C:\FolderPermissions.csv"
$FolderList = Get-Content $InputFile

ForEach ($Folder in $FolderList)
{
    $Permissions = (Get-ACL $Folder).access
    $Report += $Permissions
}

$Report | Select-Object IdentityReference,FileSystemRights,IsInherited | Export-CSV $OutputFile -NoTypeInformation

我已经使用下面的行成功添加并导出了单个文件夹,但是一旦我组合对象,只有列名被导出,没有值。

$Folder = "\\server\share"
$Name=Folder
$Permissions = (Get-ACL $Folder).access
$Permissions | Add-Member -MemberType NoteProperty -Name $Name -Value $Folder

$Permissions | Select-Object Folder,IdentityReference,FileSystemRights,IsInherited | Export-CSV $OutputFile -NoTypeInformation

解决办法:

再次感谢各位! MFT 的回答通过一些调整解决了这个问题。这是我想要的过滤器的工作代码。请注意,.IsInherited 过滤器使用 -eq "TRUE" 返回了不正确的数据,但使用 -ne "FALSE" 成功。

$InputFile = "C:\Folders.txt"
$OutputFile = "C:\FolderPermissions.csv"
$FolderList = Get-Content $InputFile

ForEach ($Folder in $FolderList)
{
    $Permissions = (Get-ACL $Folder).access | ForEach-Object {$_ |
        Add-Member -MemberType NoteProperty -Name Folder -Value $Folder}
    $Report += $Permissions
}

$Report | Select-Object Folder,IdentityReference,FileSystemRights,IsInherited |
    Where {$_.Folder -ne $Null -and $_.IdentityReference -like "HARRAHS*" -and $_.IsInherited -ne "TRUE"} |
    Export-CSV $OutputFile -NoTypeInformation

【问题讨论】:

  • 看起来只是一个错字。您将 ACL 存储在 $Permissions 中,但将成员添加到 $Permission
  • 在问题创建过程中有 2 个错误输入。现已修复。
  • 您能否提供适用于此解决方案的输入文件格式?

标签: powershell export-to-csv


【解决方案1】:

你在正确的轨道上。为每个访问列表项添加路径属性将实现您的目标。

# Get access list items of the folder
$Permissions = (Get-Acl -Path $Folder).Access | 

    # Add the path property and assign its value, -PassThru so the object is assigned to $Permissions
    ForEach-Object { $_ | Add-Member -MemberType NoteProperty -Name Path -Value $Folder -PassThru }

【讨论】:

  • 这行得通,但有一点需要注意。我正在使用具有 2 个共享的文件对其进行测试,它所做的是将第二个共享 ACL 信息导出到 CSV 顶部而不使用路径,然后导出共享 1 和共享 2 的信息以及路径。我过滤掉了空白行,现在可以正常工作了。但是,当添加更多过滤器时它会阻塞,以便仅导出未继承的域帐户和权限。有任何想法吗?这是我的过滤器管道:其中 {$_.path -ne $null -and $_.IdentityReference -contains "HARRAHS" -and $_.IsInherited -eq "FALSE"}
  • 想通了。我改为 -like "HARRAHS*" 和 -ne "TRUE" 并且它起作用了。再次感谢!
  • @AKAJim 你知道你为什么需要身份参考吗?
【解决方案2】:

这将为您提供格式良好的 CSV

$Items = (Get-ChildItem "D:\" -Recurse | Where { $_.PSIsContainer } | select fullname | %{$_.fullname.trim()})
$Path = "C:\temp\ACLs.csv"

$Table = @()
$Record = [ordered]@{
"Directory" = ""
"Owner" = ""
"FileSystemRights" = ""
"AccessControlType" = ""
"IdentityReference" = ""
"IsInherited" = ""
"InheritanceFlags" = ""
"PropogationFlags" = ""

}

Foreach ($Item in $Items)
{

$ACL = (Get-Acl -Path $Item)

$Record."Directory" = $ACL.path | %{$_.trimstart("Microsoft.PowerShell.Core\FileSystem::")}
$Record."Owner" = $ACL.Owner

Foreach ($SItem in $ACL.access)
{
$Record."FileSystemRights" = $SItem.FileSystemRights
$Record."AccessControlType" = $SItem.AccessControlType
$Record."IdentityReference" = $SItem.IdentityReference
$Record."IsInherited" = $SItem.IsInherited
$Record."InheritanceFlags" = $SItem.InheritanceFlags
$Record."PropogationFlags" = $SItem.PropagationFlags


$objRecord = New-Object PSObject -property $Record
$Table += $objrecord
}
}
$Table | Export-Csv -Path $Path -NoTypeInformation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多