Powershell: 如何获取并导出NTFS权限

huangapple go评论74阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月17日 22:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定