【问题标题】:PowerShell Where-Object - Nothing returned - I must be doing something wrongPowerShell Where-Object - 没有返回 - 我一定做错了什么
【发布时间】:2021-07-01 12:35:56
【问题描述】:

我运行以下命令并没有收到任何回报 - 我查看了 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"

【问题讨论】:

  • 这不是 -contains 所做的,它只匹配整个字符串。这是一个常见的混淆点。
  • 当我使用 -like 'Office' (Office 之前和之后的星号)时仍然没有结果 - 我尝试了所有类别,甚至作为一个没有快乐的单曲。
  • 你确定你有Where-Object displayname -like '*office*'吗?尝试删除其他过滤器以确保它是正确的。例如,对于 O365 安装,显示名称为 Office 16 Click-to-Run
  • 谢谢!我希望解决的最后一件事是写入输出之前的 $status 。没有变化 - 所以我发布了 - 现在当我按照你的建议做 - 它有效!如果我删除最后一个 $status - 不高兴!感谢您的帮助!

标签: powershell powershell-3.0 powershell-remoting


【解决方案1】:

您可能只是让这个用例过于复杂。只需使用简单的 RegEx 匹配即可。 Where-Object 语句并不是真正需要的。例如:

RegEx 匹配以获取与字符串和数字模式匹配的所有内容

(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 /...                                                                             
...
#>  

RegEx 匹配以获取与字符串和数字模式匹配的所有内容,并仅选择所需的属性和年份字符串匹配的行

((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\...
#>

【讨论】:

    【解决方案2】:

    或者你可以使用get-package:

    get-package *microsoft*office*201[0369]* | 
      select name,version,@{n='Uninstallstring';e={$_.metadata['uninstallstring']}}
    
    Name                                    Version        Uninstallstring
    ----                                    -------        ---------------
    Microsoft Office Professional Plus 2016 16.0.4266.1001 "C:\Program Files\Common Files\Microsoft Shared\OFFICE16\Office Setup Controller\setup.exe" /uninstall PROPLUS /dll OSETUP.DLL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多