【问题标题】:Display/Filter the output that only match the string from my list显示/过滤仅与我列表中的字符串匹配的输出
【发布时间】: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..

标签: powershell windows-update


【解决方案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

【讨论】:

    【解决方案2】:

    假设 $mylistfile 是 get-content myfilelist.txt,select-string 会将通过管道传输到其中的对象转换为字符串。因此名称选择字符串与选择对象。您可以这样做,仍然处理对象的正确属性,并使用带有模式数组的 select-string:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多