【问题标题】:Create a Script: Enumerate Access Control Lists创建脚本:枚举访问控制列表
【发布时间】:2020-10-21 19:08:12
【问题描述】:

我正在“尝试”使用 foreach 循环创建脚本,以便在该目录中显示 ACL。以下是说明:

在 foreach 条件之上,将变量 $directory 设置为当前目录的内容。

编写一个强文本命令来枚举文件的 ACL,使用 $item 变量代替文件名。

您需要使用以下 cmdlet:

  • Get-ChildItem(或Get-ChildItem 的任何别名,例如lsdir
  • Get-Acl

对于我的脚本,

foreach ($item in $directory) {
    
}

我完全不知道如何编写整个脚本。

【问题讨论】:

  • 停止尝试一次编写整个脚本 - 一次解决一个问题:1)弄清楚如何枚举当前目录的内容,2)弄清楚如何将结果存储在$directory,然后 3) 弄清楚如何对文件使用 Get-Acl

标签: powershell acl


【解决方案1】:

你有一个很好的开始,实际上很简单。下面的第一个命令是将命令Get-ChildItem "C:\" 的输出存储在$directory 变量中(C:\ 可以是您想要的任何目录)。

完成后,您只需遍历每个文件并运行Get-Acl 命令:

$directory = $(Get-ChildItem 'C:\')  # Store cmd output

# Iterate through each file entry
foreach ($item in $directory) {
    Get-Acl $item  # Get the ACL
}

【讨论】:

    【解决方案2】:

    即使我对 powershell 还是很陌生,但是,这是我为类似问题编写的代码。希望对您有所帮助:

      $BaseFolder = "<Path to Folder>"
    
    #Since Get-Acl can only report on one file or directory at a time. 
    $FolderPath = Get-ChildItem -Directory -Path $BaseFolder -Recurse -Force
    
    $Output = @()
    
    
    ForEach ($Folder in $FolderPath) {
    
      $Acl = Get-Acl -Path $Folder.FullName
    
      ForEach ($Access in $Acl.Access) {
    
    $Permissions = [ordered]@{'Name'=$Folder.FullName;'Users'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights}
    
    $Output += New-Object -TypeName PSObject -Property $Properties       
    
    }
    
    }
    
    
    $Output | Export-Csv "<path>\$((Get-Date).ToString("yyyy-MM-dd_HHmmss")).csv" -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 2011-07-09
      相关资源
      最近更新 更多