Skip to content

Instantly share code, notes, and snippets.

@p0w3rsh3ll
Last active January 11, 2022 13:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save p0w3rsh3ll/5b875b4c0c0bb66c663fc82f0d118fa1 to your computer and use it in GitHub Desktop.
Save p0w3rsh3ll/5b875b4c0c0bb66c663fc82f0d118fa1 to your computer and use it in GitHub Desktop.
Function Get-ServerInfo {
[CmdletBinding()]
Param()
Begin {
try {
$cv = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -ErrorAction 'Stop'
} catch {
Throw "Failed to read the registry because $($_.Exception.Message)"
}
}
Process {
try {
if ($cv.InstallationType -match 'Server') {
[PSCustomObject]@{
Branch = $(if($cv.ReleaseId){$cv.ReleaseId} else { $cv.CurrentVersion })
ProductName = $cv.ProductName
Build = $cv.CurrentBuild
Revision = $cv.UBR
Channel = $(if ($cv.ProductName -notmatch '\s\d{4}\s') { 'Semi-Annual Channel (SAC)' } else { 'Long Term Support (LTS)' })
isSAC = $(if ($cv.ProductName -match '\s\d{4}\s') { $false } else { $true })
isLTS = $(if ($cv.ProductName -notmatch '\s\d{4}\s') { $false } else { $true })
} |
Add-Member -MemberType ScriptProperty -Name isExpired -Value {
$y,$m = $this.Branch -split '(?<=\G.{2})'
if($this.isSAC -and ((Get-Date) -gt ((Get-Date -Year "20$($y)" -Month "$($m)").AddMonths(18)))) {
$true
} else {
$false
}
} -Force -PassThru -ErrorAction 'Stop'
}
} catch {
Throw "Failed to get server info because $($_.Exception.Message)"
}
}
End {}
<#
.SYNOPSIS
Get information about the server branch, channel, build,...
.DESCRIPTION
Get information about the server:
branch,
channel (SAC = Semi Annual Channel or LTS = Long Term Support)
build
revision,
product name
by reading the registry key
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
.EXAMPLE
Get-ServerInfo
Branch : 1809
ProductName : Windows Server 2019 Datacenter
Build : 17763
Revision : 437
Channel : Long Term Support (LTS)
isSAC : False
isLTS : True
isExpired : False
.EXAMPLE
Get-ServerInfo
Branch : 1709
ProductName : Windows Server Datacenter
Build : 16299
Revision : 1087
Channel : Semi-Annual Channel (SAC)
isSAC : True
isLTS : False
isExpired : True
.EXAMPLE
Get-ServerInfo
Branch : 1903
ProductName : Windows Server Standard
Build : 18362
Revision : 113
Channel : Semi-Annual Channel (SAC)
isSAC : True
isLTS : False
isExpired : False
.EXAMPLE
Get-ServerInfo
Branch : 6.3
ProductName : Windows Server 2012 R2 Standard
Build : 9600
Revision : 19356
Channel : Long Term Support (LTS)
isSAC : False
isLTS : True
isExpired : False
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment