匹配两个列表并使用Powershell查找卸载字符串

问题描述

我的脚本有问题。 我想制作一个脚本,该脚本列出在特定注册表路径中找到的软件列表 并查看该软件是否等同于已安装的软件。如果是这样,它应该向我输出卸载字符串。 但现在它无法按需工作。即使它相似,也永远不会显示我想要的输出。作为示例,我将Git程序作为品牌,在软件中我获得了Git版本2.26.2,但是当我选择git时,它不会输出卸载字符串。

我的代码是:

$branding = Get-ChildItem "HKLM:\Software\DLR\branding" | Get-ItemProperty | Select-Object -expandProperty ProgramName
$software = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall,HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Select-Object -ExpandProperty displayName

ForEach ($brandinglist in $branding) {
    $objComboBox.Items.Add("$brandinglist")
}

$objComboBox_SelectedindexChanged=
{
    $selectme = $objComboBox.SelectedItem
    Write-Host $selectme
    if ("$selectme" -like "*$software*") {
        $uninstall = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall,HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.displayName -match "$electme" } | Select-Object -Property UninstallString
        Write-Host "$uninstall"
    }
}

解决方法

您尝试的-like比较是错误的,在该比较中,您将选定的项目与显示名数组进行了比较,而显示名数组则无法正常工作。

此外,没有理由使用几乎相同的代码两次获取“卸载”字符串和“显示名称”。

尝试

# get a string array of program names
$branding = Get-ChildItem -Path 'HKLM:\Software\DLR\Branding' | Get-ItemProperty | Select-Object -ExpandProperty ProgramName

$regPaths = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
# get an object array of DisplayName and UninstallStrings
$software = Get-ChildItem -Path $regPaths | Get-ItemProperty | Select-Object DisplayName,UninstallString

# fill the combobox with the (string array) $branding
$objCombobox.Items.AddRange($branding)

$objComboBox.Add_SelectedIndexChanged ({
    $selectme = $objCombobox.SelectedItem
    Write-Host $selectme
    # get the objects that have a displayname like the selected item and write out the matching Uninstall strings
    $software | Where-Object {$_.DisplayName -like "*$selectme*" } | ForEach-Object {
        Write-Host $_.UninstallString
    }
})