'ACTivate clean UPgrade install.vbs -- this script enables activation 
'of a clean install of Windows Upgrade 
'
'DO NOT REMOVE THIS HEADER!
'
'Copyright Andrew ARONOFF 11 November 2012, http://www.pcdr.fr/
'This script is provided without any warranty, either express or implied
'It may not be copied or distributed without permission
'
'(END OF HEADER)

'Revision R01

Option Explicit

'Objets
Dim Wshso : Set Wshso = WScript.CreateObject("WScript.Shell")
Dim oWMI : Set oWMI = GetObject("winmgmts:root\cimv2")
Dim oReg : Set oReg = GetObject("winmgmts:root\default:StdRegProv")
Dim WshoArgs : Set WshoArgs = WScript.Arguments
Dim colSWLP, oSWLP  'SoftWare Licensed Product collection/member
Dim colOS, oOS  'OS collection/member
Dim oShellApp

'string variables
Dim strArg  'argument string
Dim strKey  'registry key

'integers
Dim intErrNum  'error number
Dim intMB  'MsgBox return value

'constants
Const DQ = """", HKLM = &H80000002, KQV = &H1, KSV = &H2

'flags
Dim flag2ndPass : flag2ndPass = False  'True if script re-entered
Dim flagAct : flagAct = False  'True if OS is activated
Dim flagAccess : flagAccess = False  'True if elevated privileges exist 

'MsgBox/Popup text strings
'MBM = MsgBox Message, MBT = MsgBox Title
Dim strMBMPub, strMBTPub
Dim strMBMWrongOS, strMBTWrongOS
Dim strMBMNTD, strMBTNTD  'Nothing To Do
Dim strMBMAdminRights, strMBTAdminRights
Dim strMBMActivate, strMBTActivate
Dim strMBMDWE, strMBTDWE  'DWORD Error
Dim strMBMSLUIE, strMBTSLUIE  'SLUI Error
Dim strMBMRebootNow, strMBTRebootNow  'reboot request
Dim strMBMCULater, strMBTCULater  'reboot deferred

'--------------------------------------------------------------------- 

 strMBMPub = "This tool was written by the company " & DQ & "PC Dr" & DQ & vbCRLF & vbCRLF &_ 
  Space(11) & "Problem Practitioner," & vbCRLF &_
  Space(13) & "Solution Specialist." & vbCRLF & vbCRLF &_ 
  Space(10) & "+33 (0)9 50 25 52 47" & vbCRLF & Space(17) & "www.pcdr.fr"

 strMBTPub = "Commercial break ;-)"

'--------------------------------------------------------------------- 

 strMBMWrongOS = "This script will only run under Windows Vista," & vbCRLF &_
  "Windows 7, or Windows 8." &_ 
  vbCRLF & vbCRLF &_
  "This script must exit."

 strMBTWrongOS = "Wrong OS!"

'--------------------------------------------------------------------- 

 strMBMNTD = "This installation of Windows is already activated." &_ 
  vbCRLF & vbCRLF &_
  "This script must exit."

 strMBTNTD = "Windows activated!"

'--------------------------------------------------------------------- 

 strMBMAdminRights = _
  "You must be an administrator to use this script!" &_
   vbCRLF & vbCRLF & "This script must exit."

 strMBTAdminRights = "Admin account required!"

'--------------------------------------------------------------------- 

 strMBMActivate = _
  "To enable Windows to be activated, this script is ready" & vbCRLF &_ 
  "to make the following changes:" &_
  vbCRLF & vbCRLF &_
  "1. Change the " & DQ & "MediaBootInstall" & DQ & " value to 0 in the registry at" & vbCRLF &_
  Space(5) & "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\OOBE\" &_
   vbCRLF & vbCRLF &_
  "2. Launch " & DQ & "slmgr -rearm" & DQ & " at a hidden command prompt" &_ 
  vbCRLF & vbCRLF &_
  "3. Request permission to reboot" &_
  vbCRLF & vbCRLF &_
  "4. Launch " & DQ & "slui 3" & DQ & " after the restart to permit entry of the product key" &_
  vbCRLF & vbCRLF &_ 
  Space(5) & "Note that you will need the product key at this step!" &_ 
  vbCRLF & vbCRLF &_
  "Click " & DQ & "OK" & DQ & " proceed with activation, or" &_
  vbCRLF & Space(9) & DQ & "Cancel" & DQ & " to quit."

 strMBTActivate = "Activate Windows?"

'--------------------------------------------------------------------- 

 strMBMDWE = "The value for " & DQ & "MediaBootInstall" & DQ & " at" & vbCRLF &_ 
  "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\OOBE\" & vbCRLF &_ 
  "could not be changed to 0." &_
  vbCRLF & vbCRLF &_
  "This script must exit."

 strMBTDWE = "Registry edit error!"

'--------------------------------------------------------------------- 

 strMBMSLUIE = DQ & "SLUI 3" & DQ & " could not be added to the registry at" & vbCRLF &_ 
  "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\" &_ 
  vbCRLF & vbCRLF &_
  "This script must exit."

 strMBTSLUIE = "Registry edit error!"

'--------------------------------------------------------------------- 

 strMBMRebootNow = _
  "Click " & DQ & "OK" & DQ & " to reboot now" &_
  ", or" & vbCRLF & Space(9) & DQ & "Cancel" & DQ &_
  " to reboot later." &_
  vbCRLF & vbCRLF &_
  "Prepare the product key."

 strMBTRebootNow = "Reboot now?"

'--------------------------------------------------------------------- 

 strMBMCULater = _
  "The product key will need to be entered at the next reboot."

 strMBTCULater = "Reboot later"

'--------------------------------------------------------------------- 

'if command-line argument found, on 2nd pass, so toggle flag 
If WshoArgs.Count > 0 Then flag2ndPass = True
Set WshoArgs=Nothing

'on opener, give myself credit
If Not flag2ndPass Then

 MsgBox strMBMPub, _
  vbOKOnly + vbInformation + vbSystemModal, _
  strMBTPub

End If

'get Windows OS collection
Set colOS = oWMI.ExecQuery ("Select * from Win32_OperatingSystem")

'exclude any OS other than WVa/Wn7/Wn8
For Each oOS in colOS

 If ((InStr(LCase(oOS.Name),"vista") = 0) And _ 
  (InStr(LCase(oOS.Name),"windows 7") = 0) And _ 
  (InStr(LCase(oOS.Name),"windows" & Chr(160) & "7") = 0) And _ 
  (InStr(LCase(oOS.Name),"windows 8") = 0) And _ 
  (InStr(LCase(oOS.Name),"windows" & Chr(160) & "8") = 0)) Then

  MsgBox strMBMWrongOS, _
   vbOKOnly + vbExclamation + vbSystemModal, _
   strMBTWrongOS

  WScript.Quit

 End If

Next 

Set colOS=Nothing

'check current activation status
Set colSWLP = oWMI.ExecQuery("Select * From SoftwareLicensingProduct")

For Each oSWLP In colSWLP

 If (oSWLP.LicenseStatus = 1) And (oSWLP.GracePeriodRemaining = 0) Then
  flagAct = True : Exit For
 End If

Next

Set colSWLP=Nothing : Set oWMI=Nothing

'quit if OS activated
If flagAct Then

 MsgBox strMBMNTD, _
  vbOKOnly + vbInformation + vbSystemModal, _
  strMBTNTD

 WScript.Quit

End If

'check for elevated privileges (Admin rights)
Set oReg = GetObject("winmgmts:root\default:StdRegProv")
strKey = "System\CurrentControlSet\Control\Session Manager"
intErrNum = oReg.CheckAccess(HKLM,strKey,KQV + KSV,flagAccess)

'if no admin rights
If Not flagAccess Then

 'if on first pass, re-enter via Shell.Application
 If Not flag2ndPass Then 

  strArg = "__UAC__"

  Set oShellApp = CreateObject("Shell.Application")
  oShellApp.ShellExecute WScript.FullName, _
   DQ & WScript.ScriptFullName & DQ & Space(1) & strArg, "", "runas", 1

  WScript.Quit

 Else  '2nd pass 

  'say Admin rights are needed & quit
  MsgBox strMBMAdminRights, _
   vbOKOnly + vbSystemModal + vbCritical, _
   strMBTAdminRights

  WScript.Quit

 End If  '1st or 2nd pass?

End If  'flagAccess?

'announce activation steps
intMB = MsgBox (strMBMActivate, _
 vbOKCancel + vbInformation + vbSystemModal, _
 strMBTActivate)

If intMB = vbCancel Then WScript.Quit

'modify MediaBootInstall DWORD value
intErrNum = oReg.SetDWORDValue (HKLM, "SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\OOBE", _
 "MediaBootInstall", 0)
If intErrNum <> 0 Then

 MsgBox strMBMDWE, _
  vbOKONly + vbExclamation + vbSystemModal, _
  strMBTDWE

 WScript.Quit

End If

'run slmgr -rearm at hidden command prompt
Wshso.Run "%comspec% /c slmgr.vbs -rearm",0,TRUE

'prepare to run slui 3 via RunOnce
intErrNum = oReg.SetStringValue (HKLM, "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", _
 "SLUI", "slui 3")

If intErrNum <> 0 Then

 MsgBox strMBMSLUIE, _
  vbOKONly + vbExclamation + vbSystemModal, _
  strMBTSLUIE

 WScript.Quit

End If

Set oReg=Nothing

'offer to reboot
intMB = MsgBox (strMBMRebootNow, _
 vbOKCancel + vbSystemModal + vbQuestion, strMBTRebootNow)

'reboot if OK clicked
If intMB = vbOK Then

 'use WMI with shutdown privileges to reboot
 Set oWMI = GetObject("winmgmts:{(Shutdown)}\root\cimv2")
 Set colOS = oWMI.ExecQuery ("Select * from Win32_OperatingSystem")

 For Each oOS In colOS : oOS.Win32ShutDown "6" : Next

Else  'Cancel clicked

 MsgBox strMBMCULater, _
  vbOKOnly + vbSystemModal + vbInformation, strMBTCULater

End If

Set Wshso=Nothing


'R00 2012-11-10

'R01
'renamed to Act-Up, added Option Explicit, ad displayed first but only
'once, simplified oShellApp logic, switched from strUAC to
'flag2ndPass, reused oWMI & colOS to shut down


