我正在尝试创建一个非常简单的脚本来检查是否安装了特定程序,如果安装了则返回该程序的版本号。
我已经能够运行脚本并能够返回二进制值(无论是否安装了程序),但不确定如何返回已安装程序的版本号。
我将发布的内容将是我正在做的事情,以便在安装程序时返回,并且在获得版本号时需要帮助。
function Check_Program_Installed {
$my_check = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, InstallDate |
Format-Table -AutoSize |
Out-String
# Check if Google Chrome is installed
$my_check -Match "Google Chrome"
}
Check_Program_Installed
最佳答案
如果您希望该函数查找特定的已安装程序而不是返回(表格)格式的字符串,那么您可以简单地执行以下操作:
function Check_Program_Installed {
[CmdletBinding()]
Param(
[Parameter(Position = 0, Mandatory=$true, ValueFromPipeline = $true)]
$Name
)
$app = Get-ItemProperty -Path "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" |
Where-Object { $_.DisplayName -match $Name } |
Select-Object DisplayName, DisplayVersion, InstallDate, Version
if ($app) {
return $app.DisplayVersion
}
}
Check_Program_Installed "Google Chrome"
这将在未找到时返回 $null
,或者像 70.0.3538.67
这样的字符串版本
关于windows - 检查是否安装了特定版本的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52856487/