Libraries/Lib.Windows.WinNT.Windows.WindowsServer/Lib.Windows.WinNT.Windows.WindowsServer.psm1

<#
.SYNOPSIS
Get the release ID of an OS
 
.DESCRIPTION
Windows Server (since Windows 10 / 2016) use the same ReleaseId as Windows Client.
But to differentiate Servers from Clients, we will use the old "year" number like "2008", "2012r2"
 
.PARAMETER Online
True to specify to fetch information from the running OS
 
.PARAMETER Root
The path to the root of an OS.
 
.EXAMPLE
Get-OSReleaseId -Online
 
.EXAMPLE
Get-OSReleaseId -Root F:\
 
.NOTES
General notes
#>


function Get-OSReleaseId {
    [CmdletBinding()][OutputType([String])]Param (
        [Parameter(ParameterSetName = 'ONLINE')][switch]$Online,
        [Parameter(ParameterSetName = 'ROOT')][string]$Root
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        $CurrentBuild = Get-OSKernelVersion @PSBoundParameters

        switch ($CurrentBuild) {
            # Windows Server 2000
            "2195"        { $releaseId = "2000" }
            # Windows Server 2003
            "3790"        { $releaseId = "2003" }
            "3790.1"    { $releaseId = "2003sp1" }
            "3790.2"    { $releaseId = "2003sp2" }
            # Windows Server 2008
            "6000"        { $releaseId = "2008" }
            "6001"        { $releaseId = "2008sp1" }
            "6002"        { $releaseId = "2008sp2" }
            # Windows Server 2008 R2
            "7600"         { $releaseId = "2008r2" }
            "7601"         { $releaseId = "2008r2sp1" }
            # Windows Server 2012
            "9200"         { $releaseId = "2012" }
            # Windows Server 2012 R2
            "9300"        { $releaseId = "2012r2" }
            "9600"        { $releaseId = "2012r2sp1" }
            # Windows Server 2016
            "14393"        { $releaseId = "2016" }
            # Windows Server 2019
            "17763"        { $releaseId = "2019" }
            # Windows Server 2022
            "20348"        { $releaseId = "2022" }
            # Windows Server 2025
            "26100"        { $releaseId = "2025" }

            Default { $releaseId = "Unknown Server Build" }
        }

        # return $Distrib + "." + $ReleaseId.replace('.','')
        return "$ReleaseId"
   }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

<#
.SYNOPSIS
Gets the short codename of the Windows Server operating system.
 
.DESCRIPTION
This function returns a codename in the form "WindowsServer<version>.<releaseID>",
e.g. "WindowsServer2022.21H2", based on the kernel build number.
 
.PARAMETER Online
Targets the currently running operating system.
 
.PARAMETER Root
Targets an offline image by specifying the root path to a mounted Windows installation.
 
.EXAMPLE
Get-OSCodename -Online
Returns the codename for the running OS, e.g. "WindowsServer2019.1809".
 
.EXAMPLE
Get-OSCodename -Root "X:\Mount"
Returns the codename for the mounted offline image.
 
.OUTPUTS
String. Returns the OS codename.
#>

function Get-OSCodename {
    [CmdletBinding()][OutputType([String])]
    Param (
        [Parameter(ParameterSetName = 'ONLINE')][switch]$Online,
        [Parameter(ParameterSetName = 'ROOT')][string]$Root
    )
    Begin { Write-PwShFwOSEnterFunction }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'ONLINE' {
                $CurrentBuild = Lib.Windows\Get-OSKernelVersion -Online
                break
            }
            'ROOT' {
                $CurrentBuild = Lib.Windows\Get-OSKernelVersion -Root $Root
                break
            }
        }
        $value = Get-OSWinServerCodenameFromCurrentBuild -CurrentBuild $CurrentBuild
        return $value
    }

    End { Write-PwShFwOSLeaveFunction }
}

<#
.SYNOPSIS
Returns the short codename for a Windows Server build.
 
.DESCRIPTION
Maps a Windows kernel build number to a short codename string
in the form "WindowsServer<version>.<releaseID>".
 
.PARAMETER CurrentBuild
The Windows build number.
 
.OUTPUTS
String. Codename of the OS version.
#>

function Get-OSWinServerCodenameFromCurrentBuild {
    param (
        [Parameter(Mandatory = $true)]
        [int]$CurrentBuild
    )

    switch ($CurrentBuild) {
        26080  { return "WindowsServer2025.24H2" }
        22621  { return "WindowsServer2022.22H2" }
        20348  { return "WindowsServer2022.21H2" }
        17763  { return "WindowsServer2019.1809" }
        14393  { return "WindowsServer2016.1607" }
        9600   { return "WindowsServer2012R2" }
        9200   { return "WindowsServer2012" }
        7601   { return "WindowsServer2008R2.SP1" }
        7600   { return "WindowsServer2008R2" }
        6002   { return "WindowsServer2008.SP2" }
        6001   { return "WindowsServer2008.SP1" }
        default {
            Write-Warning "Unknown build number: $CurrentBuild"
            return "WindowsServer?.?"
        }
    }
}

<#
.SYNOPSIS
Gets the long codename of the Windows Server operating system.
 
.DESCRIPTION
This function returns a human-readable name like
"Windows Server 2022 21H2", based on the kernel build number.
 
.PARAMETER Online
Targets the currently running operating system.
 
.PARAMETER Root
Targets an offline image by specifying the root path to a mounted Windows installation.
 
.EXAMPLE
Get-OSLongCodename -Online
Returns the long codename of the current OS.
 
.EXAMPLE
Get-OSLongCodename -Root "X:\Mount"
Returns the long codename of the mounted OS image.
 
.OUTPUTS
String. A human-readable OS version name.
#>

function Get-OSLongCodename {
    [CmdletBinding()][OutputType([String])]
    Param (
        [Parameter(ParameterSetName = 'ONLINE')][switch]$Online,
        [Parameter(ParameterSetName = 'ROOT')][string]$Root
    )
    Begin { Write-PwShFwOSEnterFunction }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'ONLINE' {
                $CurrentBuild = Lib.Windows\Get-OSKernelVersion -Online
                break
            }
            'ROOT' {
                $CurrentBuild = Lib.Windows\Get-OSKernelVersion -Root $Root
                break
            }
        }
        $value = Get-OSWinServerLongCodenameFromCurrentBuild -CurrentBuild $CurrentBuild
        return $value
    }

    End { Write-PwShFwOSLeaveFunction }
}

<#
.SYNOPSIS
Returns the long codename of a Windows Server build.
 
.DESCRIPTION
Maps a Windows kernel build number to a full OS name like
"Windows Server 2019 1809" or "Windows Server 2016 1607".
 
.PARAMETER CurrentBuild
The Windows build number.
 
.OUTPUTS
String. Human-readable OS name.
#>

function Get-OSWinServerLongCodenameFromCurrentBuild {
    param (
        [Parameter(Mandatory = $true)]
        [int]$CurrentBuild
    )

    switch ($CurrentBuild) {
        26080  { return "Windows Server 2025 24H2" }
        22621  { return "Windows Server 2022 22H2" }
        20348  { return "Windows Server 2022 21H2" }
        17763  { return "Windows Server 2019 1809" }
        14393  { return "Windows Server 2016 1607" }
        9600   { return "Windows Server 2012 R2" }
        9200   { return "Windows Server 2012" }
        7601   { return "Windows Server 2008 R2 SP1" }
        7600   { return "Windows Server 2008 R2" }
        6002   { return "Windows Server 2008 SP2" }
        6001   { return "Windows Server 2008 SP1" }
        default {
            Write-Warning "Unknown build number: $CurrentBuild"
            return "Windows Server ? ?"
        }
    }
}