Select-String 不显示与 Get-AppxPackage 的所有匹配项 [英] Select-String Doesn't Show All Matches With Get-AppxPackage

查看:17
本文介绍了Select-String 不显示与 Get-AppxPackage 的所有匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Get-AppxPackage 在我的 PC 上安装了所有软件包,并且我试图在使用 Select-StringSelect-String.

I get all the packages installed on my PC using Get-AppxPackage and I'm trying to find all the matches in that with N lines before and after using Select-String.

但是,选择字符串仅将匹配项显示为单行,也没有显示所有匹配项.这只发生在我从 Get-AppxPackage 输出管道时,而不是将它写入文件然后执行 cat <filename>|选择字符串 ....

However, the select string is only showing a matches as single line and it's not showing all the matches either. This only happens when I pipe the output from Get-AppxPackage and not if I write it to a file and then do cat <filename> | select-string ....

正如您在下面的示例中看到的使用管道和 cat 的两个结果.我对来自 cat 之类的结果感兴趣,即有关应用程序的详细信息.

As you can see in the example below the two results of using pipe and cat. I'm interested in results like from cat i.e. detailed info about the app.

那么我在这里做错了什么?为什么输出不同?

So what am I doing wrong here? Why is the output different?

示例(每个人都应该拥有 MS Edge,所以我将以此为例):

Example (everyone should have MS Edge so I'll use that as an example) :

PS > Get-AppxPackage | Select-String -pattern 'edge' -context 3, 3 -allmatches

  Microsoft.Windows.StartMenuExperienceHost_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
  Microsoft.Windows.Cortana_1.13.0.18362_neutral_neutral_cw5n1h2txyewy
  Microsoft.AAD.BrokerPlugin_1000.18362.329.0_neutral_neutral_cw5n1h2txyewy
> Microsoft.MicrosoftEdge_44.18362.329.0_neutral__8wekyb3d8bbwe
  Microsoft.Windows.CloudExperienceHost_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
  Microsoft.Windows.ContentDeliveryManager_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
  Windows.CBSPreview_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
  Microsoft.Windows.Apprep.ChxApp_1000.18362.329.0_neutral_neutral_cw5n1h2txyewy
  Microsoft.Win32WebViewHost_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
  Microsoft.PPIProjection_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
> Microsoft.MicrosoftEdgeDevToolsClient_1000.18362.329.0_neutral_neutral_8wekyb3d8bbwe
  Microsoft.LockApp_10.0.18362.329_neutral__cw5n1h2txyewy
> Microsoft.EdgeDevtoolsPlugin_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
  Microsoft.ECApp_10.0.18362.329_neutral__8wekyb3d8bbwe
  Microsoft.CredDialogHost_10.0.18362.329_neutral__cw5n1h2txyewy
  Microsoft.BioEnrollment_10.0.18362.329_neutral__cw5n1h2txyewy

PS > cat .appx-packages.txt | select-string -pattern 'edge' -context 3, 3 -allmatches

  SignatureKind     : System
  Status            : Ok

> Name              : Microsoft.MicrosoftEdge
  Publisher         : CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
  Architecture      : Neutral
  ResourceId        :
  Version           : 44.18362.329.0
> PackageFullName   : Microsoft.MicrosoftEdge_44.18362.329.0_neutral__8wekyb3d8bbwe
> InstallLocation   : C:WindowsSystemAppsMicrosoft.MicrosoftEdge_8wekyb3d8bbwe
  IsFramework       : False
> PackageFamilyName : Microsoft.MicrosoftEdge_8wekyb3d8bbwe
  PublisherId       : 8wekyb3d8bbwe
  IsResourcePackage : False
  IsBundle          : False
  SignatureKind     : System
  Status            : Ok

> Name              : Microsoft.MicrosoftEdgeDevToolsClient
  Publisher         : CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
  Architecture      : Neutral
  ResourceId        : neutral
  Version           : 1000.18362.329.0
> PackageFullName   : Microsoft.MicrosoftEdgeDevToolsClient_1000.18362.329.0_neutral_neutral_8wekyb3d8bbwe
> InstallLocation   : C:WindowsSystemAppsMicrosoft.MicrosoftEdgeDevToolsClient_8wekyb3d8bbwe
  IsFramework       : False
> PackageFamilyName : Microsoft.MicrosoftEdgeDevToolsClient_8wekyb3d8bbwe
  PublisherId       : 8wekyb3d8bbwe
  IsResourcePackage : False
  IsBundle          : False
  SignatureKind     : System
  Status            : Ok

> Name              : Microsoft.EdgeDevtoolsPlugin
  Publisher         : CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
  Architecture      : Neutral
  ResourceId        : neutral
  Version           : 10.0.18362.329
> PackageFullName   : Microsoft.EdgeDevtoolsPlugin_10.0.18362.329_neutral_neutral_cw5n1h2txyewy
> InstallLocation   : C:WindowsSystemAppsMicrosoft.EdgeDevtoolsPlugin_cw5n1h2txyewy
  IsFramework       : False
> PackageFamilyName : Microsoft.EdgeDevtoolsPlugin_cw5n1h2txyewy
  PublisherId       : cw5n1h2txyewy
  IsResourcePackage : False
  IsBundle          : False


推荐答案

Select-String,当给定输入other而不是字符串时,使用简单的.ToString() 字符串化[1] 在查找给定模式之前对每个输入对象进行.

Select-String, when given input other than strings, uses simple .ToString() stringification[1] on each input object before looking for the given pattern.

在您的情况下,由 Get-AppXPackage 输出的 [Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage] 实例将字符串化为完整的包名称(例如,Microsoft.MicrosoftEdge_44.18362.387.0_neutral__8wekyb3d8bbwe),它解释了您的输出.

In your case, the [Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage] instances output by Get-AppXPackage stringify to the full package names (e.g., Microsoft.MicrosoftEdge_44.18362.387.0_neutral__8wekyb3d8bbwe), which explains your output.

为了使Select-String搜索对象for-display字符串表示 - 因为它们会打印到控制台,以及它们会出现在使用 >/Out-File 保存的文件中(catOut-File 在 Windows 上的内置别名) - 令人惊讶的是,您必须使用 Out-String -Stream 作为中间管道段:

In order to make Select-String search the for-display string representations of objects - as they would print to the console and as they would appear in a file saved to with > / Out-File (cat is Out-File's built-in alias on Windows) - you must, surprisingly, use Out-String -Stream as an intermediate pipeline segment:

Get-AppxPackage | Out-String -Stream | Select-String -Pattern 'edge' -Context 3, 3

Out-String 使用 PowerShell 的 格式化系统以生成输入对象的人性化显示表示,与默认控制台输出、Format-* cmdlet 和 >/Out-File 做.
-Stream 使输出行通过管道一一发送.

Out-String uses PowerShell's formatting system to produce human-friendly display representations of the input objects, the same way that default console output, the Format-* cmdlets, and > / Out-File do.
-Stream causes the output lines to be sent through the pipeline one by one.

鉴于该解决方案既不明显又麻烦,如果 Select-String 直接支持此行为就好了,理想情况下默认,但至少在通过 switch 参数选择加入的基础上 - 请参阅 GitHub 上的功能请求 #10726 - 如果您同意,请对该提案进行投票.

Given that the solution is both non-obvious and cumbersome, it would be nice if Select-String directly supported this behavior, ideally by default, but at least on an opt-in basis via a switch parameter - see feature request #10726 on GitHub - up-vote the proposal there if you agree.

[1] 更准确地说,.psobject.ToString() 被调用,或者按原样调用,或者 - 如果对象的 ToString 方法支持 IFormatProvider-typed 参数 - 作为 .psobject.ToString([cultureinfo]::InvariantCulture) 以获得 culture-invariant 表示 - 见 这个答案了解更多信息.

[1] More accurately, .psobject.ToString() is called, either as-is, or - if the object's ToString method supports an IFormatProvider-typed argument - as .psobject.ToString([cultureinfo]::InvariantCulture) so as to obtain a culture-invariant representation - see this answer for more information.

这篇关于Select-String 不显示与 Get-AppxPackage 的所有匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆