读取哈希时数据无效 table

Invalid data when reading hash table

以下代码应通过散列 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"
}

结果给我屏幕输出

Matching regex for desa is System.Collections.Hashtable[desa]
Matching regex for unpd is System.Collections.Hashtable[unpd]
Matching regex for dpad is System.Collections.Hashtable[dpad]
Matching regex for dsd is System.Collections.Hashtable[dsd]

并且没有筛选目标文件。

我哪里做错了...?

经过一些实验,我发现了为什么我无法输出该散列的内容 table。问题出在我从那个散列 table 中调用对象的方式。以下失败:

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

当我把它改成:

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

成功了。因为它在引号中,所以它实际上并没有正确解析散列 table。