英文:
Powershell: How to get and export NTFS Permissions
问题
我目前一直在使用以下脚本来获取NTFS权限。问题在于,当涉及到较大的共享时,它会占用大量内存。
问题出在于较大的共享,这会占用大量内存。我尝试过其他方法,比如将ACL附加到CSV文件,但通常遇到相同的问题。这是因为 $NTFS += foreach ($Item in $Tree)
,我认为它只是将内容添加到变量中,直到递归完成才会导出。
英文:
I currently have been using the following script to get NTFS Permissions. The issue is that when it comes to larger shares, it is very RAM intensive.
# 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
The issue is regarding larger shares, this is very RAM intensive. I have tried other methods such as appending ACL's to a CSV but I usually run into the same issue. This is because of $NTFS += foreach ($Item in $Tree)
as I believe it just adds to the variable, and will not export until it has recursively finished.
答案1
得分: 1
使用管道操作,避免使用临时变量以保持内存占用低。这样做的额外好处是,代码变得更简洁。
# 根共享或根路径
$RootShare = '<\\Share>'
# 启动一个脚本块,将命令分组并通过管道传递给Export-Csv
& {
# 收集RootShare的NTFS权限。
# -> 成为脚本块的隐式输出
Get-NTFSAccess -Path $RootShare
# 收集文件和目录。通过将Get-ChildItem传递给Get-NTFSAccess,我们避免了另一个临时变量。
# -> 成为脚本块的隐式输出
Get-ChildItem -Path $RootShare -Recurse |
Get-NTFSAccess #-ExcludeInherited #或 -ExcludeExplicit
} | Export-Csv -Path 'C:\Temp\File.csv' -NoTypeInformation
# 在这里,我们将脚本块的输出通过管道传递给Export-Csv
英文:
Use the pipeline consequently and avoid temporary variables to keep memory footprint low. As an added benefit, the code becomes more concise.
# 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论