显示/过滤仅与我列表中的字符串匹配的输出
我想过滤或仅显示匹配文件列表中的数据。现在,我只使用了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
输出:$ FILFEREDRESULT
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
as niraj gajjar 说,使用 Where-Object
。使用KBArticleIDs
属性进行匹配,如下:
$filter = Get-Content -Path "C:tempkblist.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文章编号(无KB
!(
(顺便说一句:您在$标准中也有错字:IsIntalled
(
假设$ mylistfile是 get-content myfilelist.txt
,select-string将管道的对象变成字符串。因此,名称select-string,vs select-object。您可以这样做,仍然可以解决对象的正确属性,并使用一系列模式使用Select-string:
# $mylistfile is 'KB4493132','KB4499406'
$SearchResult | where { $_.title | select-string $mylistfile }