Sign in to follow this  
Followers 0
rudi

Someone with a script, to *CHECK*, if Win / Office are activated?

6 posts in this topic

#1 ·  Posted (edited)

Hi.

As a "Checklist Verifyer" I'm doing a Autoit Script, checkin many things, that have to be done, befor the PCs are deployed to the network.

One point, that has been missed several times is, that Windows 7 or Office 2010 were *NOT ACTIVATED*.

So I'm wondering, if somebody knows howto simply check the ACTIVATION STATUS of Win7 and Office 2010:

  • activated yes/no
  • license key present yes/no
(The slmgr.vbs commands to fix it, in case license key or activation were missed, are not the problem [1])

Thanks for any suggestions, Rudi.

[1]

start /w slmgr /upk

start /w slmgr /ipk LICEN-SEKEY-GOES-HERE-TEXT

start slmgr /ato

edit: typo

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



I know that Office 2010 comes with the ospp.vbs script that will do this for you, not sure about 2007 (Office 2010 install directoryOffice12ospp.vbs), run with the /dstatus switch to find license status.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here's how you do it via WMI.

If IsActivated() = False Then
   ;InstallProductKey($OSkey) ; installs a product key and also activates it
  Else
   Return
  EndIf

 If OfficeIsActivated() = False Then
  ;InstallOfficeProductKey($officekey) ; installs a product key and also activates it
 Else
  Return
 EndIf

Func IsActivated()
 $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
 If IsObj($oWMIService) Then
  $oCollection = $oWMIService.ExecQuery("SELECT Description, LicenseStatus, GracePeriodRemaining FROM SoftwareLicensingProduct WHERE PartialProductKey <> null")
  If IsObj($oCollection) Then
   For $oItem In $oCollection
    Switch $oItem.LicenseStatus
     Case 0
      ConsoleWrite("Unlicensed" & @CRLF)
      Return False
     Case 1
      If $oItem.GracePeriodRemaining Then
       If StringInStr($oItem.Description, "TIMEBASED_") Then
        ConsoleWrite("Timebased activation will expire in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
        Return False
       Else
        ConsoleWrite("Volume activation will expire in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
        Return False
       EndIf
      Else
       ConsoleWrite("The machine is permanently activated." & @CRLF)
       Return True
      EndIf
     Case 2
      ConsoleWrite("Initial grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 3
      ConsoleWrite("Additional grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 4
      ConsoleWrite("Non-genuine grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 5
      ConsoleWrite("Windows is in Notification mode" & @CRLF)
      Return False
     Case 6
      ConsoleWrite("Extended grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
    EndSwitch
   Next
  EndIf
 EndIf
EndFunc

Func OfficeIsActivated()
 $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
 If IsObj($oWMIService) Then
  $oCollection = $oWMIService.ExecQuery("SELECT Description, LicenseStatus, GracePeriodRemaining FROM OfficeSoftwareProtectionProduct WHERE PartialProductKey <> null")
  If IsObj($oCollection) Then
   For $oItem In $oCollection
    Switch $oItem.LicenseStatus
     Case 0
      ConsoleWrite(" ---UNLICENSED--- " & @CRLF)
      Return False
     Case 1
      ConsoleWrite(" ---LICENSED--- " & @CRLF)
       ;If licSr = 0 Then
       ; WScript.Echo MSG_ERRCODE & licSr & " as licensed"   
       ;End If
      Return True
     Case 2
      ConsoleWrite("---OOB_GRACE--- Initial grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 3
      ConsoleWrite("---OOT_GRACE--- Initial grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 4
      ConsoleWrite("---NON_GENUINE_GRACE--- Grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 5
      ConsoleWrite("---NOTIFICATIONS--- Grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case 6
      ConsoleWrite("---EXTENDED GRACE--- Extended grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
      Return False
     Case Else
      ConsoleWrite("---UNKNOWN---" & @CRLF)
      Return False
    EndSwitch
   Next
  EndIf
 EndIf
EndFunc

If you are interested I will give you the code to activate it directly through WMI as well.

Edited by NDog

Share this post


Link to post
Share on other sites

Can somebody supply the code to activate Windows through WMI as was mentioned above?

Share this post


Link to post
Share on other sites

Here you go.

The IsActivated() Function was written by trancexx (who never replies to PM's TT), and the rest I wrote. I think the Windows XP activation part is a bit broken, maybe someone can check it. Part of this code block here has missing proxy gateway authentication stuff, which may get added eventually. It also has offline token restore for Office 2010 activation, but that is a whole lot more complex and I have not included it here.

Please add improvements if you have any.

$OSkey = "ZZZZZ-VVVVV-XXXXX-VVVVV-ZZZZZ"
$officekey = "ZZZZZ-VVVVV-XXXXX-VVVVV-ZZZZZ"

; For WPA activation
Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling.
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Install a custom error handler

; Detect OS version
If @OSVersion == "WIN_8" Or @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Then
    $OS = 'NT6'
ElseIf @OSVersion == "WIN_XP" Then
    $OS = 'NT5'
Else
    MsgBox(48, "Error", "Wrong OS version")
    Exit
EndIf


If IsDeclared('OSkey') Then OSactivate()
If IsDeclared('officekey') Then Officeactivate()


Func OSactivate()
    If $OS = 'NT6' Then
        ; First attempt - go out to the internet directly
        If IsActivated() = False Then
            InstallProductKey($OSkey) ; installs a product key and also activates it
        Else
            Return
        EndIf
        
        ; Second attempt - set the gateway as the proxy and go to .microsoft.com out the proxy
        If IsActivated() = False Then
            ;setgatewayproxy("on")
            InstallProductKey($OSkey) ; installs a product key and also activates it
            ;setgatewayproxy("off")
        Else
            Return
        EndIf

        ; Third attempt - proxy out through gateway using a user/pass combo
        If IsActivated() = False Then
            ;setgatewayproxy("on","user","password")
            InstallProductKey($OSkey) ; installs a product key and also activates it
            ;setgatewayproxy("off")
        Else
            Return
        EndIf

    ElseIf $OS = 'NT5' Then
        $VOL_PROD_KEY = StringReplace($OSkey,"-","") ;remove hyphens if any
        $x = ObjGet("winmgmts:{impersonationLevel=impersonate}")
        $y = $x.InstancesOf ("win32_WindowsProductActivation")
        For $Obj In $y
            $result = $Obj.SetProductKey ($VOL_PROD_KEY)
            If $g_eventerror then Msgbox(0,"","The key used is not valid - " & $VOL_PROD_KEY)
        Next
    EndIf
EndFunc

Func IsActivated()
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Description, LicenseStatus, GracePeriodRemaining FROM SoftwareLicensingProduct WHERE PartialProductKey <> null")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                Switch $oItem.LicenseStatus
                    Case 0
                        ConsoleWrite("Unlicensed" & @CRLF)
                        Return False
                    Case 1
                        If $oItem.GracePeriodRemaining Then
                            If StringInStr($oItem.Description, "TIMEBASED_") Then
                                ConsoleWrite("Timebased activation will expire in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                                Return False
                            Else
                                ConsoleWrite("Volume activation will expire in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                                Return False
                            EndIf
                        Else
                            ConsoleWrite("The machine is permanently activated." & @CRLF)
                            Return True
                        EndIf
                    Case 2
                        ConsoleWrite("Initial grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 3
                        ConsoleWrite("Additional grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 4
                        ConsoleWrite("Non-genuine grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 5
                        ConsoleWrite("Windows is in Notification mode" & @CRLF)
                        Return False
                    Case 6
                        ConsoleWrite("Extended grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                EndSwitch
            Next
        EndIf
    EndIf
EndFunc

Func InstallProductKey($strProductKey)
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Version FROM SoftwareLicensingService")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite($oItem.Version & @CRLF)
                
                $oItem.InstallProductKey($strProductKey)
                If $g_eventerror then Return
                ActivateProduct()
                
            Next
        EndIf
    EndIf
EndFunc

Func ActivateProduct()
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT ID, ApplicationId, PartialProductKey, LicenseIsAddon, Description, Name, LicenseStatus FROM SoftwareLicensingProduct WHERE PartialProductKey <> null")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite($oItem.Name & @CRLF)
                ConsoleWrite($oItem.ID & @CRLF)
                
                $oItem.Activate()
                If $g_eventerror then Return
                RefreshStatus() ; Needed otherwise windows complains about non genuine key and changes background to black
                $oItem.refresh_
            Next
        EndIf
    EndIf
EndFunc

Func RefreshStatus()
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Version FROM SoftwareLicensingService")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite("Refresh Status" & @CRLF)
                $oItem.RefreshLicenseStatus()
            Next
        EndIf
    EndIf
EndFunc


Func Officeactivate()
    ; First attempt - go out to the internet directly
    If OfficeIsActivated() = False Then
        InstallOfficeProductKeyActivateInternet($officekey) ; installs a product key and also activates it
    Else
        Return
    EndIf
EndFunc

Func OfficeIsActivated()
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Description, LicenseStatus, GracePeriodRemaining FROM OfficeSoftwareProtectionProduct WHERE PartialProductKey <> null")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                Switch $oItem.LicenseStatus
                    Case 0
                        ConsoleWrite(" ---UNLICENSED--- " & @CRLF)
                        Return False
                    Case 1
                        ConsoleWrite(" ---LICENSED--- " & @CRLF)
                            ;If licSr = 0 Then
                            ;    WScript.Echo MSG_ERRCODE & licSr & " as licensed"    
                            ;End If
                        Return True
                    Case 2
                        ConsoleWrite("---OOB_GRACE--- Initial grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 3
                        ConsoleWrite("---OOT_GRACE--- Initial grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 4
                        ConsoleWrite("---NON_GENUINE_GRACE--- Grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 5
                        ConsoleWrite("---NOTIFICATIONS--- Grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case 6
                        ConsoleWrite("---EXTENDED GRACE--- Extended grace period ends in " & $oItem.GracePeriodRemaining & " minutes" & @CRLF)
                        Return False
                    Case Else
                        ConsoleWrite("---UNKNOWN---" & @CRLF)
                        Return False
                EndSwitch
            Next
        EndIf
    EndIf
EndFunc


Func InstallOfficeProductKeyOnly($strProductKey)
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Version FROM OfficeSoftwareProtectionService")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite($oItem.Version & @CRLF)
                
                $oItem.InstallProductKey($strProductKey)
                If $g_eventerror then Return
                
            Next
        EndIf
    EndIf
EndFunc



Func InstallOfficeProductKeyActivateInternet($strProductKey)
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Version FROM OfficeSoftwareProtectionService")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite($oItem.Version & @CRLF)
                
                $oItem.InstallProductKey($strProductKey)
                If $g_eventerror then Return
                ActivateOfficeProduct()
                
            Next
        EndIf
    EndIf
EndFunc

Func ActivateOfficeProduct()
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT ID, ApplicationId, PartialProductKey, LicenseIsAddon, Description, Name, LicenseStatus FROM OfficeSoftwareProtectionProduct WHERE PartialProductKey <> null")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite($oItem.Name & @CRLF)
                ConsoleWrite($oItem.ID & @CRLF)
                
                $oItem.Activate()
                If $g_eventerror then Return
                RefreshOfficeStatus() ; Needed to refresh status
                $oItem.refresh_
            Next
        EndIf
    EndIf
EndFunc

Func RefreshOfficeStatus()
    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    If IsObj($oWMIService) Then
        $oCollection = $oWMIService.ExecQuery("SELECT Version FROM OfficeSoftwareProtectionService")
        If IsObj($oCollection) Then
            For $oItem In $oCollection
                ConsoleWrite("Refresh Status" & @CRLF)
                $oItem.RefreshLicenseStatus()
            Next
        EndIf
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

NDog, thank you very much for posting that code!  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0