哈希 table 是前进的方向吗?
Is a hash table the way forward?
我有一个看起来像这样的变量:
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()
不在这里为我工作?
- 哈希表是我想要的方式吗?
是的,哈希表是可行的方法。 $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 }
获取实际的字符串值。
我有一个看起来像这样的变量:
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()
不在这里为我工作? - 哈希表是我想要的方式吗?
是的,哈希表是可行的方法。 $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 }
获取实际的字符串值。