none
Need Help With VBS Script (Getting Value From Registry) RRS feed

  • Question

  • Hello!

     

    I need assistance with coniguring a script to check for value data in the registry. I checked around but did not see how to integrate the functions into my script. I got the function to check for the key working.

     

    Here is my script:

     

    'Bind to the Windows Scripting Host Shell object
    Set Shell = CreateObject("Wscript.Shell")

    'iTunes
    DisplayName = "iTunes"
    DisplayVersion = "10.1.1.4"

    'Find iTunes Version
    If RegExistValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0C682623-8F66-46A8-B9B3-93FE1E66A001}\" & DisplayName = (DisplayVersion) Then

        wscript.echo "iTunes Version 10.1.1.4 found"
    else
        wscript.echo "iTunes was not found"

    end if

    Function RegExistValue(ValuetoCheck)

    If Mid(ValuetoCheck, Len(ValuetoCheck)) = "\" Then ValuetoCheck = Left(ValuetoCheck,Len(ValuetoCheck)-1)
    Set wshshell = CreateObject("WScript.Shell")
    bKey = WshShell.RegRead(ValuetoCheck)
    If Err.Number =0 Then
        'msgbox ValuetoCheck & " | Exists " & Bkey
        RegExistValue = True   
    Else
        'msgbox ValuetoCheck & " | Does Not Exist"
        RegExistValue = False
    End If
    End Function

     

     

    Basically, I want the script to look into HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0C682623-8F66-46A8-B9B3-93FE1E66A001}\ find the value 'DisplayVerison' and verify the data is 10.1.1.4.

     

    Thank you



    Thursday, March 31, 2011 2:39 AM

Answers

  • I think I see unbalanced parentheses in your script (the If RegExistValue statement). There are two entry names in the subkey, "DisplayName" and "DisplayVersion". If the value of "DisplayName" is "iTunes, you want to verify that "DisplayVersion" is "10.1.1.4". I would suggest:

    Option Explicit
    
    Dim objShell, strKey, strName1, strName2, strEntry1, strEntry2, strValue1, strValue2
    
    Set objShell = CreateObject("Wscript.Shell")
    
    strKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0C682623-8F66-46A8-B9B3-93FE1E66A001}"
    strEntry1 = "DisplayName"
    strEntry2 = "DisplayVersion"
    
    On Error Resume Next
    strValue1 = objShell.RegRead(strKey & "\" & strEntry1)
    If (Err.Number = 0) Then
      On Error GoTo 0
      If (strValue1 = "iTunes") Then
        strValue2 = objShell.RegRead(strKey & "\" & strEntry2)
        If (strValue2 = "10.1.1.4") Then
          Wscript.Echo "iTunes version 10.1.1.4 found"
        Else
          Wscript.Echo "iTunes is version: " & strValue2
        End If
      Else
        Wscript.Echo "iTunes was not found"
      End If
    Else
      Wscript.Echo "Registry key not found"
    End If

     

    The error trapping is in case the key does not exist at all. That handles the "if exists" condition.

     


    Richard Mueller - MVP Directory Services
    • Marked as answer by Aizen Thursday, March 31, 2011 4:14 AM
    Thursday, March 31, 2011 3:26 AM
    Moderator

All replies

  • I think I see unbalanced parentheses in your script (the If RegExistValue statement). There are two entry names in the subkey, "DisplayName" and "DisplayVersion". If the value of "DisplayName" is "iTunes, you want to verify that "DisplayVersion" is "10.1.1.4". I would suggest:

    Option Explicit
    
    Dim objShell, strKey, strName1, strName2, strEntry1, strEntry2, strValue1, strValue2
    
    Set objShell = CreateObject("Wscript.Shell")
    
    strKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0C682623-8F66-46A8-B9B3-93FE1E66A001}"
    strEntry1 = "DisplayName"
    strEntry2 = "DisplayVersion"
    
    On Error Resume Next
    strValue1 = objShell.RegRead(strKey & "\" & strEntry1)
    If (Err.Number = 0) Then
      On Error GoTo 0
      If (strValue1 = "iTunes") Then
        strValue2 = objShell.RegRead(strKey & "\" & strEntry2)
        If (strValue2 = "10.1.1.4") Then
          Wscript.Echo "iTunes version 10.1.1.4 found"
        Else
          Wscript.Echo "iTunes is version: " & strValue2
        End If
      Else
        Wscript.Echo "iTunes was not found"
      End If
    Else
      Wscript.Echo "Registry key not found"
    End If

     

    The error trapping is in case the key does not exist at all. That handles the "if exists" condition.

     


    Richard Mueller - MVP Directory Services
    • Marked as answer by Aizen Thursday, March 31, 2011 4:14 AM
    Thursday, March 31, 2011 3:26 AM
    Moderator
  • Thank you so much! This works perfect and I was able to include it in my script I'm using to install iTunes.
    Thursday, March 31, 2011 4:14 AM