ContainerHandling/Get-BestGenericImageName.ps1

<#
 .Synopsis
  Get Best Generic NAV/BC Container Image Name
 .Description
  If the best matching generic container name based on the Host Operating system. Returns blank if no generic matches the host OS.
 .Example
  $genericImageName = Get-BestGenericImageName
#>

function Get-BestGenericImageName {
    Param (
        [switch] $onlyMatchingBuilds,
        [Version] $hostOsVersion = $null
    )

    if ($hostOsVersion -eq $null) {
        $os = (Get-CimInstance Win32_OperatingSystem)
        if ($os.OSType -ne 18 -or !$os.Version.StartsWith("10.0.")) {
            throw "Unknown Host Operating System"
        }
    
        $UBR = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name UBR).UBR
        $hostOsVersion = [System.Version]::Parse("$($os.Version).$UBR")
    }
    else {
        $revision = $hostOsVersion.Revision
        if ($revision -eq -1) { $revision = [int32]::MaxValue }
        $build = $hostOsVersion.Build
        if ($build -eq -1) { $build = [int32]::MaxValue }
        $hostOsVersion = [System.Version]::new($hostOsVersion.Major, $hostOsVersion.Minor, $build, $revision)
    }

    $genericImageNameSetting = (Get-ContainerHelperConfig).genericImageName
    $repo = $genericImageNameSetting.Split(':')[0]
    $tag = $genericImageNameSetting.Split(':')[1].Replace('{0}','*')

    $imagetags = Get-NavContainerImageTags -imageName $repo
    $versions = @()
    if ($imagetags) {
        $ver = [Version]"0.0.0.0"
        $versions = $imagetags.tags | Where-Object { $_ -like $tag -and [System.Version]::TryParse($_.SubString($tag.indexOf('*'), $_.length-$tag.length+1), [ref]$ver) } | % { [System.Version]($_.SubString($tag.indexOf('*'), $_.length-$tag.length+1)) }
    }
    if (-not $versions) {

        # ImageTags not yet updated - use hardcoded list
        $versions = @(
        "10.0.14393.2906"
        "10.0.14393.2972"
        "10.0.14393.3025"
        "10.0.14393.3085"
        "10.0.14393.3144"
        "10.0.14393.3204"
        "10.0.14393.3326"
        "10.0.14393.3384"
        "10.0.14393.3443"
        "10.0.14393.3506"
        "10.0.14393.3630"
        "10.0.14393.3686"
        "10.0.14393.3750"
        "10.0.14393.3808"
        "10.0.17134.1006"
        "10.0.17134.1130"
        "10.0.17134.950"
        "10.0.17763.1040"
        "10.0.17763.1158"
        "10.0.17763.1217"
        "10.0.17763.1282"
        "10.0.17763.1339"
        "10.0.17763.437"
        "10.0.17763.504"
        "10.0.17763.557"
        "10.0.17763.615"
        "10.0.17763.678"
        "10.0.17763.737"
        "10.0.17763.864"
        "10.0.17763.914"
        "10.0.17763.973"
        "10.0.18362.116"
        "10.0.18362.175"
        "10.0.18362.239"
        "10.0.18362.295"
        "10.0.18362.356"
        "10.0.18362.476"
        "10.0.18362.535"
        "10.0.18362.592"
        "10.0.18362.658"
        "10.0.18362.720"
        "10.0.18362.778"
        "10.0.18362.836"
        "10.0.18362.900"
        "10.0.18362.959"
        "10.0.18363.476"
        "10.0.18363.535"
        "10.0.18363.592"
        "10.0.18363.658"
        "10.0.18363.720"
        "10.0.18363.778"
        "10.0.18363.836"
        "10.0.18363.900"
        "10.0.18363.959"
        "10.0.19041.264"
        "10.0.19041.329"
        "10.0.19041.388"
        ) | % { [System.Version]$_ } | Sort-Object
    }
    
    $genericImageName = ""
    $myversions = $versions | Where-Object { $_.Major -eq $hostOsVersion.Major -and $_.Minor -eq $hostOsVersion.Minor -and $_.Build -eq $hostOsVersion.Build } | Sort-Object
    if (-not $myversions) {
        if (-not $onlyMatchingBuilds) {
            $myversions = $versions | Sort-Object
        }
    }
    if ($myversions) {
        $version = $myversions | Where-Object { $_ -ge $hostOsVersion } | Select-Object -First 1
        if (-not $version) {
            $version = $myversions | Select-Object -Last 1
        }
        $genericImageName = [string]::format($genericImageNameSetting, $version.ToString())
    }
    $genericImageName
}
Export-ModuleMember -Function Get-BestGenericImageName