diff --git a/sysprep/activate_instance.ps1 b/sysprep/activate_instance.ps1 index 6cbbb6c0..6797f9f5 100644 --- a/sysprep/activate_instance.ps1 +++ b/sysprep/activate_instance.ps1 @@ -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 }