我目前一直在使用以下脚本来获取 NTFS 权限。问题是,当涉及到较大的份额时,它会非常占用 RAM。
# Root Share or Root path
$RootShare = '<\\Share>'
# Gathering files and Directories
$Tree = Get-ChildItem -Path $RootShare -Recurse
# Gathering NTFS permissions for the RootShare
$NTFS = Get-NTFSAccess -Path $RootShare
# Adding Files and SubDirectories NTFS Permissions
$NTFS += foreach ($Item in $Tree)
{
#excluseInherited for a concise report
Get-NTFSAccess -Path $Item.FullName #-ExcludeInherited #or -ExcludeExplicit
}
# Export result to a file
$NTFS | Export-Csv -Path 'C:\Temp\File.csv' -NoTypeInformation
问题是关于更大的份额,这是非常 RAM 密集型的。我尝试过其他方法,例如将 ACL 附加到 CSV,但我通常会遇到同样的问题。这是因为
$NTFS += foreach ($Item in $Tree)
,因为我相信它只是添加到变量中,并且在递归完成之前不会导出。
因此使用管道并避免临时变量以保持低内存占用。作为一个额外的好处,代码变得更加简洁。
# Root Share or Root path
$RootShare = '<\\Share>'
# Start a scriptblock to group commands which are piped to Export-Csv
& {
# Gathering NTFS permissions for the RootShare.
# -> becomes implict output of the scriptblock
Get-NTFSAccess -Path $RootShare
# Gathering files and Directories. By piping Get-ChildItem
# to Get-NTFSAccess we save another temporary variable.
# -> becomes implict output of the scriptblock
Get-ChildItem -Path $RootShare -Recurse |
Get-NTFSAccess #-ExcludeInherited #or -ExcludeExplicit
} | Export-Csv -Path 'C:\Temp\File.csv' -NoTypeInformation
# Here we are piping the output of the scriptblock to Export-Csv