ContainerHandling/Get-BestNavContainerImageName.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
 .Synopsis
  Get Best NAV/BC Container Image Name
 .Description
  If a Container Os platform name is not specified in the imageName, find the best container os and add it (if his is a microsoft image)
 .Parameter imageName
  Name of image
 .Example
  $imageName = Get-BestNavContainerImageName -imageName $imageName
#>

function Get-BestNavContainerImageName {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true, ValueFromPipeline)]
        [string] $imageName
    )

    if (!(
          $imagename.EndsWith('-ltsc2016') -or
          $imagename.EndsWith('-1709') -or
          $imagename.EndsWith('-1803') -or
          $imagename.EndsWith('-ltsc2019') -or
          $imagename.EndsWith('-1903') -or
          $imagename.EndsWith(':ltsc2016') -or
          $imagename.EndsWith(':1709') -or
          $imagename.EndsWith(':1803') -or
          $imagename.EndsWith(':ltsc2019') -or
          $imagename.EndsWith(':1903')
       )) {

        if ($imagename.StartsWith('microsoft/') -or 
            $imagename.StartsWith('bcprivate.azurecr.io/') -or 
            $imagename.StartsWith('bcinsider.azurecr.io/') -or 
            $imagename.StartsWith('mcr.microsoft.com/') -or 
            $imagename.StartsWith('mcrbusinesscentral.azurecr.io/')) {

            $os = (Get-CimInstance Win32_OperatingSystem)
            if ($os.OSType -eq 18 -and $os.Version.StartsWith("10.0.")) {
                $bestContainerOs = "ltsc2016"
                if ($os.BuildNumber -ge 17763) { 
                    $bestContainerOs = "ltsc2019"
                }

                if (!$imageName.Contains(':')) {
                    $imageName += ":$bestContainerOs"
                } else {
                    $imageName += "-$bestContainerOs"
                }
            }
        }
    }
    
    $imageName
}
Set-Alias -Name Get-BestBCContainerImageName -Value Get-BestNavContainerImageName
Export-ModuleMember -Function Get-BestNavContainerImageName -Alias Get-BestBCContainerImageName