ps/Modules/Alkami.PowerShell.SDK/Public/Get-LocalPackages.ps1
2023-05-30 22:51:22 -07:00

73 lines
2.9 KiB
PowerShell

function Get-LocalPackages() {
<#
.SYNOPSIS
Set the recovery options to restart for all Alkami SDK services.
#>
[CmdletBinding()]
param(
[System.Collections.ArrayList] [Parameter(Mandatory=$true)] $Packages
)
Write-Host "$logLead : Gathering locally installed package details"
# highly brittle command here with the choco list -lr
# -l = local packages only
# -r = limit output (things are pipe delimited, id|version)
$allLocallyInstalledPackagesRaw = (choco list -lr)
$allLocallyInstalledPackages = @{}
$localInstalledPackages = @()
foreach ($package in $allLocallyInstalledPackagesRaw) {
if ($package.IndexOf('|') -eq -1) {
continue
}
$splits = $package -split '\|'
$id = $splits[0]
$version = $splits[1]
$allLocallyInstalledPackages[$id] = $version
$localInstalledPackages += $splits[0]
Write-Verbose "$id $version"
}
Write-Host "$logLead : Locally installed package details gathered"
# When we built the tier package lists via automation, we ensured that each package is only in one tier/feature location, so it can't be in two lists at once.
# Because we rely on automation, we are not checking that again
$newPackagesToInstall = @()
$packagesToUpgrade = @()
$packagesToRemove = @()
$tierCount = 0
foreach ($tier in $Packages) {
foreach ($package in $tier) {
if ($null -ne $allLocallyInstalledPackages[$package.Id]) {
# package is installed locally
if ($allLocallyInstalledPackages[$package.Id] -eq $package.Version) {
# no need to touch the package, it's already here
$packageVersion = "[@($package.Version)]"
if ($package.VersionCanTakeAny) {
$packageVersion = "the latest available version"
}
"Found already existing matched version of [$($package.Id)] @ [$($allLocallyInstalledPackages[$package.Id])]" | Out-Default -Transcript | Out-Null
#$transcriptOfChanges += "Upgrading package [$($package.Id)] from version [$($allLocallyInstalledPackages[$package.Id])] to $packageVersion"
} else {
$package | Add-Member -NotePropertyName 'Tier' -NotePropertyValue $tierCount
$packagesToUpgrade += $package
Write-Verbose "$logLead : Upgrading $($package.Tier) -> $($package.Id) $($package.Version)"
"Upgrading $($package.Tier) -> $($package.Id) $($package.Version)" | Out-Default -Transcript | Out-Null
}
} else {
# package is not installed locally
$package | Add-Member -NotePropertyName 'Tier' -NotePropertyValue $tierCount
$newPackagesToInstall += $package
Write-Verbose "$logLead : Installing $($package.Tier) -> $($package.Id) $($package.Version)"
}
}
$tierCount+=1
}
foreach ($package in $removePackages) {
Write-Host "Checking to see if $($package) is installed"
if ($localInstalledPackages -Contains $package) {
Write-Verbose "Yep it is showing it is installed"
$packagesToRemove += $package
}
}
return $packagesToUpgrade, $newPackagesToInstall, $packagesToRemove
}