【发布时间】:2019-06-29 21:57:04
【问题描述】:
显示/过滤只匹配我列表中的字符串的输出
我想过滤或仅显示在我的文件列表中匹配的数据。现在我只使用了 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
【问题讨论】:
-
您应该重新阅读 Select-String 的作用,尤其是
-List参数。此外,$myfilelist应该是一个包含 KB 编号的数组,而不仅仅是一个文件名。 -
你可以在这个场景中使用 Where-Object..