0

I have this vbscript which outputs all installed applications in a pc using this registry key :

 HKEY_LOCAL_MACHINE \SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\

Now i have this code to format and output all applications which only have DisplayName attribute.

 Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE 
    strComputer = "." 
    strKey = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" 
    strEntry1a = "DisplayName" 
    strEntry1b = "QuietDisplayName" 
    strEntry2 = "InstallDate" 
    strEntry3 = "DisplayVersion" 


    set objReg = GetObject("winmgmts://" & strComputer & _ 
     "/root/default:StdRegProv") 

    RegData = objReg.RegRead(RegValue)

    objReg.EnumKey HKLM, strKey, arrSubkeys 


    WScript.Echo "Installed Applications" 
    For Each strSubkey In arrSubkeys 
      intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _ 
       strEntry1a, strValue1) 
      If intRet1 <> 0 Then 
        objReg.GetStringValue HKLM, strKey & strSubkey, _ 
         strEntry1b, strValue1 
      End If 

      objReg.GetStringValue HKLM, strKey & strSubkey, _ 
       strEntry2, strValue2 

      objReg.GetStringValue HKLM, strKey & strSubkey, _ 
       strEntry3, intValue3 


      strEntry6 = strValue1 
      If strValue2 <> "" Then
        strEntry6 = strEntry6 & ";" & strValue2
      Else
        strEntry6 = strEntry6 & ";" & "-"
      End If 

      If intValue3 <> "" Then 
        strEntry6 = strEntry6 & ";" & intValue3 
      Else
        strEntry6 = strEntry6 & ";" & "-"
      End If
      If (strValue1 <> "") Then
         WScript.Echo RegData 
      End If
    Next 

The problem is it produces duplicate data. I want to like filter and display only a unique DisplayName. I think the key here is for every loop, it checks if the same display name is found in other field. Im new to vbscripting and i barely know how to use the objects.

user352156
  • 99
  • 1
  • 4
  • 14

2 Answers2

0

I can't get your code to run, but I suggest you can use a Scripting.Dictionary object to ensure unique values. Generally, you can use it like this:

dim dictionary: set dictionary = CreateObject("Scripting.Dictionary")
dictionary.CompareMode = 1 'TextCompare

dim values(2)
values(0) = "one"
values(1) = "two"
values(2) = "one"
' add values as keys
for each value in values
    if not dictionary.exists(value) then
         dictionary.add value, null
    end if
next 'value

WScript.Echo Join(values, " ")
'output just the keys
WScript.Echo Join(dictionary.keys(), " ")

one two one
one two

's ArrayList is an alternative.

Community
  • 1
  • 1
0

If I understood you correctly you only want the value for the attribute "DisplayName" for each key. The code for that is much shorter:

    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE 
    strComputer = "." 
    strKey = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 
    strEntry1a = "DisplayName" 

    Set objReg = GetObject("winmgmts://" & strComputer & _ 
     "/root/default:StdRegProv") 

    objReg.EnumKey HKLM, strKey, subKeys

    WScript.Echo "Installed Applications:" 

    For Each subKey In subKeys
        If (IsNull( subKey) = false) Then
            strNewKey = strKey & "\" & subKey
            objReg.GetStringValue HKLM, strNewKey, strEntry1a, szValue
        If (IsNull(szValue) = false) Then
            wScript.Echo szValue
        End If
    End If
    Next 

EDIT: After reading your code again I think you want to do the following:

  • Check if there is a value for attribute "DisplayName" - if so: take it
  • If it isn't there check if there is an attribute "QuietDisplayName" and take that
  • If that one isn't there neither - skip that entry
  • Get install date and display version if they exist
  • Concatenate them to the DisplayName or QuietDisplayName

Here is the corrected code:

    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE 
    strComputer = "." 
    strKey = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 
    strEntry1a = "DisplayName" 
    strEntry1b = "QuietDisplayName" 
    strEntry2 = "InstallDate" 
    strEntry3 = "DisplayVersion" 

    Set objReg = GetObject("winmgmts://" & strComputer & _ 
     "/root/default:StdRegProv") 

    objReg.EnumKey HKLM, strKey, subKeys

    WScript.Echo "Installed Applications" 

    For Each subKey In subKeys
        entry = ""
        If (IsNull( subKey) = false) Then
            strNewKey = strKey & "\" & subKey
            objReg.GetStringValue HKLM, strNewKey, strEntry1a, szValue
        If (IsNull(szValue) = false) Then
            entry = szValue
            else
                objReg.GetStringValue HKLM, strNewKey, strEntry1b, szValue
                If (IsNull(szValue) = false) Then
                    entry = szValue
                End If
        End If

            objReg.GetStringValue HKLM, strNewKey , _ 
            strEntry2, installDate

            objReg.GetStringValue HKLM, strNewKey , _ 
            strEntry3, displayVersion

            If(entry <> "") Then
                If (isNull(installDate) = false) Then
                    entry = entry & ";" & installDate
                Else
                    entry = entry & ";" & "-"
                End If

                If (isNull(displayVersion) = false) Then
                    entry = entry & ";" & displayVersion
                Else
                    entry = entry & ";" & "-"
                End If
                WScript.Echo entry
            End If

    End If
    Next
JanTheGun
  • 2,165
  • 17
  • 25