Display/Filter 只匹配我列表中的字符串的输出

Display/Filter the output that only match the string from my list

Display/Filter 只匹配我列表中的字符串的输出

我只想过滤或显示与我的文件列表匹配的数据。现在我只是使用 select-string -Pattern mylistfile.txt ,结果只会显示字符串匹配的整行。如何在输出中包含所有数据集?

$Criteria = "IsIntalled=0"
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$myfilelist = C:\myfilelist.txt
$SearchResult = $Searcher.Search($Criteria).Updates 
$filteredResult = $SearchResult | select-string -Pattern $mylistfile -list 

输出:$SearchResult --- 我最多打印 5 行输出

Title                           : 2019-04 Update for Windows 7 for x64-based 
                                  Systems (KB4493132)
AutoSelectOnWebSites            : False
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

Title                           : 2019-05 Security and Quality Rollup for .NET 
                                  Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 
                                  4.7, 4.7.1, 4.7.2, 4.8 for Windows 7 and 
                                  Server 2008 R2 for x64 (KB4499406)
AutoSelectOnWebSites            : True
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject


Title                           : Windows Malicious Software Removal Tool x64 
                                  - June 2019 (KB890830)
AutoSelectOnWebSites            : True
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

输出:$filteredResult

Systems (KB4493132)   
Server 2008 R2 for x64 (KB4499406)

mylistfile.txt

KB4493132
KB4499406

我的预期输出 -- 它只显示 (KB4493132) 和 (KB4499406) 的数据集

Title                           : 2019-04 Update for Windows 7 for x64-based 
                                  Systems (KB4493132)
AutoSelectOnWebSites            : False
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

Title                           : 2019-05 Security and Quality Rollup for .NET 
                                  Framework 3.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 
                                  4.7, 4.7.1, 4.7.2, 4.8 for Windows 7 and 
                                  Server 2008 R2 for x64 (KB4499406)
AutoSelectOnWebSites            : True
BundledUpdates                  : System.__ComObject
CanRequireSource                : False
Categories                      : System.__ComObject

正如 Niraj Gajjar 所说,使用 Where-Object。使用 KBArticleIDs 属性 进行匹配,像这样:

$filter = Get-Content -Path "C:\temp\kblist.txt"

$criteria = "IsInstalled=0"
$searcher = New-Object -ComObject Microsoft.Update.Searcher
$searchResult = $searcher.Search($criteria).Updates     
$filteredResult = $searchResult | Where-Object { $_.KBArticleIDs -in $filter }

其中 "C:\temp\kblist.txt" 包含知识库文章编号(没有 KB!)

(顺便说一句:您在 $Criteria 中也有错别字:IsIntalled)

假设 $mylistfile 是 get-content myfilelist.txt,select-string 将管道输入的对象转换为字符串。因此名称 select-string,与 select-object 相对。您可以这样做,仍然寻址对象的 属性,并使用带有模式数组的 select-string:

# $mylistfile is 'KB4493132','KB4499406'
$SearchResult | where { $_.title | select-string $mylistfile }