首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell Select不匹配,尽管尝试了TFM中的所有内容

PowerShell Select不匹配,尽管尝试了TFM中的所有内容
EN

Stack Overflow用户
提问于 2020-01-14 10:41:44
回答 3查看 450关注 0票数 2

每次我进入PowerShell,都是一样的。一些随机的象形文字从来没有像预期的那样起作用。这一次,我试图过滤Get-WindowsFeature的输出,以显示包含TIFF的所有行(因此PS等效于| grep TIFF| find /i "TIFF")。这就是我到目前为止尝试过的,输出(在所有情况下都是空的),当然还有RTFM。前几行显示Get-WindowsFeature输出的结束,因此肯定至少有一条匹配行(从末尾第6行):

代码语言:javascript
复制
Display Name                                   Name                    Install State
------------                                   ----                    -------------
[ ] Windows Search Service                     Search-Service          Available
[ ] Windows Server Backup                      Windows-Server-Backup   Available
[ ] Windows Server Migration Tools             Migration               Available
[ ] Windows Standards-Based Storage Management WindowsStorageManage... Available
[ ] Windows TIFF IFilter                       Windows-TIFF-IFilter    Available
[ ] WinRM IIS Extension                        WinRM-IIS-Ext           Available
[ ] WINS Server                                WINS                    Available
[ ] Wireless LAN Service                       Wireless-Networking     Available
[X] WoW64 Support                              WoW64-Support           Installed
[ ] XPS Viewer                                 XPS-Viewer              Available


PS C:\Windows\system32> get-windowsfeature | select-string "TIFF"
PS C:\Windows\system32> get-windowsfeature | select-string TIFF
PS C:\Windows\system32> get-windowsfeature | select-string 'TIFF'
PS C:\Windows\system32> get-windowsfeature | select-string -Pattern 'TIFF'
PS C:\Windows\system32> get-windowsfeature | select-string -Pattern 'TIFF' -AllMatches
PS C:\Windows\system32> get-windowsfeature | select-string -Pattern "TIFF"
PS C:\Windows\system32> get-windowsfeature | select-string -Pattern TIFF
PS C:\Windows\system32> Get-WindowsFeature | Select-String -Pattern TIFF
PS C:\Windows\system32> Get-WindowsFeature | Select-String -Pattern 'TIFF'
PS C:\Windows\system32> $A = Get-WindowsFeature | Select-String -Pattern 'TIFF'
PS C:\Windows\system32> $A
PS C:\Windows\system32> Get-WindowsFeature | Select-String -Pattern 'TIFF' -SimpleMatch
PS C:\Windows\system32> Get-WindowsFeature | Select-String -SimpleMatch 'TIFF'
PS C:\Windows\system32> Get-WindowsFeature |& Select-String -SimpleMatch 'TIFF'
PS C:\Windows\system32> Get-WindowsFeature | Select-String -SimpleMatch 'TIFF' 2>&1
PS C:\Windows\system32> Get-WindowsFeature 2>&1 | Select-String -SimpleMatch 'TIFF'
PS C:\Windows\system32> Get-WindowsFeature 3>&1 | Select-String -SimpleMatch 'TIFF'
PS C:\Windows\system32> 

为了解决这一次的问题,我放弃并将输出重定向到一个文件,并使用记事本找到它。但对于下一次,我仍然想知道需要什么样的随机字符集合才能让这件事正常工作。

为了得到额外的澄清,下面是一个预期的输出模型:

代码语言:javascript
复制
PS C:\Windows\system32> Get-WindowsFeature | Select-String <what exactly goes here?>
[ ] Windows TIFF IFilter                                Windows-TIFF-IFilter           Available

PS C:\Windows\system32>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-14 15:08:08

我重新编辑了标题。我想这可能是某种奇怪的定制格式,比如get。无论如何,在powershell中过滤属性的通常方法是这样的。有点像sql。这是一个服务器专用命令:

代码语言:javascript
复制
get-windowsfeature | where displayname -match tiff
get-windowsfeature | where name -match tiff
get-windowsfeature | where installstate -eq installed

标题与此处的属性名称略有不同,这并不好。

对于这种情况,将findstr保存在工具箱中并不是个坏主意。注意,findstr通常是区分大小写的,没有'/i‘选项:

代码语言:javascript
复制
get-windowsfeature | findstr TIFF
票数 3
EN

Stack Overflow用户

发布于 2020-01-14 13:13:47

您的问题是您误解了命令Select-String

Select-String文档中所述

在字符串和文件中查找文本。

参考资料:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7

你的Get-WindowsFeature既不是字符串也不是文件.它主要是某种对象,就像powershell中的大多数其他命令一样。您可以轻松地查看:

代码语言:javascript
复制
(Get-WindowsFeature).getType()

我没有Windows可供测试,但我们也可以接受另一个命令,如Get-Alias

代码语言:javascript
复制
(Get-Alias).getType()

输出:

代码语言:javascript
复制
IsPublic IsSerial Name      BaseType
-------- -------- ----      --------
True     True     Object[]  System.Array

如您所见,这是一个数组,Select-String在文件或字符串上工作。

因此,@Lieven Keersmaekers在评论中说的必须是直觉的方式:

代码语言:javascript
复制
Get-windowsfeature | ? {$_.Name -match 'TIFF'}

在使用Get-Alias的示例中

代码语言:javascript
复制
Get-Alias | ? {$_.DisplayName -match "Time"} 

输出:

代码语言:javascript
复制
CommandType     Name                    Version    Source
-----------     ----                    -------    ------
Alias           gtz -> Get-TimeZone     3.1.0.0    Microsoft.PowerShell.Management
Alias           stz -> Set-TimeZone     3.1.0.0    Microsoft.PowerShell.Management

如你所见,我拿走了DisplayName。这是因为Name只有gtz / stz,但是我想搜索->旁边的文本。

要显示所有您有访问权限的字段,可以用它打印(如注释中@Lieven Keersmaekers中提到的那样):

代码语言:javascript
复制
(Get-Alias)[0] | fl *

或者在你的情况下:

代码语言:javascript
复制
(Get-WindowsFeature)[0] | fl *

选择字符串

在使用Select-String的情况下,在上面提到的参考页面中也使用了两种方法。

正如上面@Lieven Keersmaekers所提到的,您可以将其转换为如下所示的字符串:

代码语言:javascript
复制
(Get-WindowsFeature | Out-String) -split "\r\n" | Select-String TIFF

另一种方法是将其打印到文件中,然后在文件上使用:

代码语言:javascript
复制
Get-WindowsFeature | Out-File -FilePath .\Windows_Feature.txt
Select-String -Path .\Windows_Feature.txt -Pattern "TIFF"

请注意,不要忘记:您可能将其视为字符串,但对于PowerShell,它是一个对象,因此不可能在其上使用Select-String

票数 2
EN

Stack Overflow用户

发布于 2020-10-20 21:46:03

我也在寻找解决问题的方法。

代码语言:javascript
复制
Get-WindowsFeature | Select-String "Web-Server"

不匹配。

但我找到了这个解决方案。

代码语言:javascript
复制
PS C:> Get-WindowsFeature | Out-String -Stream | Select-String "Web-Server

[X] Web サーバー (IIS)                                      Web-Server                     Installed

“Out String -Stream”意思是以字符串的形式返回。

我试过你的模式了。

代码语言:javascript
复制
PS C:> Get-WindowsFeature | Out-String -Stream | Select-String "TIFF"

[ ] Windows TIFF IFilter                                Windows-TIFF-IFilter           Available

我在学英语,所以我很抱歉我的英语很差。2020/10/21(Wed)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59732154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档