'# D.Collins - 14:15 25/11/2014 '# Applies a license key to a command line depending on if a qualifying user is logged on. '# Designed to be ran as an admin Service user or SYSTEM. Option Explicit '________________________________________________________________________________ ' Setup Error Handler 'ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ '# Error Exception Handler. Class QuitErrorlevelOnRuntimeError Private Sub Class_Terminate() '# If Err.Number = 0 Then wscript.quit will return -2147155971. '# If the script just ends with no WScript.Quit, Err.Number will = 0 If Err.Number <> -2147155971 And Err.Number <> 0 Then Set wshShell = CreateObject("WScript.Shell") wshShell.LogEvent 1, "FATAL ERROR - Fatal error in '" & WScript.ScriptFullName & "' process. Error " & Err.Number & " - " & Err.Source & " - " & Err.Description WScript.Quit Err.Number End If End Sub End Class Dim wshShell, qeloreObj : Set qeloreObj = New QuitErrorlevelOnRuntimeError '________________________________________________________________________________ ' Main Script 'ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ Dim dicLicenses, strMSIpath, arrUnames, strUser, blFound, strCmd, ret Set wshShell = CreateObject("WScript.Shell") Set dicLicenses = CreateObject("Scripting.Dictionary") dicLicenses("username1") = "lic-key-key-key-1" dicLicenses("username2") = "lic-key-key-key-2" dicLicenses("username3") = "lic-key-key-key-3" dicLicenses("username4") = "lic-key-key-key-4" dicLicenses("username5") = "lic-key-key-key-5" dicLicenses("username6") = "lic-key-key-key-6" dicLicenses("username7") = "lic-key-key-key-7" dicLicenses("username8") = "lic-key-key-key-8" dicLicenses("username9") = "lic-key-key-key-9" dicLicenses("username10") = "lic-key-key-key-10" strMSIpath = "Full\Path\To\Your\Installer.msi" '## EDIT PATH HERE ###### arrUnames = fGetLoggedOnUserName() If Not IsEmpty(arrUnames) Then blFound = False For Each strUser In arrUnames If dicLicenses.Exists(strUser) Then strCmd = "msiexec /i """ & strMSIpath & """ LICKEY=" & dicLicenses(strUser) '## EDIT COMMAND LINE HERE ####### MsgBox "About to Run: " & vbCrlf & strCmd '#### comment out this line when executing as non-interactive ret = wshShell.Run(strCmd, 1, True) blFound = True End If If blFound Then Exit For Next If Not blFound Then MsgBox "No licensed users logged on here" '#### comment out this line when executing as non-interactive WScript.Quit 0 '# Success, just no qualifying users were logged on End If Else MsgBox "No logged on users found" '#### comment out this line when executing as non-interactive WScript.Quit 0 '# Success, just there was no-one logged on End If WScript.Quit ret '# return the exit code from the command in strCmd above. '# End of Script '----- Function ------ Function fGetLoggedOnUserName() '# returns an array of logged on usernames (there may be more than one) Const HKEY_USERS = &H80000003 Dim objReg, arrSubKeys, strKey, strSIDList, strSID, arrSIDList, sUname, sUnameList Set objReg = GetObject("winmgmts:{impersonationLevel = impersonate}!\\.\root\default:StdRegProv") objReg.EnumKey HKEY_USERS, "", arrSubKeys If IsNull(arrSubKeys) Then '# Nothing found under HKU - this must be an error WScript.Quit 114001 End If strSIDList = "" For Each strKey In arrSubKeys If Left(strKey, 8) = "S-1-5-21" And Right(strKey, 8) <> "_Classes" Then strSIDList = strSIDList & strKey & "|" End If Next arrSIDList = Split(strSIDList, "|") sUnameList = "" For Each strSID In arrSIDList If strSID = "" Then Exit For objReg.GetStringValue HKEY_USERS, strSID & "\Volatile Environment", "USERNAME", sUname If Not IsNull(sUname) Then sUnameList = sUnameList & sUname & "|" End If Next If Right(sUnameList, 1) = "|" Then sUnameList = Left(sUnameList, Len(sUnameList) - 1) '# Remove trailing '|' If sUnameList = "" Then '# No-one logged on fGetLoggedOnUserName = Empty Exit Function End If fGetLoggedOnUserName = Split(sUnameList, "|") End Function