Powershell - 如何过滤输出

时间:2021-02-19 23:27:22

标签: powershell

我需要帮助过滤输出中的内容。 这是我的代码:

$FolderPath = Get-ChildItem -Directory -Path "Z:\D482\F11\SECURE" -Recurse -Force 
$Output = @()
ForEach ($Folder in $FolderPath) {
    $Acl = Get-Acl -Path $Folder.FullName
    ForEach ($Access in $Acl.Access) {
        $Properties = [ordered]@{'Folder Name'=$Folder.FullName;'Group/User'=$Access.IdentityReference  }
        $Output += New-Object -TypeName PSObject -Property $Properties            
    }
}
$Output  | ConvertTo-Csv | Out-File C:\Temp\Secured.txt

我的输出如下所示:

#TYPE System.Management.Automation.PSCustomObject
"Folder Name","Group/User"
"Z:\D482\F11\SECURE\QA\CDM\To_Load\Duals\Load_Completed","S-1-5-21-1275210071-879983540-1801674531-105509"
"Z:\D482\F11\SECURE\QA\Files\CDM\To_Load\Duals\Load_Completed","S-1-5-21-1275210071-879983540-1801674531-121910"
"Z\D482\F11\SECURE\QA\D482\To_Load\Duals\Load_Completed","DOMAIN\CXL3708"

在我的输出中,我只想要包含我们域名的行(如其中包含 DOMAIN 的行所示。

我没有成功 - 要么什么也没得到,要么在控制台中收到错误消息。

1 个答案:

答案 0 :(得分:2)

这是一种完成工作的方法...... [咧嘴笑]

它的作用...

  • 设置常量
  • Get-ChildItem 调用构建参数 splat
  • 从源路径中获取目录
  • 遍历目录列表
  • 获取当前目录的 acl 列表和以所需域名开头的过滤器
    请注意,我的 ACL 中没有 .IdentityReference 属性,因此我使用了 .Owner
  • 遍历这些 acl 项
  • 为当前的 acl 构建一个 [PSCustomObject]
  • 将其发送到 $AccessInfo 集合
  • 在屏幕上显示上述内容
  • 将集合保存到 csv 文件

代码...

$SourcePath = $env:TEMP
$DomainName = $env:USERDOMAIN
$ReportFile = "SO_Erich_Powershell - How To Filter Output.csv"
$FullReportFile = Join-Path -Path $env:TEMP -ChildPath $ReportFile

$GCI_Params = @{
    LiteralPath = $SourcePath
    Directory = $True
    Force = $True
    Recurse = $True
    ErrorAction = 'SilentlyContinue'
    }
$DirList = Get-ChildItem @GCI_Params

$AccessInfo = foreach ($DL_Item in $DirList)
    {
    $AclList = Get-Acl -LiteralPath $DL_Item.FullName |
        Where-Object {
            $_.Owner -match "^$DomainName"
            }
    foreach ($AL_Item in $AclList)
        {
        [PSCustomObject]@{
            DirName = $DL_Item.FullName
            # my single system has no ".IdentityReference" property
            #    so i used ".Owner"
            GroupOrUser = $AL_Item.Owner
            }
        }
    }

# display the data
$AccessInfo

# send to a csv file
$AccessInfo |
    Export-Csv -LiteralPath $FullReportFile -NoTypeInformation

截断的屏幕输出...

DirName                             GroupOrUser     
-------                             -----------     
C:\Temp\1                           MySysName\AnotherUserName
C:\Temp\2                           MySysName\AnotherUserName
C:\Temp\3                           MySysName\AnotherUserName

[*snip ...*]

C:\Temp\vscode-update-system-x64    MySysName\MyUserName   
C:\Temp\WPF                         MySysName\MyUserName   
C:\Temp\mbam\qt-jl-icons            MySysName\MyUserName

截断的 csv 文件内容...

"DirName","GroupOrUser"
"C:\Temp\1","MySysName\AnotherUserName"
"C:\Temp\2","MySysName\AnotherUserName"

[*snip ...*]

"C:\Temp\WPF","MySysName\MyUserName"
"C:\Temp\mbam\qt-jl-icons","MySysName\MyUserName"