Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 匹配两个列表并使用Powershell查找卸载字符串_Windows_Powershell_Operators - Fatal编程技术网

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

Windows 匹配两个列表并使用Powershell查找卸载字符串,windows,powershell,operators,Windows,Powershell,Operators,我的脚本有问题。 我想制作一个脚本,该脚本生成在特定注册表路径中找到的软件列表 看看这个软件是否等于已安装的软件。如果是这样,它应该输出卸载字符串给我。 但现在它并没有像人们所希望的那样发挥作用。它从未向我显示我想要的输出,即使它相似。例如,我有程序Git作为品牌,在软件中我得到了Git版本2.26.2,但当我选择Git时,它不会输出卸载字符串 我的代码是: $branding=Get ChildItem“HKLM:\Software\DLR\branding”| Get ItemPropert

我的脚本有问题。 我想制作一个脚本,该脚本生成在特定注册表路径中找到的软件列表 看看这个软件是否等于已安装的软件。如果是这样,它应该输出卸载字符串给我。 但现在它并没有像人们所希望的那样发挥作用。它从未向我显示我想要的输出,即使它相似。例如,我有程序Git作为品牌,在软件中我得到了Git版本2.26.2,但当我选择Git时,它不会输出卸载字符串

我的代码是:

$branding=Get ChildItem“HKLM:\Software\DLR\branding”| Get ItemProperty |选择对象-expandProperty ProgramName
$software=Get-ChildItem-路径HKLM:\software\Microsoft\Windows\CurrentVersion\Uninstall,HKLM:\software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty |选择对象-ExpandProperty DisplayName
ForEach($brandinglist,单位为$branding){
$objCombobox.Items.Add(“$brandinglist”)
}
$OBJCOMBOX\您选择的索引已更改=
{
$selectme=$OBJCOMBOX.SelectedItem
写入主机$selectme
如果(“$selectme”-类似“*$software*”){
$uninstall=Get-ChildItem-路径HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\uninstall,HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\uninstall | Get-ItemProperty |其中对象{$\显示名称-匹配“$electme”}|选择对象-属性卸载字符串
写入主机“$uninstall”
}
}

您尝试了类似于
的比较错误,即您将所选项目与不起作用的显示名数组进行比较

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

试一试


使用
Set StrictMode-version'latest'
会告诉您有
$selectme
$electme
变量。这看起来像是一个输入错误,无论是在你的代码中还是在问题中。同上,另外你已经创建了一个scriptblock,但这是(据我所知)未附加的。使用
$objComboBox.Add_SelectedIndexChanged({…})
,这样当组合框中的索引发生变化时,代码就会运行@Theo所做的一切就是完美的!我现在明白了。如果还有什么问题,我会回到你身边。Thanks@Flighty谢谢你的反馈。请不要忘记通过点击左边的复选标记图标来接受答案。我在下一步有点麻烦。事实上,我想添加一个删除按钮,它应该卸载按钮点击时选择的软件,但我有点卡住了。我没有收到任何错误,但脚本只是在单击按钮后显示一个空行。我使用的函数将在单击按钮后运行。函数delete{If($uninstall){$uninst=$uninstall&cmd/c$uninst/quiet/norestart}忘了提到…$uninstall=$software | Where Object{$\像“$selectme”}ForEach Object{Write Host$\ UninstallString
# 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
    }
})