Last active
November 1, 2024 04:28
-
-
Save scriptingstudio/cea6307e85acd3563c0123a61a70cae0 to your computer and use it in GitHub Desktop.
Windows version script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-WindowsVersion ([string]$Computername) { | |
| $params = @{} | |
| if ($computername) { | |
| [uint32]$HKLM = 2147483650 | |
| $winreg = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion' | |
| $params['CimSession'] = New-CimSession -computername $computername -sessionoption (new-cimsessionoption -protocol Dcom) | |
| if (-not $params['CimSession']) { | |
| $params['CimSession'] = New-CimSession -computername $computername -sessionoption (new-cimsessionoption -protocol wsman) | |
| } | |
| if (-not $params['CimSession']) {return} | |
| $os = Get-CimInstance win32_operatingsystem @params | |
| $params['CimClass'] = get-cimclass -classname 'StdRegProv' -Namespace 'root\cimv2' -ErrorAction 0 | |
| $stringValue = {param ($value) (invoke-cimmethod -MethodName GetStringValue -arguments @{hDefKey=$HKLM; sSubKeyName=$winreg; sValueName=$value} @params).sValue} | |
| $dwordValue = {param ($value) (invoke-cimmethod -MethodName GetdwordValue -arguments @{hDefKey=$HKLM; sSubKeyName=$winreg; sValueName=$value} @params).uValue} | |
| $qwordValue = {param ($value) (invoke-cimmethod -MethodName GetQwordValue -arguments @{hDefKey=$HKLM; sSubKeyName=$winreg; sValueName=$value} @params).uValue} | |
| $binaryValue = {param ($value) (invoke-cimmethod -MethodName GetBinaryValue -arguments @{hDefKey=$HKLM; sSubKeyName=$winreg; sValueName=$value} @params).uValue} | |
| $wininfo = [pscustomobject]@{ | |
| EditionID = . $stringValue EditionID | |
| ProductId = . $stringValue ProductId | |
| Lcuver = . $stringValue lcuver | |
| UBR = . $dwordValue UBR | |
| DisplayVersion = . $stringValue DisplayVersion | |
| BuildBranch = . $stringValue BuildBranch | |
| ReleaseID = . $stringValue ReleaseID | |
| InstallTime = . $qwordValue InstallTime | |
| DigitalProductId = . $binaryValue DigitalProductId | |
| } | |
| } else { | |
| $computername = $env:COMPUTERNAME | |
| $wininfo = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' | |
| $os = Get-CimInstance win32_operatingsystem @params | |
| } | |
| # decrypt base24 encoded binary data | |
| $binArray = $wininfo.DigitalProductId[52..66] | |
| $containsN = ($binArray[14] -shr 3) -bAnd 1 | |
| $binArray[14] = $binArray[14] -band 0xF7 | |
| $base24 = 'BCDFGHJKMPQRTVWXY2346789' | |
| $productKey = [System.Collections.Generic.List[string]]::new() | |
| for ($i = 24; $i -ge 0; $i--) { | |
| $k = 0 | |
| for ($j = 14; $j -ge 0; $j--) { | |
| $k = ($k -shl 8) -bXor $binArray[$j] | |
| $binArray[$j] = [System.Math]::DivRem($k, 24, [ref]$k) | |
| } | |
| $productKey.add($base24[$k]) | |
| } | |
| $productKey.Reverse() | |
| if ($containsN -eq 1) { | |
| $index = $base24.IndexOf($productKey[0]) | |
| $productKey.RemoveAt(0) | |
| $productKey.Insert($index, 'N') | |
| } | |
| $wininfo.DigitalProductId = (-join $productKey).trim() -replace '(.....(?!$))', '$1-' | |
| #$ppk = (Get-CimInstance -Query 'Select PartialProductKey From SoftwareLicensingProduct Where Name LIKE "Windows%" AND LicenseStatus>0' @params).PartialProductKey | |
| #$wininfo.DigitalProductId -match "$ppk$" | |
| [pscustomobject]@{ | |
| ProductName = $os.Caption | |
| EditionID = $wininfo.EditionID | |
| ProductId = $wininfo.ProductId | |
| Version = if ($wininfo.lcuver) {$wininfo.lcuver} else {'{0}.{1}' -f $os.version,$wininfo.UBR} | |
| DisplayVersion = $wininfo.DisplayVersion | |
| DigitalId = $wininfo.DigitalProductId | |
| #ReleaseID = $wininfo.ReleaseID | |
| Branch = $wininfo.BuildBranch | |
| Architecture = $os.OSArchitecture | |
| OSLanguage = $os.OSLanguage | |
| InstalledDate = ([datetime]"1/1/1601").AddTicks($wininfo.InstallTime) | |
| Computername = $computername.tolower() | |
| } | |
| if ($params['CimSession']) {Remove-CimSession $params['CimSession'] -ErrorAction 0} | |
| } # END Get-WindowsVersion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment