显示/过滤仅与列表中的字符串匹配的输出
我只想过滤或显示文件列表中匹配的数据。现在,我只使用了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
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
答案 0 :(得分:1)
正如 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
)
答案 1 :(得分:0)
假定$ mylistfile为get-content myfilelist.txt
,则select-string会将通过管道传送到其中的对象转换为字符串。因此,名称为选择字符串,而名称为选择对象。您可以通过这种方式进行操作,仍然解决对象的正确属性,并使用带有模式数组的选择字符串:
# $mylistfile is 'KB4493132','KB4499406'
$SearchResult | where { $_.title | select-string $mylistfile }