Windows License Management Script
Windows License Management Script
SLMGR - REARM
g_ShowKmsInfo
g_ShowKmsClientInfo
g_ShowTkaClientInfo
g_ShowTBLInfo
g_ShowPhoneInfo
g_ShowKmsInfo = False
g_ShowKmsClientInfo = false
g_ShowTBLInfo = False
g_ShowPhoneInfo = False
' Messages
'Global options
private const L_optInstallProductKey
private const L_optInstallProductKeyUsage
places existing key)"
= "ipk"
= "Install product key (re
= "upk"
= "Uninstall product key"
= "ato"
= "Activate Windows"
= "dli"
= "Display license informa
= "dlv"
= "Display detailed licens
= "xpr"
= "Expiration date for cur
'Advanced options
private const L_optClearPKeyFromRegistry
private const L_optClearPKeyFromRegistryUsage
the registry (prevents disclosure attacks)"
= "cpky"
= "Clear product key from
= "ilc"
= "Install license"
= "rilc"
= "Re-install system licen
= "dti"
= "Display Installation ID
= "atp"
= "Activate product with u
= "rearm"
= "Reset the licensing sta
'KMS options
private const L_optSetKmsName
= "skms"
private const L_optSetKmsNameUsage
= "Set the name and/or the
port for the KMS computer this machine will use. IPv6 address must be specified
in the format [hostname]:port"
private const L_optClearKmsName
private const L_optClearKmsNameUsage
ter used (sets the port to the default)"
= "ckms"
= "Clear name of KMS compu
= "skhc"
= "Enable KMS host caching
= "ckhc"
= "Disable KMS host cachin
= "sprt"
= "Set TCP port KMS will u
= "sdns"
= "Enable DNS publishing b
= "cdns"
= "Disable DNS publishing
= "spri"
= "Set KMS priority to nor
= "cpri"
= "Set KMS priority to low
= "lil"
= "List installed Token-ba
= "ril"
= "Remove installed Token-
= "ctao"
= "Clear Token-based Activ
= "stao"
= "Set Token-based Activat
= "ltc"
= "List Token-based Activa
= "fta"
= "Force Token-based Activ
=
=
=
=
=
=
=
"<Activation ID>"
"[Activation ID]"
"[Activation ID | All]"
"<Product Key>"
"<License file>"
"<Confirmation ID>"
"<Name[:Port] | : port>
= "<Port>"
= "<Activation Interval>"
= "<Renewal Interval>"
= "<ILID> <ILvID>"
= "<Certificate Thumbprint
expire %ENDDATE%"
private const L_MsgLicenseStatusTBL
ll expire %ENDDATE%"
private const L_MsgLicenseStatusLicensed
tly activated."
private const L_MsgLicenseStatusInitialGrace
ds %ENDDATE%"
private const L_MsgLicenseStatusAdditionalGrace
ends %ENDDATE%"
private const L_MsgLicenseStatusNonGenuineGrace
d ends %ENDDATE%"
private const L_MsgLicenseStatusNotification
ion mode"
private const L_MsgLicenseStatusExtendedGrace
nds %ENDDATE%"
= "Timebased activation wi
= "The machine is permanen
= "Initial grace period en
= "Additional grace period
= "Non-genuine grace perio
= "Windows is in Notificat
= "Extended grace period e
= "Notification Reason: 0x
= "Notification Reason: 0x
= "Notification Reason: 0x
= "Time remaining: %MINUTE
= "License Status: Unknown
= "Evaluation End Date: "
= "Re-installing license f
= "License files re-instal
= "Software licensing serv
=
=
=
=
=
=
"Name: "
"Description: "
"Activation ID: "
"Application ID: "
"Extended PID: "
"Processor Certificate U
= "Key Managemen
= "Total request
= "Failed reques
= "Requests with
= "Requests with
= "Requests with
= "Requests with
= "Requests with
= "Requests with
= "Token-based Activation
= "%ILID%
%ILVID%"
= "License ID (ILID): %ILI
= "Version ID (ILvID): %IL
= "Valid to: %TODATE%"
= "Additional Information:
= "Error: 0x%ERRCODE%"
= "Description: %DESC%"
= "No licenses found."
= "Removing Token-based Ac
= "Additional Information:
private
%"
private
private
private
private
const L_MsgTkaCertThumbprint
= "Thumbprint: %THUMBPRINT
const
const
const
const
=
=
=
=
L_MsgTkaCertSubject
L_MsgTkaCertIssuer
L_MsgTkaCertValidFrom
L_MsgTkaCertValidTo
= "Token-based Activation
= "License ID (ILID): %ILI
= "Version ID (ILvID): %IL
= "Grant Number: %GRANTNO%
= "Certificate Thumbprint:
"Subject: %SUBJECT%"
"Issuer: %ISSUER%"
"Valid from: %FROMDATE%"
"Valid to: %TODATE%"
private
private
private
y"
private
"
const NoPrimaryKeyFound
const TblPrimaryKey
const NotSpecialCasePrimaryKey
= "NoPrimaryKeyFound"
= "TblPrimaryKey"
= "NotSpecialCasePrimaryKe
const IndeterminatePrimaryKeyFound
= "IndeterminatePrimaryKey
= &H80000002
= &H80000003
= "1688"
= 0
= 1
= "SOFTWARE\Microsoft\Wind
const
const
const
const
HR_S_OK
HR_ERROR_FILE_NOT_FOUND
HR_SL_E_GRACE_TIME_EXPIRED
HR_SL_E_NOT_GENUINE
=
=
=
=
0
&H80070002
&HC004F009
&HC004F200
= "SoftwareLicensingServic
= "SoftwareLicensingProduc
= "SoftwareLicensingTokenA
= "55c92734-d682-4d71-983e
= "PartialProductKey <> nu
= 3
= 6
= ""
Call ExecCommandLine()
ExitScript 0
Private Sub DisplayUsage ()
LineOut GetResource("L_MsgHelp_1")
LineOut GetResource("L_MsgHelp_2")
LineOut "
" & GetResource("L_MsgHelp_3")
LineOut "
" & GetResource("L_MsgHelp_4")
LineOut "
" & GetResource("L_MsgHelp_5")
LineOut ""
LineOut GetResource("L_MsgGlobalOptions")
OptLine GetResource("L_optInstallProductKey"),
GetResource("L_Params
ProductKey"),
GetResource("L_optInstallProductKeyUsage")
OptLine GetResource("L_optActivateProduct"),
GetResource("L_Params
ActivationIDOptional"), GetResource("L_optActivateProductUsage")
OptLine GetResource("L_optDisplayInformation"),
GetResource("L_Params
ActIDOptional"),
GetResource("L_optDisplayInformationUsage")
OptLine GetResource("L_optDisplayInformationVerbose"), GetResource("L_Params
ActIDOptional"),
GetResource("L_optDisplayInformationUsageVerbose")
OptLine GetResource("L_optExpirationDatime"),
GetResource("L_Params
ActivationIDOptional"), GetResource("L_optExpirationDatimeUsage")
LineFlush ""
LineOut GetResource("L_MsgAdvancedOptions")
OptLine GetResource("L_optClearPKeyFromRegistry"),
"",
GetResource("L_optClearPKeyFromRegistryUsage")
OptLine GetResource("L_optInstallLicense"),
GetResource("L_Params
LicenseFile"),
GetResource("L_optInstallLicenseUsage")
OptLine GetResource("L_optReinstallLicenses"),
"",
GetResource("L_optReinstallLicensesUsage")
OptLine GetResource("L_optReArmWindows"),
"",
GetResource("L_optReArmWindowsUsage")
OptLine GetResource("L_optUninstallProductKey"),
GetResource("L_Params
ActivationIDOptional"), GetResource("L_optUninstallProductKeyUsage")
LineOut ""
OptLine GetResource("L_optDisplayIID"),
GetResource("L_ParamsActi
vationIDOptional"), GetResource("L_optDisplayIIDUsage")
OptLine2 GetResource("L_optPhoneActivateProduct"), GetResource("L_ParamsPhon
eActivate"),
GetResource("L_ParamsActivationIDOptional"), GetResource(
"L_optPhoneActivateProductUsage")
LineOut ""
LineOut GetResource("L_MsgKmsClientOptions")
OptLine2 GetResource("L_optSetKmsName"),
GetResource("L_ParamsSetK
ms"),
GetResource("L_ParamsActivationIDOptional"), GetResource(
"L_optSetKmsNameUsage")
OptLine GetResource("L_optClearKmsName"),
GetResource("L_ParamsActi
vationIDOptional"), GetResource("L_optClearKmsNameUsage")
OptLine GetResource("L_optSetKmsHostCaching"),
"",
GetResource("L_optSetKmsHostCachingUsage")
OptLine GetResource("L_optClearKmsHostCaching"), "",
GetResource("L_optClearKmsHostCachingUsage")
LineFlush ""
LineOut GetResource("L_MsgTkaClientOptions")
OptLine GetResource("L_optListInstalledILs"),
"",
GetResource("L_optListInstalledILsUsage")
OptLine GetResource("L_optRemoveInstalledIL"),
GetResource("L_ParamsRemo
veInstalledIL"),
GetResource("L_optRemoveInstalledILUsage")
OptLine GetResource("L_optClearTkaOnly"),
"",
GetResource("L_optClearTkaOnlyUsage")
OptLine GetResource("L_optSetTkaOnly"),
"",
GetResource("L_optSetTkaOnlyUsage")
OptLine GetResource("L_optListTkaCerts"),
"",
GetResource("L_optListTkaCertsUsage")
OptLine GetResource("L_optForceTkaActivation"),
GetResource("L_ParamsForc
eTkaActivation"),
GetResource("L_optForceTkaActivationUsage")
LineFlush ""
LineOut GetResource("L_MsgKmsOptions")
OptLine GetResource("L_optSetKmsListenPort"),
GetResource("L_ParamsSetL
istenKmsPort"),
GetResource("L_optSetKmsListenPortUsage")
OptLine GetResource("L_optSetActivationInterval"), GetResource("L_ParamsSetA
ctivationInterval"), GetResource("L_optSetActivationIntervalUsage")
OptLine GetResource("L_optSetRenewalInterval"),
GetResource("L_ParamsSetR
enewalInterval"),
GetResource("L_optSetRenewalIntervalUsage")
OptLine GetResource("L_optSetDNS"),
"",
GetResource("L_optSetDNSUsage")
OptLine GetResource("L_optClearDNS"),
"",
GetResource("L_optClearDNSUsage")
OptLine GetResource("L_optSetNormalPriority"),
"",
GetResource("L_optSetNormalPriorityUsage")
OptLine GetResource("L_optClearNormalPriority"), "",
GetResource("L_optClearNormalPriorityUsage")
ExitScript 1
End Sub
Private Sub OptLine(strOption, strParams, strUsage)
LineOut "/" & strOption & " " & strParams
LineOut "
" & strUsage
End Sub
Private Sub OptLine2(strOption, strParam1, strParam2, strUsage)
LineOut "/" & strOption & " " & strParam1 & " " & strParam2
LineOut "
" & strUsage
End Sub
Private
Dim
Dim
Dim
Sub ExecCommandLine
intOption, indexOption
strOption, chOpt
remoteInfo(3)
'
' First three parameters before "/" or "-" may be remote connection info
'
remoteInfo(0) = "."
intOption = intUnknownOption
For indexOption = 0 To 3
If indexOption >= WScript.Arguments.Count Then
Exit For
End If
strOption = WScript.Arguments.Item(indexOption)
chOpt = Left(strOption, 1)
If chOpt = "/" Or chOpt = "-" Then
intOption = intKnownOption
Exit For
End If
remoteInfo(indexOption) = strOption
Next
'
' Connect to remote only if syntax is reasonably good
'
If intUnknownOption = intOption Or 2 = indexOption Then
g_strComputer = "."
intOption = intUnknownOption
Else
g_strComputer = remoteInfo(0)
g_strUserName = remoteInfo(1)
g_strPassword = remoteInfo(2)
End If
If Not booleanConnect() Then
ExitScript 1
End If
If intUnknownOption = intOption Then
LineOut GetResource("L_MsgInvalidOptions")
LineOut ""
Call DisplayUsage()
End If
intOption = ParseCommandLine(indexOption)
If intUnknownOption = intOption Then
LineOut GetResource("L_MsgUnrecognizedOption") & WScript.Arguments.Item(
indexOption)
LineOut ""
Call DisplayUsage()
End If
End Sub
Private Function ParseCommandLine(index)
Dim strOption, chOpt
ParseCommandLine = intKnownOption
strOption = LCase(WScript.Arguments.Item(index))
chOpt = Left(strOption, 1)
If (chOpt <> "-") And (chOpt <> "/") Then
ParseCommandLine = intUnknownOption
Exit Function
End If
strOption = Right(strOption, Len(strOption) - 1)
If strOption = GetResource("L_optInstallLicense") Then
If HandleOptionParam(index+1, True, GetResource("L_optInstallLicense"),
GetResource("L_ParamsLicenseFile")) Then
InstallLicense WScript.Arguments.Item(index+1)
End If
ElseIf strOption = GetResource("L_optInstallProductKey") Then
If HandleOptionParam(index+1, True, GetResource("L_optInstallProductKey"
), GetResource("L_ParamsProductKey")) Then
InstallProductKey WScript.Arguments.Item(index+1)
End If
ElseIf strOption = GetResource("L_optUninstallProductKey") Then
If HandleOptionParam(index+1, False, GetResource("L_optUninstallProductK
ey"), GetResource("L_ParamsActivationIDOptional")) Then
UninstallProductKey WScript.Arguments.Item(index+1)
Else
UninstallProductKey ""
End If
ElseIf strOption = GetResource("L_optDisplayIID") Then
If HandleOptionParam(index+1, False, GetResource("L_optDisplayIID"), Get
Resource("L_ParamsActivationIDOptional")) Then
DisplayIID WScript.Arguments.Item(index+1)
Else
DisplayIID ""
End If
ElseIf strOption = GetResource("L_optActivateProduct") Then
If HandleOptionParam(index+1, False, GetResource("L_optActivateProduct")
, GetResource("L_ParamsActivationIDOptional")) Then
ActivateProduct WScript.Arguments.Item(index+1)
Else
ActivateProduct ""
End If
ElseIf strOption = GetResource("L_optPhoneActivateProduct") Then
If HandleOptionParam(index+1, True, GetResource("L_optPhoneActivateProdu
ct"), GetResource("L_ParamsPhoneActivate")) Then
If HandleOptionParam(index+2, False, GetResource("L_optPhoneActivate
Product"), GetResource("L_ParamsActivationIDOptional")) Then
PhoneActivateProduct WScript.Arguments.Item(index+1), WScript.Ar
guments.Item(index+2)
Else
PhoneActivateProduct WScript.Arguments.Item(index+1), ""
End If
End If
ElseIf strOption = GetResource("L_optDisplayInformation") Then
If HandleOptionParam(index+1, False, GetResource("L_optDisplayInformatio
n"), "") Then
DisplayAllInformation WScript.Arguments.Item(index+1), False
Else
DisplayAllInformation "", False
End If
ElseIf strOption = GetResource("L_optDisplayInformationVerbose") Then
If HandleOptionParam(index+1, False, GetResource("L_optDisplayInformatio
nVerbose"), "") Then
DisplayAllInformation WScript.Arguments.Item(index+1), True
Else
DisplayAllInformation "", True
End If
ElseIf strOption = GetResource("L_optClearPKeyFromRegistry") Then
ClearPKeyFromRegistry
ElseIf strOption = GetResource("L_optReinstallLicenses") Then
ReinstallLicenses
ElseIf strOption = GetResource("L_optReArmWindows") Then
ReArmWindows()
ElseIf strOption = GetResource("L_optExpirationDatime") Then
If HandleOptionParam(index+1, False, GetResource("L_optExpirationDatime"
), GetResource("L_ParamsActivationIDOptional")) Then
ExpirationDatime WScript.Arguments.Item(index+1)
Else
ExpirationDatime ""
End If
ElseIf strOption = GetResource("L_optSetKmsName") Then
If HandleOptionParam(index+1, True, GetResource("L_optSetKmsName"), GetR
esource("L_ParamsSetKms")) Then
If HandleOptionParam(index+2, False, GetResource("L_optSetKmsName"),
GetResource("L_ParamsActivationIDOptional")) Then
SetKmsMachineName WScript.Arguments.Item(index+1), WScript.Argum
ents.Item(index+2)
Else
SetKmsMachineName WScript.Arguments.Item(index+1), ""
End If
End If
ElseIf strOption = GetResource("L_optClearKmsName") Then
If HandleOptionParam(index+1, False, GetResource("L_optClearKmsName"), G
etResource("L_ParamsActivationIDOptional")) Then
ClearKms WScript.Arguments.Item(index+1)
Else
ClearKms ""
End If
ElseIf strOption = GetResource("L_optSetKmsHostCaching") Then
SetHostCachingDisable(False)
ElseIf strOption = GetResource("L_optClearKmsHostCaching") Then
SetHostCachingDisable(True)
ElseIf strOption = GetResource("L_optSetActivationInterval") Then
If HandleOptionParam(index+1, True, GetResource("L_optSetActivationInter
val"), GetResource("L_ParamsSetActivationInterval")) Then
SetActivationInterval WScript.Arguments.Item(index+1)
End If
ElseIf strOption = GetResource("L_optSetRenewalInterval") Then
If HandleOptionParam(index+1, True, GetResource("L_optSetRenewalInterval
"), GetResource("L_ParamsSetRenewalInterval")) Then
SetRenewalInterval WScript.Arguments.Item(index+1)
End If
ElseIf strOption = GetResource("L_optSetKmsListenPort") Then
If HandleOptionParam(index+1, True, GetResource("L_optSetKmsListenPort")
, GetResource("L_ParamsSetListenKmsPort")) Then
SetKmsListenPort WScript.Arguments.Item(index+1)
End If
ElseIf strOption = GetResource("L_optSetDNS") Then
SetDnsPublishingDisabled(False)
ElseIf strOption = GetResource("L_optClearDNS") Then
SetDnsPublishingDisabled(True)
ElseIf strOption = GetResource("L_optSetNormalPriority") Then
SetKmsLowPriority(False)
ElseIf strOption = GetResource("L_optClearNormalPriority") Then
SetKmsLowPriority(True)
ElseIf strOption = GetResource("L_optListInstalledILs") Then
TkaListILs
ElseIf strOption = GetResource("L_optRemoveInstalledIL") Then
If HandleOptionParam(index+2, True, GetResource("L_optRemoveInstalledIL"
), GetResource("L_ParamsRemoveInstalledIL")) Then
TkaRemoveIL WScript.Arguments.Item(index+1), WScript.Arguments.Item(
index+2)
End If
ElseIf strOption = GetResource("L_optClearTkaOnly") Then
TkaSetTokenActivationOnly False
ElseIf strOption = GetResource("L_optSetTkaOnly") Then
TkaSetTokenActivationOnly True
ElseIf strOption = GetResource("L_optListTkaCerts") Then
TkaListCerts
ElseIf strOption = GetResource("L_optForceTkaActivation") Then
If HandleOptionParam(index+2, False, GetResource("L_optForceTkaActivatio
n"), GetResource("L_ParamsForceTkaActivation")) Then
TkaActivate WScript.Arguments.Item(index+1), WScript.Arguments.Item(
index+2)
ElseIf HandleOptionParam(index+1, True, GetResource("L_optForceTkaActiva
tion"), GetResource("L_ParamsForceTkaActivation")) Then
TkaActivate WScript.Arguments.Item(index+1), ""
End If
Else
ParseCommandLine = intUnknownOption
End If
End Function
' global options
Private Function CheckProductForCommand(objProduct, strActivationID)
Dim bCheckProductForCommand
bCheckProductForCommand = False
Sub UninstallProductKey(strActivationID)
objService, objProduct
lRet, strVersion, strDescription
kmsServerFound, uninstallDone
iIsPrimaryWindowsSku, bPrimaryWindowsSkuKeyUninstalled
bCheckProductForCommand
Sub DisplayIID(strActivationID)
objProduct
iIsPrimaryWindowsSku, bFoundAtLeastOneKey
bCheckProductForCommand
strActivationID = LCase(strActivationID)
bFoundAtLeastOneKey = False
For Each objProduct in GetProductCollection(ProductIsPrimarySkuSelectClause
& ", OfflineInstallationId", PartialProductKeyNonNullWhereClause)
bCheckProductForCommand = CheckProductForCommand(objProduct, strActivati
onID)
If (bCheckProductForCommand) Then
iIsPrimaryWindowsSku = GetIsPrimaryWindowsSKU(objProduct)
Sub ActivateProduct(strActivationID)
objService, objProduct
iIsPrimaryWindowsSku, bFoundAtLeastOneKey
strOutput
bCheckProductForCommand
strActivationID = LCase(strActivationID)
bFoundAtLeastOneKey = False
set objService = GetServiceObject("Version")
strActivationID = LCase(strActivationID)
bFoundAtLeastOneKey = False
set objService = GetServiceObject("Version")
For Each objProduct in GetProductCollection(ProductIsPrimarySkuSelectClause
& ", OfflineInstallationId, LicenseStatus, LicenseStatusReason", PartialProductK
eyNonNullWhereClause)
bCheckProductForCommand = CheckProductForCommand(objProduct, strActivati
onID)
If (bCheckProductForCommand) Then
iIsPrimaryWindowsSku = GetIsPrimaryWindowsSKU(objProduct)
If (strActivationID = "") And (iIsPrimaryWindowsSku = 2) Then
OutputIndeterminateOperationWarning(objProduct)
End If
On Error Resume Next
objProduct.DepositOfflineConfirmationId objProduct.OfflineInstallati
onId, strCID
QuitIfError()
objService.RefreshLicenseStatus()
objProduct.refresh_
If (objProduct.LicenseStatus = 1) Then
strOutput = Replace(GetResource("L_MsgConfID"), "%ACTID%", objPr
oduct.ID)
LineOut strOutput
ElseIf (objProduct.LicenseStatus = 4) Then
LineOut GetResource("L_MsgErrorText_8") & GetResource("L_MsgErro
rText_11")
ElseIf ((objProduct.LicenseStatus = 5) And (objProduct.LicenseStatus
Reason = HR_SL_E_NOT_GENUINE)) Then
LineOut GetResource("L_MsgErrorText_8") & GetResource("L_Msg
ErrorText_12")
ElseIf (objProduct.LicenseStatus = 6) Then
LineOut GetResource("L_MsgActivated")
LineOut GetResource("L_MsgLicenseStatusExtendedGrace_1")
Else
LineOut GetResource("L_MsgActivated_Failed")
End If
bFoundAtLeastOneKey = True
If (strActivationID <> "") Or (iIsPrimaryWindowsSku = 1) Then
Exit Sub
End If
End If
Next
If (bFoundAtLeastOneKey = True) Then
Exit Sub
End If
LineOut GetResource("L_MsgErrorProductNotFound")
End Sub
Private
Dim
Dim
Dim
Dim objProductKMSValues
set objProductKMSValues = GetProductObject( _
"IsKeyManagementServiceMachine, KeyManagementServiceCurrentCount, " & _
"KeyManagementServiceTotalRequests, KeyManagementServiceFailedRequests,
" & _
"KeyManagementServiceUnlicensedRequests, KeyManagementServiceLicensedReq
uests, " & _
"KeyManagementServiceOOBGraceRequests, KeyManagementServiceOOTGraceReque
sts, " & _
"KeyManagementServiceNonGenuineGraceRequests, KeyManagementServiceNotifi
cationRequests", _
"id = '" & objProduct.ID & "'")
If objProductKMSValues.IsKeyManagementServiceMachine > 0 Then
LineOut ""
LineOut GetResource("L_MsgKmsEnabled")
LineOut "
" & GetResource("L_MsgKmsCurrentCount") & objProductKMSValu
es.KeyManagementServiceCurrentCount
dwValue = objService.KeyManagementServiceListeningPort
If 0 = dwValue Then
LineOut "
" & GetResource("L_MsgKmsListeningOnPort") & DefaultPor
t
Else
LineOut "
End If
boolValue = objService.KeyManagementServiceDnsPublishing
If true = boolValue Then
LineOut "
" & GetResource("L_MsgKmsDnsPublishingEnabled")
Else
LineOut "
" & GetResource("L_MsgKmsDnsPublishingDisabled")
End If
boolValue = objService.KeyManagementServiceLowPriority
If false = boolValue Then
LineOut "
" & GetResource("L_MsgKmsPriNormal")
Else
LineOut "
" & GetResource("L_MsgKmsPriLow")
End If
On Error Resume Next
KeyManagementServiceTotalRequests = objProductKMSValues.KeyManagementSer
viceTotalRequests
If (Not(IsNull(KeyManagementServiceTotalRequests))) And (Not(IsEmpty(Key
ManagementServiceTotalRequests))) Then
LineOut ""
LineOut GetResource("L_MsgKmsCumulativeRequestsFromClients")
LineOut "
" & GetResource("L_MsgKmsTotalRequestsRecieved") & objP
roductKMSValues.KeyManagementServiceTotalRequests
LineOut "
" & GetResource("L_MsgKmsFailedRequestsReceived") & obj
ProductKMSValues.KeyManagementServiceFailedRequests
LineOut "
" & GetResource("L_MsgKmsRequestsWithStatusUnlicensed")
& objProductKMSValues.KeyManagementServiceUnlicensedRequests
LineOut "
" & GetResource("L_MsgKmsRequestsWithStatusLicensed") &
objProductKMSValues.KeyManagementServiceLicensedRequests
LineOut "
" & GetResource("L_MsgKmsRequestsWithStatusInitialGrace
") & objProductKMSValues.KeyManagementServiceOOBGraceRequests
LineOut "
" & GetResource("L_MsgKmsRequestsWithStatusLicenseExpir
edOrHwidOot") & objProductKMSValues.KeyManagementServiceOOTGraceRequests
LineOut "
" & GetResource("L_MsgKmsRequestsWithStatusNonGenuineGr
ace") & objProductKMSValues.KeyManagementServiceNonGenuineGraceRequests
LineOut "
" & GetResource("L_MsgKmsRequestsWithStatusNotification
") & objProductKMSValues.KeyManagementServiceNotificationRequests
End If
End If
End Sub
Private Sub DisplayTkaClientInformation(objService, objProduct)
LineOut ""
LineOut GetResource("L_MsgTkaInfo")
LineOut "
" & Replace(GetResource("L_MsgTkaInfoILID"
),
, objProduct.TokenActivationILID)
LineOut "
" & Replace(GetResource("L_MsgTkaInfoILVID"
),
, objProduct.TokenActivationILVID)
LineOut "
" & Replace(GetResource("L_MsgTkaInfoGrantNo" ),
, objProduct.TokenActivationGrantNumber)
LineOut "
" & Replace(GetResource("L_MsgTkaInfoThumbprint"),
%", objProduct.TokenActivationCertificateThumbprint)
End Sub
"%ILID%"
"%ILVID%"
"%GRANTNO%"
"%THUMBPRINT
Else
LineOut "
Port
End If
End If
LineOut "
" & GetResource("L_MsgKmsPID4") & objProduct.KeyManagementServi
ceProductKeyID
strOutput = Replace(GetResource("L_MsgActivationInterval"), "%INTERVAL%", iV
LActivationInterval)
LineOut "
" & strOutput
strOutput = Replace(GetResource("L_MsgRenewalInterval"), "%INTERVAL%", iVLRe
newalInterval)
LineOut "
" & strOutput
if (objService.KeyManagementServiceHostCaching = True) Then
LineOut "
" & GetResource("L_MsgKmsHostCachingEnabled")
Else
LineOut "
" & GetResource("L_MsgKmsHostCachingDisabled")
End If
End Sub
'
'
'
ActID = parm or
default to current ActID (parm = "" and IsPrimaryWindowsSKU is 1 or
2)
iIsPrimaryWindowsSku = GetIsPrimaryWindowsSKU(objProduct)
bUseDefault = False
bShowSkuInformation = False
If (strParm = "" And ((iIsPrimaryWindowsSku = 1) Or (iIsPrimaryWindowsSk
u = 2))) Then
bUseDefault = True
bShowSkuInformation = True
End If
If (strParm = "" And (objProduct.LicenseIsAddon And objProduct.PartialPr
oductKey <> "")) Then
bShowSkuInformation = True
End If
If (strParm = "all") Then
bShowSkuInformation = True
End If
If (strParm = LCase(strSLActID)) Then
bShowSkuInformation = True
End If
If (bShowSkuInformation) Then
strDescription = objProduct.Description
'If the user didn't specify anything and we are showing the default
case, warn them
' if this can't be verified as the primary SKU
If ((bUseDefault = True) And (iIsPrimaryWindowsSku = 2)) Then
OutputIndeterminateOperationWarning(objProduct)
End IF
productKeyFound = True
LineOut ""
LineOut GetResource("L_MsgProductName") & objProduct.Name
LineOut GetResource("L_MsgProductDesc") & strDescription
If objProduct.TokenActivationAdditionalInfo <> "" Then
LineOut Replace( _
GetResource("L_MsgTkaInfoAdditionalInfo"), _
"%MOREINFO%", _
objProduct.TokenActivationAdditionalInfo _
)
End If
bKmsServer = IsKmsServer(strDescription)
bKmsClient = IsKmsClient(strDescription)
bTBL
= IsTBL(strDescription)
If bVerbose
LineOut
LineOut
LineOut
Then
GetResource("L_MsgActID") & strSLActID
GetResource("L_MsgAppID") & objProduct.ApplicationID
GetResource("L_MsgPID4") & objProduct.ProductKeyID
ElseIf ls = 4 Then
LineOut GetResource("L_MsgLicenseStatusNonGenuineGrace_1")
gpMin = objProduct.GracePeriodRemaining
gpDay = Int(gpMin / (24 * 60))
strOutput = Replace(GetResource("L_MsgLicenseStatusTimeRemaining
"), "%MINUTE%", gpMin)
strOutput = Replace(strOutput, "%DAY%", gpDay)
LineOut strOutput
ElseIf ls = 5 Then
LineOut GetResource("L_MsgLicenseStatusNotification_1")
strErr = CStr(Hex(objProduct.LicenseStatusReason))
if (objProduct.LicenseStatusReason = HR_SL_E_NOT_GENUINE) Then
strOutput = Replace(GetResource("L_MsgNotificationErrorReason
NonGenuine"), "%ERRCODE%", strErr)
ElseIf (objProduct.LicenseStatusReason = HR_SL_E_GRACE_TIME_EXPI
RED) Then
strOutput = Replace(GetResource("L_MsgNotificationErrorReaso
nExpiration"), "%ERRCODE%", strErr)
Else
strOutput = Replace(GetResource("L_MsgNotificationErrorReaso
nOther"), "%ERRCODE%", strErr)
End If
LineOut strOutput
ElseIf ls = 6 Then
LineOut GetResource("L_MsgLicenseStatusExtendedGrace_1")
gpMin = objProduct.GracePeriodRemaining
gpDay = Int(gpMin / (24 * 60))
strOutput = Replace(GetResource("L_MsgLicenseStatusTimeRemaining
"), "%MINUTE%", gpMin)
strOutput = Replace(strOutput, "%DAY%", gpDay)
LineOut strOutput
Else
LineOut GetResource("L_MsgLicenseStatusUnknown")
End If
If (ls <> 0 And bVerbose) Then
Set displayDate = CreateObject("WBemScripting.SWbemDateTime")
displayDate.Value = objProduct.EvaluationEndDate
If (displayDate.GetFileTime(false) <> 0) Then
LineOut GetResource("L_MsgLicenseStatusEvalEndData") & displ
ayDate.GetVarDate
End If
End If
If (bVerbose) Then
LineOut Replace(GetResource("L_MsgRemainingWindowsRearmCount"),
"%COUNT%", objService.RemainingWindowsReArmCount)
Set displayDate = CreateObject("WBemScripting.SWbemDateTime")
displayDate.Value = objProduct.TrustedTime
If (displayDate.GetFileTime(false) <> 0) Then
LineOut GetResource("L_MsgCurrentTrustedTime") & displayDate
.GetVarDate
End If
End If
'
' KMS client properties
'
If bKmsClient Then
If IsTokenActivated(objProduct) Then
DisplayTkaClientInformation objService, objProduct
ElseIf ls <> 1 Then
LineOut GetResource("L_MsgPleaseActivateRefreshKMSInfo")
Else
DisplayKMSClientInformation objService, objProduct
End If
End If
If (bKmsServer Or (iIsPrimaryWindowsSku = 1) Or (iIsPrimaryWindowsSk
u = 2)) Then
DisplayKMSInformation objService, objProduct
End If
'We should stop processing if we aren't processing All and either we
were told to process a single
'entry only or we found the primary SKU
If strParm <> "all" Then
If (strParm = LCase(strSLActID)) Then
Exit For 'no need to continue
End If
End If
LineOut ""
End If
Next
If productKeyFound = False Then
LineOut GetResource("L_MsgErrorPKey")
End If
End Sub
Private
Dim
Dim
Dim
Sub InstallProductKey(strProductKey)
objService, objProduct
lRet, strDescription, strOutput, strVersion
iIsPrimaryWindowsSku, bIsKMS
bIsKMS = False
On Error Resume Next
set objService = GetServiceObject("Version")
strVersion = objService.Version
objService.InstallProductKey(strProductKey)
QuitIfError()
' Installing a product key could change Windows licensing state.
' Since the service determines if it can shut down and when is the next star
t time
' based on the licensing state we should reconsume the licenses here.
objService.RefreshLicenseStatus()
Sub ReinstallLicenses()
shell, fso, strOemFolder
strSppTokensFolder, folder, subFolder
shell = WScript.CreateObject("WScript.Shell")
fso = CreateObject("Scripting.FileSystemObject")
Sub ExpirationDatime(strActivationID)
objProduct
strSLActID, ls, graceRemaining, strEnds
strOutput
strDescription, bTBL
iIsPrimaryWindowsSku
bFound
strActivationID = LCase(strActivationID)
bFound = False
For Each objProduct in GetProductCollection(ProductIsPrimarySkuSelectClause
& ", LicenseStatus, GracePeriodRemaining", EmptyWhereClause)
If ((strActivationID = "" And LCase(objProduct.ApplicationId) = WindowsA
ppId) Or LCase(objProduct.ID) = strActivationID) And (objProduct.PartialProductK
ey <> "") Then
strSLActID = objProduct.ID
ls = objProduct.LicenseStatus
graceRemaining = objProduct.GracePeriodRemaining
strEnds = DateAdd("n", graceRemaining, Now)
bFound = True
iIsPrimaryWindowsSku = GetIsPrimaryWindowsSKU(objProduct)
If (strActivationID = "") And (iIsPrimaryWindowsSku = 2) Then
OutputIndeterminateOperationWarning(objProduct)
End If
strOutput = ""
If ls = 0 Then
strOutput = GetResource("L_MsgLicenseStatusUnlicensed")
ElseIf ls = 1 Then
If graceRemaining <> 0 Then
strDescription = objProduct.Description
bTBL = IsTBL(strDescription)
If bTBL Then
strOutput = Replace(GetResource("L_MsgLicenseStatusTBL")
, "%ENDDATE%", strEnds)
Else
strOutput = Replace(GetResource("L_MsgLicenseStatusVL"),
"%ENDDATE%", strEnds)
End If
Else
strOutput = GetResource("L_MsgLicenseStatusLicensed")
End If
ElseIf ls = 2 Then
strOutput = Replace(GetResource("L_MsgLicenseStatusInitialGrace"
), "%ENDDATE%", strEnds)
ElseIf ls = 3 Then
strOutput = Replace(GetResource("L_MsgLicenseStatusAdditionalGra
ce"), "%ENDDATE%", strEnds)
ElseIf ls = 4 Then
strOutput = Replace(GetResource("L_MsgLicenseStatusNonGenuineGra
ce"), "%ENDDATE%", strEnds)
ElseIf ls = 5 Then
strOutput = GetResource("L_MsgLicenseStatusNotification")
ElseIf ls = 6 Then
strOutput = Replace(GetResource("L_MsgLicenseStatusExtendedGrace
"), "%ENDDATE%", strEnds)
End If
If strOutput <> "" Then
LineOut objProduct.Name & ":"
Lineout "
" & strOutput
End If
End If
Next
If True <> bFound Then
LineOut GetResource("L_MsgErrorPKey")
End If
End Sub
' volume license service/client management
Private Sub QuitIfErrorRestoreKmsName(obj, strKmsName)
Dim errNum
If Err.Number <> 0 Then
errNum = Err.Number
If strKmsName = "" Then
obj.ClearKeyManagementServiceMachine()
Else
obj.SetKeyManagementServiceMachine(strKmsName)
End If
ShowErrorNum GetResource("L_MsgErrorText_8"), CStr(Hex(errNum))
ExitScript errNum
End If
End Sub
Private
Dim
Dim
tEnd
Dim
Dim
nKmsPort = CLng(strKmsPort)
QuitIfErrorRestoreKmsName objProduct, strKmsNamePrev
objProduct.SetKeyManagementServicePort(nKmsPort)
QuitIfErrorRestoreKmsName objProduct, strKmsNamePrev
Else
objProduct.ClearKeyManagementServicePort()
QuitIfErrorRestoreKmsName objProduct, strKmsNamePrev
End If
activationIDFound = True
Exit For
End If
Next
if activationIDFound = False Then
strOutput = Replace(GetResource("L_MsgErrorActivationID"), "%ActID%"
, strActivationID)
Lineout strOutput
End If
End If
QuitIfError()
If (strActivationID = "") Or (activationIDFound = True) Then
strOutput = Replace(GetResource("L_MsgKmsNameSet"), "%KMS%", strKmsNameP
ort)
LineOut strOutput
End If
End Sub
Private Sub SetHostCachingDisable(boolHostCaching)
Dim objService
On Error Resume Next
set objService = GetServiceObject("Version")
QuitIfError()
objService.DisableKeyManagementServiceHostCaching(boolHostCaching)
QuitIfError()
If boolHostCaching Then
LineOut GetResource("L_MsgKmsHostCachingDisabled")
Else
LineOut GetResource("L_MsgKmsHostCachingEnabled")
End If
End Sub
Private
Dim
Dim
Dim
Sub ClearKms(strActivationID)
objService, objProduct
lRet
activationIDFound, strOutput
strActivationID = LCase(strActivationID)
On Error Resume Next
set objService = GetServiceObject("Version")
QuitIfError()
End If
End Sub
Private Sub SetRenewalInterval(intInterval)
Dim objService, objProduct
Dim kmsFlag, strOutput
If (intInterval < 0) Then
LineOut GetResource("L_MsgInvalidDataError")
Exit Sub
End If
On Error Resume Next
set objService = GetServiceObject("Version")
QuitIfError()
For Each objProduct in GetProductCollection("ID, IsKeyManagementServiceMachi
ne", PartialProductKeyNonNullWhereClause)
kmsFlag = objProduct.IsKeyManagementServiceMachine
If kmsFlag Then
objService.SetVLRenewalInterval(intInterval)
QuitIfError()
strOutput = Replace(GetResource("L_MsgRenewalSet"), "%RENEWAL%", int
Interval)
LineOut strOutput
LineOut GetResource("L_MsgWarningKmsReboot")
Exit For
End If
Next
If kmsFlag <> 1 Then
LineOut GetResource("L_MsgWarningRenewal")
End If
End Sub
Private
Dim
Dim
Dim
Sub SetKmsListenPort(strPort)
objService, objProduct
kmsFlag, lRet, strOutput
nPort
End If
Next
If kmsFlag <> 1 Then
LineOut GetResource("L_MsgWarningKmsPort")
End If
End Sub
Private Sub SetDnsPublishingDisabled(bool)
Dim objService, objProduct
Dim kmsFlag, lRet, dwValue
On Error Resume Next
set objService = GetServiceObject("Version")
QuitIfError()
For Each objProduct in GetProductCollection("ID, IsKeyManagementServiceMachi
ne", PartialProductKeyNonNullWhereClause)
kmsFlag = objProduct.IsKeyManagementServiceMachine
If kmsFlag Then
objService.DisableKeyManagementServiceDnsPublishing(bool)
QuitIfError()
If bool Then
LineOut GetResource("L_MsgKmsDnsPublishingDisabled")
Else
LineOut GetResource("L_MsgKmsDnsPublishingEnabled")
End If
LineOut GetResource("L_MsgWarningKmsReboot")
Exit For
End If
Next
If kmsFlag <> 1 Then
LineOut GetResource("L_MsgKmsDnsPublishingWarning")
End If
End Sub
Private Sub SetKmsLowPriority(bool)
Dim objService, objProduct
Dim kmsFlag, lRet, dwValue
On Error Resume Next
set objService = GetServiceObject("Version")
QuitIfError()
For Each objProduct in GetProductCollection("ID, IsKeyManagementServiceMachi
ne", PartialProductKeyNonNullWhereClause)
kmsFlag = objProduct.IsKeyManagementServiceMachine
If kmsFlag Then
objService.EnableKeyManagementServiceLowPriority(bool)
QuitIfError()
If bool Then
LineOut GetResource("L_MsgKmsPriSetToLow")
Else
LineOut GetResource("L_MsgKmsPriSetToNormal")
End If
LineOut GetResource("L_MsgWarningKmsReboot")
End If
Exit For
Next
If kmsFlag <> 1 Then
LineOut GetResource("L_MsgWarningKmsPri")
End If
End Sub
''
'' Token-based Activation Commands
''
Private Function IsTokenActivated(objProduct)
Dim nILVID
On Error Resume Next
nILVID = objProduct.TokenActivationILVID
IsTokenActivated = ((Err.Number = 0) And (nILVID <> &HFFFFFFFF))
End Function
Private
Dim
Dim
Dim
Dim
Dim
Dim
Sub TkaListILs
objLicense
strHeader
strError
strGuids
arrGuids
nListed
Dim objWmiDate
LineOut GetResource("L_MsgTkaLicenses")
LineOut ""
Set objWmiDate = CreateObject("WBemScripting.SWbemDateTime")
nListed = 0
For Each objLicense in g_objWMIService.InstancesOf(TkaLicenseClass)
strHeader = GetResource("L_MsgTkaLicenseHeader")
strHeader = Replace(strHeader, "%ILID%" , objLicense.ILID )
strHeader = Replace(strHeader, "%ILVID%", objLicense.ILVID)
LineOut strHeader
LineOut "
bjLicense.ILID)
LineOut "
objLicense.ILVID)
objWmiDate.Value = objLicense.ExpirationDate
If (objWmiDate.GetFileTime(false) <> 0) Then
LineOut "
" & Replace(GetResource("L_MsgTkaLicenseExpiration"
), "%TODATE%", objWmiDate.GetVarDate)
End If
End If
If Not IsNull(objLicense.AdditionalInfo) Then
LineOut "
" & Replace(GetResource("L_MsgTkaLicenseAdditionalInfo"
), "%MOREINFO%", objLicense.AdditionalInfo)
End If
If Not IsNull(objLicense.AuthorizationStatus) And _
objLicense.AuthorizationStatus <> 0 _
Then
strError = CStr(Hex(objLicense.AuthorizationStatus))
LineOut "
" & Replace(GetResource("L_MsgTkaLicenseAuthZStatus"),
"%ERRCODE%", strError)
Else
LineOut "
" & Replace(GetResource("L_MsgTkaLicenseDescr"), "%DESC
%", objLicense.Description)
End If
LineOut ""
nListed = nListed + 1
Next
if 0 = nListed Then
LineOut GetResource("L_MsgTkaLicenseNone")
End If
End Sub
Private
Dim
Dim
Dim
Dim nILVID
On Error Resume Next
nILVID = CInt(strILVID)
QuitIfError()
LineOut GetResource("L_MsgTkaRemoving")
LineOut ""
nRemoved = 0
For Each objLicense in g_objWMIService.InstancesOf(TkaLicenseClass)
If strILID = objLicense.ILID And nILVID = objLicense.ILVID Then
strMsg = GetResource("L_MsgTkaRemovedItem")
strMsg = Replace(strMsg, "%SLID%", objLicense.ID)
On Error Resume Next
objLicense.Uninstall
QuitIfError()
LineOut strMsg
nRemoved = nRemoved + 1
End If
Next
If nRemoved = 0 Then
LineOut GetResource("L_MsgTkaRemovedNone")
End If
End Sub
Private Sub TkaSetTokenActivationOnly(bTao)
Dim objService
On Error Resume Next
set objService = GetServiceObject("Version")
QuitIfError()
objService.DisableKeyManagementServiceActivation(bTao)
QuitIfError()
If bTao Then
LineOut GetResource("L_MsgTkaTaoSet")
Else
LineOut GetResource("L_MsgTkaTaoClear")
End If
End Sub
Private
Dim
Dim
Dim
Dim
Dim
Dim
Sub TkaListCerts
objProduct
objSigner
iRet
arrGrants()
arrThumbprints
strThumbprint
Dim strChallenge
Dim strAuthInfo1
Dim strAuthInfo2
Set objSigner = TkaGetSigner()
Set objProduct = TkaGetProduct()
Set objService = TkaGetService()
DisplayActivatingSku objProduct
On Error Resume Next
iRet = objProduct.GenerateTokenActivationChallenge(strChallenge)
QuitIfError()
strAuthInfo1 = objSigner.Sign(strChallenge, strThumbprint, strPin, strAuthIn
fo2)
QuitIfError()
iRet = objProduct.DepositTokenActivationResponse(strChallenge, strAuthInfo1,
strAuthInfo2)
QuitIfError()
objService.RefreshLicenseStatus()
Err.Number = 0
objProduct.refresh_
DisplayActivatedStatus objProduct
QuitIfError()
End Sub
Private Function TkaGetService()
Set TkaGetService = GetServiceObject("Version")
End Function
Private Function TkaGetProduct()
Dim objWinProductsWithPKeyInstalled
Dim objProduct
On Error Resume Next
Set TkaGetProduct = Nothing
Set TkaGetProduct = GetProductObject( _
"ID, Name, ApplicationId, PartialProductKey, Description,
LicenseIsAddon ", _
"ApplicationId = '" & WindowsAppId & "' " &_
"AND PartialProductKey <> NULL " & _
"AND LicenseIsAddon = FALSE" _
)
QuitIfError()
End Function
"%THUMBPRINT%", arrPa
"%SUBJECT%"
, arrPa
"%ISSUER%"
, arrPa
"%FROMDATE%" , Forma
"%TODATE%"
, Forma
Sub InstallLicense(licFile)
objService
LicenseData
strOutput
e)
LineOut strOutput
LineOut ""
End Sub
' Returns the encoding for a givven file.
' Possible return values: ascii, unicode, unicodeFFFE (big-endian), utf-8
Function GetFileEncoding(strFileName)
Dim strData
Dim strEncoding
Dim oStream
Set oStream = CreateObject("ADODB.Stream")
oStream.Type = 1 'adTypeBinary
oStream.Open
oStream.LoadFromFile(strFileName)
' Default encoding is ascii
strEncoding = "ascii"
strData = BinaryToString(oStream.Read(2))
' Check for little endian (x86) unicode preamble
If (Len(strData) = 2) and strData = (Chr(255) + Chr(254)) Then
strEncoding = "unicode"
Else
oStream.Position = 0
strData = BinaryToString(oStream.Read(3))
' Check for utf-8 preamble
If (Len(strData) >= 3) and strData = (Chr(239) + Chr(187) + Chr(191)) Th
en
strEncoding = "utf-8"
End If
End If
oStream.Close
GetFileEncoding = strEncoding
End Function
' Converts binary data (VT_UI1 | VT_ARRAY) to a string (BSTR)
Function BinaryToString(dataBinary)
Dim i
Dim str
For i = 1 To LenB(dataBinary)
str = str & Chr(AscB(MidB(dataBinary, i, 1)))
Next
BinaryToString = str
End Function
' Returns string containing the whole text file data.
' Supports ascii, unicode (little-endian) and utf-8 encoding.
Function ReadAllTextFile(strFileName)
Dim strData
Dim oStream
'There is no error.
'If this is the local computer, set everything and return immediately
If g_strComputer = "." Then
Set g_objWMIService = GetObject("winmgmts:\\" & g_strComputer & "\root\c
imv2")
Set g_objRegistry = GetObject("winmgmts:\\" & g_strComputer & "\root\def
ault:StdRegProv")
If Not g_serviceConnected Then
g_serviceConnected = True
End If
Exit Function
End If
'Otherwise, establish the remote object connections
' Create Locator object to connect to remote CIM object manager
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
strErr = CStr(Hex(Err.Number))
If Err.Number <> 0 Then
strOutput = Replace(GetResource("L_MsgErrorWMI"), "%ERRCODE%", strErr)
LineOut strOutput
If Err.Description <> "" Then
LineOut GetResource("L_MsgErrorDescription") & Err.Description & "."
End If
Err.Clear
booleanConnect = False
'An error occurred
Exit Function
End If
' Connect to the namespace which is either local or remote
Set g_objWMIService = objLocator.ConnectServer (g_strComputer, "\root\cimv2"
, g_strUserName, g_strPassword)
strErr = CStr(Hex(Err.Number))
If Err.Number <> 0 Then
strOutput = Replace(GetResource("L_MsgErrorConnection"), "%ERRCODE%", st
rErr)
strOutput = Replace(strOutput, "%COMPUTERNAME%", g_strComputer)
LineOut strOutput
If Err.Description <> "" Then
LineOut GetResource("L_MsgErrorDescription") & Err.Description & "."
End If
Err.Clear
booleanConnect = False
'An error occurred
Exit Function
End If
g_objWMIService.Security_.impersonationlevel = wbemImpersonationLevelImperso
nate
strErr = CStr(Hex(Err.Number))
If Err.Number <> 0 Then
strOutput = Replace(GetResource("L_MsgErrorImpersonation"), "%ERRCODE%",
strErr)
LineOut strOutput
If Err.Description <> "" Then
LineOut GetResource("L_MsgErrorDescription") & Err.Description & "."
End If
Err.Clear
booleanConnect = False
'An error occurred
Exit Function
End If
g_objWMIService.Security_.AuthenticationLevel = wbemAuthenticationLevelPktPr
ivacy
strErr = CStr(Hex(Err.Number))
If Err.Number <> 0 Then
strOutput = Replace(GetResource("L_MsgErrorAuthenticationLevel"), "%ERRC
ODE%", strErr)
LineOut strOutput
If Err.Description <> "" Then
LineOut GetResource("L_MsgErrorDescription") & Err.Description & "."
End If
Err.Clear
booleanConnect = False
'An error occurred
Exit Function
End If
' Get the SPP service version on the remote machine
set objService = GetServiceObject("Version")
strVersion = objService.Version
' The Windows 7 version of SLMgr.vbs does not support remote connections to
Vista/WS08 machines
if (Not IsNull(strVersion)) Then
strVersion = Left(strVersion, 3)
If (strVersion = "6.0") Then
LineOut GetResource("L_MsgRemoteWmiVersionMismatch")
booleanConnect = False
Exit Function
End If
End If
Set objServer = objLocator.ConnectServer(g_strComputer, "\root\default:StdRe
gProv", g_strUserName, g_strPassword)
strErr = CStr(Hex(Err.Number))
If Err.Number <> 0 Then
strOutput = Replace(GetResource("L_MsgErrorConnectionRegistry"), "%ERRCO
DE%", strErr)
strOutput = Replace(strOutput, "%COMPUTERNAME%", g_strComputer)
LineOut strOutput
If Err.Description <> "" Then
LineOut GetResource("L_MsgErrorDescription") & Err.Description & "."
End If
Err.Clear
booleanConnect = False
'An error occurred
Exit Function
End If
objServer.Security_.ImpersonationLevel = 3
Set g_objRegistry = objServer.Get("StdRegProv")
strErr = CStr(Hex(Err.Number))
If Err.Number <> 0 Then
strOutput = Replace(GetResource("L_MsgErrorConnectionRegistry"), "%ERRCO
DE%", strErr)
strOutput = Replace(strOutput, "%COMPUTERNAME%", g_strComputer)
LineOut strOutput
If Err.Description <> "" Then
LineOut GetResource("L_MsgErrorDescription") & Err.Description & "."
End If
Err.Clear
booleanConnect = False
'An error occurred
Exit Function
End If
If Not g_serviceConnected Then
g_serviceConnected = True
End If
End Function
Function GetServiceObject(strQuery)
Dim objService
Dim colServices
On Error Resume Next
Set colServices = g_objWMIService.ExecQuery("SELECT " & strQuery & " FROM "
& ServiceClass)
QuitIfError()
For each objService in colServices
QuitIfError()
Exit For
Next
set GetServiceObject = objService
End Function
Function GetProductCollection(strSelect, strWhere)
Dim colProducts
On Error Resume Next
If strWhere = EmptyWhereClause Then
Set colProducts = g_objWMIService.ExecQuery("SELECT " & strSelect & " FR
OM " & ProductClass)
QuitIfError()
Else
Set colProducts = g_objWMIService.ExecQuery("SELECT " & strSelect & " FR
OM " & ProductClass & " WHERE " & strWhere)
QuitIfError()
End If
set GetProductCollection = colProducts
End Function
Function GetProductObject(strSelect, strWhere)
Dim objProduct
Dim colProducts
On Error Resume Next
Set colProducts = GetProductCollection(strSelect, strWhere)
QuitIfError()
End If
Else
'If we can not get the AddOn property then we assume this is a previ
ous version
'and we return a value of Uncertain, unless we can prove otherwise
If (IsKmsClient(objProduct.Description) Or IsKmsServer(objProduct.De
scription)) Then
'If the description is KMS related, we can be certain that this
is a primary SKU
iPrimarySku = 1
Else
'Indeterminate since the property was missing and we can't verif
y KMS
iPrimarySku = 2
End If
End If
End If
GetIsPrimaryWindowsSKU = iPrimarySku
End Function
Private Function WasPrimaryKeyFound(strPrimarySkuType)
If (IsKmsServer(strPrimarySkuType) Or IsKmsClient(strPrimarySkuType) Or (InS
tr(strPrimarySkuType, NotSpecialCasePrimaryKey) > 0) Or (InStr(strPrimarySkuType
, TblPrimaryKey) > 0) Or (InStr(strPrimarySkuType, IndeterminatePrimaryKeyFound)
> 0)) Then
WasPrimaryKeyFound = True
Else
WasPrimaryKeyFound = False
End If
End Function
Private Function CanPrimaryKeyTypeBeDetermined(strPrimarySkuType)
If ((InStr(strPrimarySkuType, IndeterminatePrimaryKeyFound) > 0) Or (InStr(s
trPrimarySkuType, NoPrimaryKeyFound) > 0)) Then
CanPrimaryKeyTypeBeDetermined = False
Else
CanPrimaryKeyTypeBeDetermined = True
End If
End Function
Private
Dim
Dim
Dim
Function GetPrimarySKUType()
objProduct
strPrimarySKUType, strDescription
iIsPrimaryWindowsSku
Exit For
Else
strPrimarySKUType = NotSpecialCasePrimaryKey
End If
End If
ElseIf ((iIsPrimaryWindowsSku = 2) And strPrimarySKUType = "") Then
strPrimarySKUType = IndeterminatePrimaryKeyFound
End If
Else
strPrimarySKUType = strDescription
Exit For
'no need to continue
End If
Next
If strPrimarySKUType = "" Then
strPrimarySKUType = NoPrimaryKeyFound
End If
GetPrimarySKUType = strPrimarySKUType
End Function
Private Function SetRegistryStr(hKey, strKeyPath, strValueName, strValue)
SetRegistryStr = g_objRegistry.SetStringValue(hKey, strKeyPath, strValueName
, strValue)
End Function
Private Function DeleteRegistryValue(hKey, strKeyPath, strValueName)
DeleteRegistryValue = g_objRegistry.DeleteValue(hKey, strKeyPath, strValueNa
me)
End Function
Private Function ExistsRegistryKey(hKey, strKeyPath)
Dim bGranted
Dim lRet
' Check for KEY_QUERY_VALUE for this key
lRet = g_objRegistry.CheckAccess(hKey, strKeyPath, 1, bGranted)
' Ignore real access rights, just look for existence of the key
If lRet<>2 Then
ExistsRegistryKey = True
Else
ExistsRegistryKey = False
End If
End Function
' Resource manipulation
' Get the resource string with the given name from the locale specific
' dictionary. If not found, use the built-in default.
Private Function GetResource(name)
LoadResourceData
If g_resourceDictionary.Exists(LCase(name)) Then
GetResource = g_resourceDictionary.Item(LCase(name))
Else
GetResource = Eval(name)
End If
End Function
' Loads resource strings from an ini file of the appropriate locale