Search code examples
windowsfile-iovbscriptbatch-processingactivation

Activate Windows on many PCs with different FPP Serials


I am writing a script to automate the activation of 200 hundreds PCs like using a Volume Licensing Key, But the case here is The serials are for Full Packaged Product.

  1. Used a text file for storing serial numbers one per line.
  2. Read a 29 chars (serial)
  3. Call slmgr to install the key and then activate it.
  4. Add to the line "Used Key" to indicate that it was consumed.

I currently have this code:

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
  WScript.Quit
End If

Dim objFSO, objTextFile
Dim sRead, sReadLine, sReadAll
Const ForReading = 1, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\SN.txt", ForReading)
sRead = objTextFile.Read(29)
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "slmgr.vbs -ipk " & sRead
objShell.Run "slmgr.vbs -ato"
Set objTextFile = objFSO.OpenTextFile("D:\SN.txt", ForAppending, True)
objTextFile.Write (" Used Key.")
objTextFile.Close

The problem is how to use this script once on each PC, i.e. skip to the next line when the script is executed on another PC.


Solution

  • It's not quite clear to me whether your question is about how to handle the product keys, or also about how to run the script on each computer. For the latter you'd need to provide more information about your environment, so in this answer I'm addressing just the key handling.

    The product key handling could be implemented by using different files for used and unused keys. First you read the available keys into an appropriate data structure (e.g. an ArrayList):

    basedir    = objFSO.GetParentFolderName(WScript.ScriptName)
    unusedKeys = objFSO.BuildPath(basedir, "SN.txt")
    
    Set objTextFile = objFSO.OpenTextFile(unusedKeys, ForReading)
    Set keys = CreateObject("System.Collections.ArrayList")
    Do Until objTextFile.AtEndOfStream
      keys.Add objTextFile.ReadLine
    Loop
    objTextFile.Close
    

    Retrieve a key from the list and record it in the "used product keys" file:

    If keys.Count > 0 Then
      productKey = keys(0)
      keys.RemoveAt(0)
      usedKeys = objFSO.BuildPath(basedir, "used-sn.txt")
      objFSO.OpenTextFile(usedKeys, ForAppending, True).WriteLine productKey
    Else
      WScript.Echo "No product key available."
      WScript.Quit 1
    End If
    

    Write the remaining unused keys back to the original file:

    Set objTextFile = objFSO.OpenTextFile(unusedKeys, 2)
    For Each key In keys
      objTextFile.WriteLine key
    Loop
    objTextFile.Close