powershell - PowerShell Where-Object - 没有返回 - 我一定做错了什么
问题描述
我运行以下命令并没有收到任何回报 - 我查看了 reg 并验证了显示名称是否存在。知道我做错了什么吗?
$OfficeYearsToLookFor = @(
'2010',
'2013',
'2016',
'2019')
$status = (Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' |
Where-Object {(($_.DisplayName -contains 'Office') -and ($_.DisplayName -contains 'Microsoft') -and ( $OfficeYearsToLookFor -contains $_.DisplayName )) } |
Select-Object DisplayName, DisplayVersion, UninstallString )
$status
Write-Output "Status: $status"
解决方案
您可能只是使这个用例过于复杂。只需使用简单的 RegEx 匹配即可。该Where-Object
声明并不是真正需要的。例如:
正则表达式匹配以获取与字符串和数字模式匹配的所有内容
(Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') -match '.*Microsoft.*Office.*\d{4}.*' |
Select-Object -Property DisplayName, DisplayVersion, UninstallString
# Results
<#
DisplayName DisplayVersion UninstallString
----------- -------------- ---------------
Microsoft Project - en-us 16.0.13801.20360 "C:\Program Files\...
Microsoft Office Professional Plus 2016 - en-us 16.0.13801.20360 "C:\Program Files\...
...
Office 16 Click-to-Run Licensing Component 16.0.13801.20360 MsiExec.exe /...
...
#>
正则表达式匹配以获取与字符串和数字模式匹配的所有内容,并仅选择所需的属性和年份字符串匹配的行
((Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') -match '.*Microsoft.*Office.*\d{4}.*' |
Select-Object -Property DisplayName, DisplayVersion, UninstallString ) -match '2010|2013|2016|2019'
# Results
<#
DisplayName DisplayVersion UninstallString
----------- -------------- ---------------
Microsoft Office Professional Plus 2016 - en-us 16.0.13801.20360 "C:\Program Files\...
#>
推荐阅读
- debugging - 是否可以在即时窗口中执行汇编语言指令
- r - 是否有基于用户提供的向量来分割数据的 R 函数?
- scipy - 在 IronPython 上升级 Scipy
- javascript - 如何在浏览器地址栏中的地址之前插入附加标题?
- postgresql - 如何连接到 GitHub Action 的作业服务?
- ios - 带有-webkit-overflow-scrolling的可滚动Div:iFrame内的触摸在iOS 10移动Safari中不起作用
- php - 如何正确(独立)包含文件
- angular - Angular PWA:未检测到匹配的服务工作者
- angular - 如何修复 angular-cli 中的原理图问题/无法创建新的 Angular 应用程序
- xml - 如何将xml文本作为简单文本