【问题标题】:Correct filtering and formatting using powershell and cmdlet Get-ACL使用 powershell 和 cmdlet Get-ACL 正确过滤和格式化
【发布时间】:2015-12-18 13:15:24
【问题描述】:

我不是 powershell 专家,但我编写了一些较小的脚本。现在正在处理一个脚本,它返回一个文件夹的 ACL 及其子项。

当我简单地运行 get-acl 时,我会收到很多信息,我想过滤或跳过这些信息。 基本上我需要:

  • 完整路径,没有 Microsoft.Powershell.Core 的东西..
  • 访问(不需要 NT-Authori... 用户/组)

到目前为止,我尝试过这样的事情:

get-childitem -recurse "$myfolder" | get-acl | select -PSPATH, Group, Access

但是这样我就没有显示全名。

【问题讨论】:

    标签: powershell acl powershell-4.0


    【解决方案1】:

    你可以这样做:

    $acl = Get-Acl -Path $path
    $group = acl.Access | ForEach-Object {$_.identityReference.value} 
    $Access = acl.Access | ForEach-Object {$_.FileSystemRights}
    

    【讨论】:

    • 是的,这或多或少是我所做的。你还记得显示完整输出字符串的命令吗?不只是前 xx 个字符?我还在路径中显示了 microsoft.powershell 的东西。
    • 输出的完整字符串以及前几个字符是从哪里得到的?
    • 以路径为例。
    【解决方案2】:

    如果使用Format-Table输出数据,可以使用-AutoSize属性强制显示所有信息。

    关于您可以使用的完整路径:

    get-childitem -recurse "$myfolder" | select FullName
    

    或者更混乱的选项 IMO:

    $FullPath = $acl.path -replace ".*::"
    

    第二个选项使用字符串操作来删除所有文本,包括“::”

    编辑:-

    这是我用过的完整代码:

    $Output = @()
    $Path = "C:\SomePath"
    
    $ChildItems = Get-ChildItem -Recurse -Path $Path
    foreach($ChildItem in $ChildItems)
    {
        $FullPath = $ChildItem.FullName
        $acl = get-acl $FullPath
        $Access = $acl.Access
    
        foreach ($AccessObject in $Access)
        {
           $Group = $AccessObject.IdentityReference.value
           $Rights = $AccessObject.FileSystemRights
    
           $Line = New-Object PSObject
           $Line | Add-Member -membertype NoteProperty -name "Path" -Value $FullPath
           $Line | Add-Member -membertype NoteProperty -name "Group" -Value $Group
           $Line | Add-Member -membertype NoteProperty -name "Rights" -Value $Rights
           $Output += $Line
        }
    }
    $Output | Format-Table -AutoSize 
    

    【讨论】:

      【解决方案3】:

      试试这个代码。这就是我正在使用的。在 Path.txt 文件中提供您需要 ACL 的文件夹列表。

      $erroractionpreference = "Continue"
      
      $todaytime = Get-Date -format ddMMMyy_HH-mm
      
      $OutPath = ".\ACL_" + $todayTime + ".txt"
      
      $ACLS=get-content .\Path.txt
      
      $Out=foreach ($ACL in $ACLS) 
      { 
          write-host $ACL -foregroundcolor green
          get-acl $ACL  | select @{Label="Path";Expression={echo $ACL}},@{Label="Access";Expression={$_.AccesstoString}} | ft -wrap -a 
      }  
      
        echo $out >> $OutPath
      

      【讨论】:

        【解决方案4】:

        我的偏好(包括删除 Microsoft.PowerShell.Core\FileSystem:: ):

        Get-ChildItem . -recurse | get-acl | Select-Object -Property @{Name='Path';Expression={Convert-Path $_.Path}},Group,AccessToString
        

        我建议使用 Format-List 或 Out-Gridview:

        Get-ChildItem . -recurse | get-acl | Select-Object -Property @{Name='Path';Expression={Convert-Path $_.Path}},Group,AccessToString | fl
        Get-ChildItem . -recurse | get-acl | Select-Object -Property @{Name='Path';Expression={Convert-Path $_.Path}},Group,AccessToString | Out-GridView
        

        我也在搜索排除或仅显示 AccessToString 属性中的匹配行,但尚未找到我需要的内容。 好奇心:你需要“组”做什么?从我的角度来看,“所有者”可能是有先见之明的,所以如果你有“组”的用法,我很想知道。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-13
          • 1970-01-01
          相关资源
          最近更新 更多