[VBScript / Batch] LQuery (System and Licensing Query Tool)

Discussion in 'Scripting' started by anemeros, Jan 31, 2011.

  1. anemeros

    anemeros MDL Developer

    Aug 12, 2009
    81
    280
    0
    #1 anemeros, Jan 31, 2011
    Last edited by a moderator: Apr 20, 2017
    I wrote this tool in December, but never posted it because I didn't know where to put it. Here on MDL seems like a good place. ;) It's a vbscript that retrieves information about your computer for you, such as the operating system name and extended build number, product key and product id numbers, product licensing status and channel, WGA genuine validation status, rearm count, grace time left, and evaluation end date. Command line parameters are

    ProductTitle — operating system name and extended build number
    ProductKeyData — product key and product id numbers
    LicenseStatus — product licensing status and channel
    GenuineStatus — wga genuine validation status
    EvaluationStatus — rearm count, grace time left, and evaluation end date

    To run it for command line output, use:
    Code:
    "%SystemRoot%\System32\cscript.exe" //nologo LQuery.vbs
    To run it for windows dialog output, use:
    Code:
    "%SystemRoot%\System32\wscript.exe" LQuery.vbs
    Code:
    ' License Query Script 1.0
    ' by anemeros & bRonCOde
    
    If WScript.Arguments.Count <> 1 Then Help(1) Else arg = LCase(WScript.Arguments.item(0))
    
    strComputer = "."
    Set objShell = CreateObject("WScript.Shell")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
    strProductKey = GetKey(objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
    Set colSLP = objWMIService.ExecQuery("SELECT * FROM SoftwareLicensingProduct WHERE PartialProductKey = '" & Right("0" & strProductKey, 5) & "'", , 48)
    Set colItems = objWMIService.ExecQuery("SELECT * FROM SoftwareLicensingService")
    
    Select Case arg
    Case "producttitle", "name", "title", "1" : ProductTitle
    Case "productkeydata", "key", "serial", "id", "2" : ProductKeyData
    Case "licensestatus", "license", "channel", "3" : LicenseStatus
    Case "genuinestatus", "genuine", "4" : GenuineStatus
    Case "evaluationstatus", "evaluation", "eval", "grace", "expiration", "expire", "exp", "5" : EvaluationStatus
    Case Else : Help(2)
    End Select
    
    Sub ProductTitle
    Wscript.Echo _
    "Product:  " & objShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName") & VBNewLine & _
    "Build:    " & objShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\BuildLabEx")
    End Sub
    
    Sub ProductKeyData
    Wscript.Echo _
    "Product Key:      " & GetKey(objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId")) & VBNewLine & _
    "Product ID:       " & objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId")
    End Sub
    
    Sub LicenseStatus
    Const HR_S_OK                                 = 0
    Const HR_ERROR_FILE_NOT_FOUND                 = &H80070002
    Const HR_SL_E_GRACE_TIME_EXPIRED              = &HC004F009
    Const HR_SL_E_NOT_GENUINE                     = &HC004F200
    For Each objSLP in colSLP
    Select Case objSLP.LicenseStatus
    Case "0" : strSLPLicenseStatus = "Unlicensed"
    Case "1" : strSLPLicenseStatus = "Licensed"
    Case "2" : strSLPLicenseStatus = "Out-Of-Box Grace Period"
    Case "3" : strSLPLicenseStatus = "Out-Of-Tolerance Grace Period"
    Case "4" : strSLPLicenseStatus = "Non-Genuine Grace Period"
    Case "5" : strSLPLicenseStatus = "Notification"
    Select Case objSLP.LicenseStatusReason
    Case HR_SL_E_GRACE_TIME_EXPIRED : strSLPLicenseStatus = strSLPLicenseStatus & ", grace time expired [code &HC004F009]"
    Case HR_SL_E_NOT_GENUINE : strSLPLicenseStatus = strSLPLicenseStatus & ", not genuine [code &HC004F200]"
    Case Else : strSLPLicenseStatus = strSLPLicenseStatus + " [code " & objSLP.LicenseStatusReason & "]"
    End Select
    Case "6" : strSLPLicenseStatus = "Extended Grace"
    Case Else : strSLPLicenseStatus = "Code " & objSLP.LicenseStatus
    End Select
    strDesc = objSLP.Description
    If InStrB(strDesc,"OEM_SLP") > 0 then
    strLicTypeChannel = "OEM_SLP"
    ElseIf InStrB(strDesc,"OEM_COA_SLP") > 0 then
    strLicTypeChannel = "OEM_COA_SLP"
    ElseIf InStrB(strDesc,"OEM_COA_NSLP") > 0 then
    strLicTypeChannel = "OEM_COA_NSLP"
    ElseIf InStrB(strDesc,"RETAIL") > 0 then
    strLicTypeChannel = "RETAIL"
    ElseIf InStrB(strDesc,"VOLUME_MAK") > 0 then
    strLicTypeChannel = "VOLUME_MAK"
    ElseIf InStrB(strDesc,"VOLUME_KMSCLIENT") > 0 then
    strLicTypeChannel = "VOLUME_KMSCLIENT"
    ElseIf InStrB(strDesc,"VOLUME_KMS") > 0 then
    strLicTypeChannel = "VOLUME_KMS"
    Else strLicTypeChannel = "Unknown"
    End If
    Wscript.Echo _
    "License Status:   " & strSLPLicenseStatus & vbNewLine & _
    "License Channel:  " & strLicTypeChannel
    Next
    End Sub
    
    Sub GenuineStatus
    For Each objSLP in colSLP
    Select Case objSLP.GenuineStatus
    Case "0" : strSLPGenuineStatus = "Genuine [code 0]"
    Case "1" : strSLPGenuineStatus = "Not Activated [code 1]"
    Case "3" : strSLPGenuineStatus = "Blocked VLK [code 3]"
    Case "4" : strSLPGenuineStatus = "No Check PID [code 4]"
    Case "5" : strSLPGenuineStatus = "PID is invalid [code 5]"
    Case "6" : strSLPGenuineStatus = "Genuine / WGA unsupported OS [code 6]"
    Case "8" : strSLPGenuineStatus = "Invalid Product Key [code 8]"
    Case "10" : strSLPGenuineStatus = "Clock sync error [code 10]"
    Case "11" : strSLPGenuineStatus = "Blocked PID [code 11]"
    Case "12" : strSLPGenuineStatus = "License store error [code 12]"
    Case "13" : strSLPGenuineStatus = "Geographically blocked PID [code 13]"
    Case "50" : strSLPGenuineStatus = "Invalid License [code 50]"
    Case "51" : strSLPGenuineStatus = "Tampered Binary [code 51]"
    Case Else : strSLPGenuineStatus = "Code " & sobjSLP.GenuineStatus
    End Select
    Wscript.Echo "Genuine Status:  " & strSLPGenuineStatus
    Next
    End Sub
    
    Sub EvaluationStatus
    For Each objItem in colItems
    strRearmsRemaining = objItem.RemainingWindowsReArmCount
    Next
    For Each objSLP in colSLP
    objSWbemDateTime.Value = objSLP.EvaluationEndDate
    Wscript.Echo _
    "Rearms Remaining:  " & strRearmsRemaining & VBNewLine & _
    "Grace Remaining:   " & objSLP.GracePeriodRemaining & " minutes" & VBNewLine & _
    "Extended Grace:    " & objSLP.ExtendedGrace & " minutes" & VBNewLine & _
    "Evaluation End:    " & objSWbemDateTime.GetVarDate(True)
    Next
    End Sub
    
    Sub Help(exitcode)
    Wscript.Echo "Command-Line Parameters:" & VBNewLine & VBNewLine & _
    "ProductTitle         operating system name and extended build number" & VBNewLine & _
    "ProductKeyData       product key and product id numbers" & VBNewLine & _
    "LicenseStatus        product licensing status and channel" & VBNewLine & _
    "GenuineStatus        wga genuine validation status" & VBNewLine & _
    "EvaluationStatus     rearm count, grace time left, and eval end date"
    Wscript.quit exitcode
    End Sub
    
    Function GetKey(d)
    Const a = 52
    i=28
    b = "BCDFGHJKMPQRTVWXY2346789"
    Do
    c = 0
    j = 14
    Do
    c = c * 256
    c = d(j + a) + c
    d(j + a) = (c \ 24) and 255
    c = c Mod 24
    j = j - 1
    Loop While j >= 0
    i = i - 1
    e = mid(b, c + 1, 1) & e
    If (29 - i) Mod 6 = 0 and i <> -1 then
    i = i - 1
    e = "-" & e
    End If
    Loop While i >= 0
    GetKey = e
    End Function
     

    Attached Files: