读取哈希表时数据无效

时间:2015-08-11 19:10:43

标签: hashtable

以下代码应该通过哈希表并使用键/值将一个大日志文件过滤为每个键的单独文件,使用正则表达式匹配的值:

<table>

结果为我提供了屏幕输出

# get today's date, or override with user-entered date
param($workingdate=(get-date).ToString("yyMMdd"))

# set hash table for each profile
$filters = @{
"desa" = "filtering string for desa";
"unpd" = "filtering string for unpd";
"dpad" = "filtering string for dpad";
"dsd" = "filtering string for dsd"
}
Foreach ($profilename in ("desa", "unpd", "dpad", "dsd")
)
{
#filter the main log and copy the filtered sub-set into individual profile logs
Write-Host "Matching regex for $profilename is $filters[profilelname]"
Get-Content "access-$workingdate.log" | Select-string -pattern $filters[$profilename] | Set-Content "$profilefilename-$workingdate.log"
}

并且没有过滤目标文件。

我做错了什么...... ??

1 个答案:

答案 0 :(得分:0)

经过一些实验,我发现了为什么我无法输出该哈希表的内容。问题在于我从该哈希表中调用了对象。以下失败:

Write-Host "Matching regex for $profilename is $filters[profilelname]"

当我将其更改为:

Write-Host "Matching regex for $profilename is" $filters.profilelname

有效。因为它是引号,所以它实际上并没有正确地解析哈希表。