2014-09-27 84 views
0

我正在使用PowerShell 4并创建了一个脚本,该脚本将文本文件中列出的共享并将ACL信息输出到CSV文件。问题是,我无法弄清楚如何导出包括文件夹路径。这里是我的脚本:将Get-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 
+0

看起来只是一个错字。您将ACL存储在'$ Permissions'中,但将成员添加到'$ Permission'。 – 2014-09-27 11:39:43

+0

在创建问题时,这两个错误类型。现在修复。 – AKAJim 2014-09-27 14:20:29

+0

您能否提供可与此解决方案配合使用的输入文件的格式? – Damainman 2015-07-06 14:37:54

回答

0

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

# 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 } 
+0

这工作,有一个警告。我使用一个具有2个共享的文件测试它,它的作用是在CSV的顶部导出第二个共享ACL信息,不包含路径,然后将共享1和共享2的信息与路径导出。我过滤了空白行,现在它工作正常。但是,在添加更多筛选器时会阻塞,以便仅导出未继承的域帐户和权限。有任何想法吗?这里是我的过滤器管道:其中{$ _。path -ne $ null - 和$ _。IdentityReference -contains“HARRAHS” - 和$ _。IsInherited -eq“FALSE”} – AKAJim 2014-09-27 21:58:30

+0

找出来了。我改变了,像“HARRAHS *”和-ne“真”,它的工作。再次感谢! – AKAJim 2014-09-28 01:17:16

+0

@AKAJim你知道你为什么需要身份证明吗? – 2015-01-13 17:50:19

0

这会给你一个很好的格式化的CSV

$项目=(GET-ChildItem “d:\” -Recurse |凡{$ .PSIsContainer} |选择全称|%{$。 $ 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