Powershell - variable number of items per row -> export-csv

Dan 1 Reputation point
2022-01-27T22:58:10.153+00:00

I have a bit of a project where I am scanning all assets for my organization for instances of the log4j vulnerability and am trying to organize all of the data into a csv before beginning to remediate all of the vulnerabilities. I have around 150 text files, some containing the desired string ("log4j-core-[version number].jar"), some not, and some with 10 instances of the string. The end goal is to have all the useful data in 1 csv organized into two columns ("asset tag" and "log4j version number" (and maybe a couple others like IP if its easy)). I read some other posts describing how to do this -> add them into arrays and then make a psobject containing all arrays and export that psobject to csv. The problem with that solution is that it will add 1 item from each array in 2 different columns for each number in the array. And the problem with that is I need to find a way to associate the two properly since they're not each 1-to-1; sometimes 1 asset can have 10 instances of log4j and I need them to correlate accurately, so I need column 1 to list 1 item and for column 2 to list a variable number of items depending on how many of them are associated with the file they were pulled from. Any help you can provide would be much appreciated.

Below is my code. It runs successfully and I get all of the desired items in each array. The text files have the asset tag in their name if you're wondering where I got that.

$files = Get-ChildItem -Path '\\[IP ADDRESS REMOVED]\Log4J\logs' -filter '*.txt'

$version = @()
$asset = @()

foreach ($f in $files)
{if ((get-content -Path $f.FullName) -like "*log4j-core*") 
{$asset += {$f.Name -split "_scanlog.txt"} ;$version += Get-Content -Path $f.fullname | Out-String -Stream | Select-String "log4j-core"}}
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,585 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points
    2022-01-28T03:58:47.937+00:00

    Is this what you're trying to do?

    Get-ChildItem -Path '\\[IP ADDRESS REMOVED]\Log4J\logs' -filter '*.txt' |
        ForEach-Object {
            if ((Get-Content -Path $_.FullName) -like "*log4j-core*") {
                [PSCustomObject]@{
                    Asset = ($_.Name -split "_scanlog.txt")[0]
                    Version = (Select-String -Path $_.fullname -Pattern "log4j-core") -join ";"
                }
            }
        } | Export-Csv <your-file-name-here> -NoTypeInformation
    
    0 comments No comments