我有一个看起来像这样的变量:
Plugin ID : 66334 Host : GCAB-L7-449090L Plugin Output : . Microsoft Operating System Patches : + To patch the remote system, you need to install the following Microsoft patches : - KB3167679 (MS16-101) (2 vulnerabilities)The following CVEs would be covered: CVE-2016-3300, CVE-2016-3237 - KB3114340 (MS16-099) (133 vulnerabilities)The following CVEs would be covered: CVE-2016-3313, CVE-2016-3315, CVE-2016-3316, CVE-2016-3317, CVE-2016-3318, - KB3115427 (MS16-099) (133 vulnerabilities)The following CVEs would be covered: CVE-2016-3313, CVE-2016-3315, CVE-2016-3316, CVE-2016-3317, CVE-2016-3318 Plugin ID : 66334 Host : GCAB-L7-449096R Plugin Output : . Microsoft Operating System Patches : + To patch the remote system, you need to install the following Microsoft patches : - KB3167679 (MS16-101) (2 vulnerabilities)The following CVEs would be covered: CVE-2016-3300, CVE-2016-3237 - KB3177725 (MS16-099) (58 vulnerabilities)The following CVEs would be covered: CVE-2016-3313, CVE-2016-3315, CVE-2016-3316, CVE-2016-3317, CVE-2016-3318
我想要完成的是包含主机的KB数组。我认为散列表是要走的路,但是如果它是我错过了关于它们的关键部分。这是我的代码:
$filtered = $data | Where-Object {[string]$_."Plugin ID" -eq "66334"}
foreach ($item in $filtered)
{
$machine = $item.Host
$kbs = Select-String -InputObject $item.'Plugin Output' -Pattern $regex -AllMatches |
ForEach-Object { $_.Matches }
foreach ($k in $kbs)
{
if ($hash.ContainsKey($k))
{
#The KB is already a part of the hash table. Edit the key value to include the new host.
}
else
{
$hash[$k] = $machine
}
}
}
如果密钥不存在,则将其添加到散列中,否则我将修改现有密钥的值以包含新主机。不幸的是,我的if
语句继续只执行else子句。
我想要的是:
KB Host KB3167679 GCAB-L7-449090L, GCAB-L7-449096R KB3114340 GCAB-L7-449090L KB3115427 GCAB-L7-449090L KB3177725 GCAB-L7-449096R
所以,有几个问题:
$hash.ContainsKey()
在这里为我工作?答案 0 :(得分:3)
是的,哈希表是要走的路。 $hash.ContainsKey()
不适合您,因为您为$kbs
列出了MatchInfo
个对象,而不是将匹配的值扩展为字符串。
正如其他人已经建议您可以在管道中添加另一个ForEach-Object
$kbs = Select-String -InputObject $item.'Plugin Output' -Pattern $regex -AllMatches |
ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }
或(如果您有PowerShell v3或更新版本)使用member enumeration
$kbs = Select-String -InputObject $item.'Plugin Output' -Pattern $regex -AllMatches |
ForEach-Object { $_.Matches.Value }
获取实际的字符串值。