如果未安装软件,请安装Chocolatey软件包,但如果已安装较新版本的软件,请跳过安装?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果未安装软件,请安装Chocolatey软件包,但如果已安装较新版本的软件,请跳过安装?相关的知识,希望对你有一定的参考价值。

我正在使用以下PowerShell脚本通过Octopus Deploy步骤安装dotnetcore-windowshosting版本1.1.1。

ChocolateyPackageId等于“dotnetcore-windowshosting”,$ ChocolateyPackageVersion等于“1.1.1”。

但是,目标计算机安装了新版本的DotNetCore.1.0.4_1.1.1-WindowsHosting.exe,而不是由Chocolatey软件包安装的版本。因此,会出现错误,提醒我目标计算机已安装了较新版本。

如何在脚本中使用cinst安装软件包,但是,如果已安装软件包(或更新版本),请忽略并不引发错误?

$chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "Machine") + "in"
if(-not (Test-Path $chocolateyBin)) {
    Write-Output "Environment variable 'ChocolateyInstall' was not found in the system variables. Attempting to find it in the user variables..."
    $chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "User") + "in"
}

$cinst = "$chocolateyBincinst.exe"
$choco = "$chocolateyBinchoco.exe"

if (-not (Test-Path $cinst) -or -not (Test-Path $choco)) {
    throw "Chocolatey was not found at $chocolateyBin."
}

if (-not $ChocolateyPackageId) {
    throw "Please specify the ID of an application package to install."
}

$chocoVersion = & $choco --version
Write-Output "Running Chocolatey version $chocoVersion"

$chocoArgs = @()
if([System.Version]::Parse($chocoVersion) -ge
   [System.Version]::Parse("0.9.8.33")) {
    Write-Output "Adding --confirm to arguments passed to Chocolatey"
    $chocoArgs += @("-y", "")
}

if (-not $ChocolateyPackageVersion) {
    Write-Output "Installing package $ChocolateyPackageId from the Chocolatey package repository..."
    & $cinst $ChocolateyPackageId $($chocoArgs)
}
else {
    Write-Output "Installing package $ChocolateyPackageId version $ChocolateyPackageVersion from the Chocolatey package repository..." & $cinst $ChocolateyPackageId -Version $ChocolateyPackageVersion $($chocoArgs)
}
答案

Open Source

继续使用您正在处理的脚本,但是请检查要安装的内容的退出代码,它可能具有指定已安装的较新版本的有效退出代码。

Commercial

可能实现您正在寻找的目标的最佳方式已经存在。 Chocolatey for Business具有sync命令和自动同步功能。 https://chocolatey.org/docs/features-synchronize

如果您运行choco sync然后调用您的安装,那么您将拥有由Chocolatey管理的该软件包的更新版本。因此它会忽略你的脚本。

choco sync
choco upgrade <name> -y <options>

如果您的源中有更新的版本,如果未安装和升级软件包,则此处的升级将实现安装

另一答案

感谢所有的建议。以下似乎对我有用

$chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "Machine") + "in"
if(-not (Test-Path $chocolateyBin)) {
    Write-Output "Environment variable 'ChocolateyInstall' was not found in the system variables. Attempting to find it in the user variables..."
    $chocolateyBin = [Environment]::GetEnvironmentVariable("ChocolateyInstall", "User") + "in"
}

$cinst = "$chocolateyBincinst.exe"
$choco = "$chocolateyBinchoco.exe"

if (-not (Test-Path $cinst) -or -not (Test-Path $choco)) {
    throw "Chocolatey was not found at $chocolateyBin."
}

if (-not $ChocolateyPackageId) {
    throw "Please specify the ID of an application package to install."
}

$chocoVersion = & $choco --version
Write-Output "Running Chocolatey version $chocoVersion"

$chocoArgs = @()
if([System.Version]::Parse($chocoVersion) -ge [System.Version]::Parse("0.9.8.33")) {
    Write-Output "Adding --confirm to arguments passed to Chocolatey"
    $chocoArgs += @("-y", "")
}

if (-not $ChocolateyPackageVersion) {
    Write-Output "Installing package $ChocolateyPackageId from the Chocolatey package repository..."
    & $cinst $ChocolateyPackageId $($chocoArgs)
} else {
    Write-Output "Installing package $ChocolateyPackageId version $ChocolateyPackageVersion from the Chocolatey package repository..."
    & $cinst $ChocolateyPackageId -Version $ChocolateyPackageVersion $($chocoArgs) --force
}

$codes = $IgnoreExitCodes -split ','

if ($codes -Contains $lastExitCode)
{
    exit 0
}
另一答案

免责声明:这是hacky。而不是一个好办法。

function Test-ChocolateyPackageInstalled {
    Param (
        [ValidateNotNullOrEmpty()]
        [string]
        $Package
    )

    Process {
        if (Test-Path -Path $env:ChocolateyInstall) {
            $packageInstalled = Test-Path -Path $env:ChocolateyInstalllib$Package
        }
        else {
            Write-Warning "Can't find a chocolatey install directory..."
        }

        return $packageInstalled
    }
}

$package = "dotnetcore-windowshosting"


if (Test-ChocolateyPackageInstalled -Package $package) {
    Write-Warning "Package is already installed"
}
else {
    choco install $package -confirm
}

我只是通过Chocolatey安装了这个,所以当其他人手动安装时,我将很难测试你的场景,但方法可能有腿。

因此,请注意,这可能无法完全解决您的问题;但它可以帮助未来的读者。

以上是关于如果未安装软件,请安装Chocolatey软件包,但如果已安装较新版本的软件,请跳过安装?的主要内容,如果未能解决你的问题,请参考以下文章

windows上更酷的软件安装方式—chocolatey

Haskell 的 GHC 未安装

Chocolatey 简介(软件自动化管理工具)

Installing Chocolatey

Chocolatey 方便的windows 包管理工具

使用ansible安装windows软件包