chris
chris

Reputation: 13

Filter csv file by array of hashtables

What I am trying todo is filter a csv file (happens to be a weblog) with an array of hashtables (user information from a database).

#$data is from a database. (about 500 items) Type: System.Data.DataTable
$users = @()
foreach($row in $data)
{
    $userItem = @{
        LoginId = $row[0]
        LastName = $row[3]
        FirstName = $row[4]
        LastAccess = $null
    }
    $users += $userItem
}
#Log files are about 14,000 lines long
$logfiles = Get-ChildItem $logFolder -Recurse | where {$_.Extension -eq ".log"} | Sort-Object BaseName -Descending
foreach($log in $logfiles)
{
    $csvLog = Import-Csv $log.FullName -Header ("Blank","LoginId","Date")
    $u = $users | Select {&_.LoginId}
    $filteredcsvLog = $cvsLog | Where-Object { $u -contains $_.LoginId}
    #This returns null
    ....

}

This does not seem to work, what am I missing. My guess is that I need to flatten the array into [string[]], however I can't seem todo that either.

Upvotes: 1

Views: 1227

Answers (1)

Keith Hill
Keith Hill

Reputation: 201682

Rather than do an array of hashtables, I would do a hashtable of custom objects e.g.:

$users = @{}
foreach($row in $data)
{
    $userItem = new-object psobject -property @{
        LoginId = $row[0]
        LastName = $row[3]
        FirstName = $row[4]
        LastAccess = $null
    }
    $users[$userItem.LoginId] = $userItem
}

Then the filtering is easier and faster:

foreach($log in $logfiles)
{
    $csvLog = Import-Csv $log.FullName -Header ("Blank","LoginId","Date")
    $filteredcsvLog = $cvsLog | Where-Object { $users[$_.LoginId} }
    ....
}

Upvotes: 1

Related Questions