我正在尝试的东西是使用哈希表数组(来自数据库的用户信息)过滤csv文件(恰好是一个博客)。
#$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
....
}
这似乎不起作用,我错过了什么。我的猜测是我需要将数组展平为[string []],但我似乎也不能这样做。
答案 0 :(得分:1)
我不是做一个哈希表数组,而是做一个自定义对象的哈希表,例如:
$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
}
然后过滤更容易,更快:
foreach($log in $logfiles)
{
$csvLog = Import-Csv $log.FullName -Header ("Blank","LoginId","Date")
$filteredcsvLog = $cvsLog | Where-Object { $users[$_.LoginId} }
....
}