每次我进入PowerShell,都是一样的。一些随机的象形文字从来没有像预期的那样起作用。这一次,我试图过滤Get-WindowsFeature的输出,以显示包含TIFF的所有行(因此PS等效于| grep TIFF或| find /i "TIFF")。这就是我到目前为止尝试过的,输出(在所有情况下都是空的),当然还有RTFM。前几行显示Get-WindowsFeature输出的结束,因此肯定至少有一条匹配行(从末尾第6行):
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> 为了解决这一次的问题,我放弃并将输出重定向到一个文件,并使用记事本找到它。但对于下一次,我仍然想知道需要什么样的随机字符集合才能让这件事正常工作。
为了得到额外的澄清,下面是一个预期的输出模型:
PS C:\Windows\system32> Get-WindowsFeature | Select-String <what exactly goes here?>
[ ] Windows TIFF IFilter Windows-TIFF-IFilter Available
PS C:\Windows\system32>发布于 2020-01-14 15:08:08
我重新编辑了标题。我想这可能是某种奇怪的定制格式,比如get。无论如何,在powershell中过滤属性的通常方法是这样的。有点像sql。这是一个服务器专用命令:
get-windowsfeature | where displayname -match tiff
get-windowsfeature | where name -match tiff
get-windowsfeature | where installstate -eq installed标题与此处的属性名称略有不同,这并不好。
对于这种情况,将findstr保存在工具箱中并不是个坏主意。注意,findstr通常是区分大小写的,没有'/i‘选项:
get-windowsfeature | findstr TIFF发布于 2020-01-14 13:13:47
您的问题是您误解了命令Select-String。
如Select-String文档中所述
在字符串和文件中查找文本。
你的Get-WindowsFeature既不是字符串也不是文件.它主要是某种对象,就像powershell中的大多数其他命令一样。您可以轻松地查看:
(Get-WindowsFeature).getType()我没有Windows可供测试,但我们也可以接受另一个命令,如Get-Alias
(Get-Alias).getType()输出:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array如您所见,这是一个数组,Select-String在文件或字符串上工作。
因此,@Lieven Keersmaekers在评论中说的必须是直觉的方式:
Get-windowsfeature | ? {$_.Name -match 'TIFF'}在使用Get-Alias的示例中
Get-Alias | ? {$_.DisplayName -match "Time"} 输出:
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中提到的那样):
(Get-Alias)[0] | fl *或者在你的情况下:
(Get-WindowsFeature)[0] | fl *选择字符串
在使用Select-String的情况下,在上面提到的参考页面中也使用了两种方法。
正如上面@Lieven Keersmaekers所提到的,您可以将其转换为如下所示的字符串:
(Get-WindowsFeature | Out-String) -split "\r\n" | Select-String TIFF另一种方法是将其打印到文件中,然后在文件上使用:
Get-WindowsFeature | Out-File -FilePath .\Windows_Feature.txt
Select-String -Path .\Windows_Feature.txt -Pattern "TIFF"请注意,不要忘记:您可能将其视为字符串,但对于PowerShell,它是一个对象,因此不可能在其上使用Select-String。
发布于 2020-10-20 21:46:03
我也在寻找解决问题的方法。
Get-WindowsFeature | Select-String "Web-Server"不匹配。
但我找到了这个解决方案。
PS C:> Get-WindowsFeature | Out-String -Stream | Select-String "Web-Server
[X] Web サーバー (IIS) Web-Server Installed“Out String -Stream”意思是以字符串的形式返回。
我试过你的模式了。
PS C:> Get-WindowsFeature | Out-String -Stream | Select-String "TIFF"
[ ] Windows TIFF IFilter Windows-TIFF-IFilter Available我在学英语,所以我很抱歉我的英语很差。2020/10/21(Wed)
https://stackoverflow.com/questions/59732154
复制相似问题