ps/Modules/Alkami.PowerShell.Choco/Public/Select-UniqueServerPackages.ps1
2023-05-30 22:51:22 -07:00

48 lines
1.6 KiB
PowerShell

function Select-UniqueServerPackages {
<#
.SYNOPSIS
Given a list of package lists (ie per server), this function returns the unique packages from each list of packages.
If there is a higher version of a package in one package list than another, the higher version is preferred.
.PARAMETER PackagesArray
Array of packages to process
#>
[CmdletBinding()]
param (
[array]$PackagesArray
)
if (Test-IsCollectionNullOrEmpty $PackagesArray) {
Write-Verbose "Array was null."
return $null
}
if ($PackagesArray.Count -eq 1) {
Write-Verbose "Only one value in array. Returning it."
return $PackagesArray[0]
}
# Determine the combined unique list of packages.
Write-Verbose "Looping through packages..."
$uniquePackages = @{ }
foreach ($packages in $PackagesArray) {
if ($null -eq $packages) {
continue
}
foreach ($package in $packages) {
$name = $package.Name.ToLower()
if (!($uniquePackages.ContainsKey($name))) {
# If this package is not in the unique package list, add it!
Write-Verbose "Found a package: $package"
$uniquePackages[$name] = $package
} elseif ($package.Version -gt $uniquePackages[$name].Version) {
# Otherwise if the version of the package on this server is greater than what is stored, prefer the higher version.
Write-Verbose "Found a package again! $package"
$uniquePackages[$name] = $package
}
}
}
Write-Verbose "Returning unique values."
return $uniquePackages.Values
}