Skip to content

Commit

Permalink
Switch to registry based verification (#252)
Browse files Browse the repository at this point in the history
* Switch to registry based verification

Except for Server 2008 which doesn't use registry to store status, this will resolve issues with attempting to activate windows when English is not the system display language.

* Consolidate to a single guaranteed return

More optimal given a single guaranteed return

* Update catch behavior for failed license lookup

Log the message rather than set $active which is already false
  • Loading branch information
jjerger authored Aug 23, 2024
1 parent c514f7d commit aab2d0f
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions sysprep/activate_instance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,33 @@ function Verify-ActivationStatus {
[String]$activation_status = $null
[String]$status = $null

try {
$slmgr_status = & cscript //E:VBScript //nologo $env:windir\system32\slmgr.vbs /dli
}
catch {
return $active
# Server 2008 doesn't store activation status in registry; check slmgr
if([Environment]::OSVersion.Version.Major -eq 6 -and [Environment]::OSVersion.Version.Minor -le 1)
{
try {
$slmgr_status = & cscript //E:VBScript //nologo $env:windir\system32\slmgr.vbs /dli
}
catch {
Write-Host "Error getting slmgr license status output."
}
$status = $slmgr_status | Select-String -Pattern '^License Status:'
# The initial space is to ensure "Unlicensed" does not match.
if ($status -match ' Licensed') {
$active = $true
}
}

$status = $slmgr_status | Select-String -Pattern '^License Status:'
# The initial space is to ensure "Unlicensed" does not match.
if ($status -match ' Licensed') {
$active = $true
# All server versions newer than 2008
else {
try {
$activation_status = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform\Activation" -Name ProductActivationResult).ProductActivationResult
}
catch {
Write-Host "Error retrieving last activation result registry key."
}
# Anything other than 0x0 is a failure.
if ($activation_status -eq '0') {
$active = $true
}
}
return $active
}
Expand Down

0 comments on commit aab2d0f

Please sign in to comment.