Windows Vista Forums

Help with VBScripts
  1. #1


    Matthew Guest

    Help with VBScripts

    Run from the command line CheckREGKeys.vbs. The script seems to work
    and least for not finding a key or a value but no matter what valid
    key I use the script all ways tells me its invalid

    cscript CheckREGKeys.vbs HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft
    \Windows\WindowsUpdate

    Any ideas?

    All I am trying to do is to see if a key/value exists and then see if
    that value is equal to some specified value.



    [code= Save as CheckREGKeys.vbs]
    '************************************
    '* Registry Item Exists (Function)
    '* Returns a value (true / false)
    '************************************
    'This function checks to see if a passed registry key/value exists,
    and
    'returns true if it does
    'Original Source http://www.tek-tips.com/faqs.cfm?fid=5864
    'Extended by Matthew Kruer to handel external arguments.
    '
    'Requirements: The registry key/value you are looking for
    (RegistryItem)
    'Note: RegistryItem MUST end in a backslash (\) if you are looking for
    a key
    ' RegistryItem's without a backslash (\) will assume you are
    looking for a value
    option explicit
    Dim objArgs, strArg
    Set objArgs = WScript.Arguments
    If (WScript.Arguments.Count = 1) Then
    Call RegistryItemExists (objArgs(0))
    WScript.Quit
    Else
    WScript.Echo "USAGE: CheckREGKeys.vbs keyname"
    WScript.Quit
    End If

    '************************************
    '* Registry Item Exists (Function)
    '* Returns a value (true / false)
    '************************************
    'This function checks to see if a passed registry key/value exists,
    and
    'returns true if it does
    '
    'Requirements: The registry key/value you are looking for
    (RegistryItem)
    'Note: RegistryItem MUST end in a backslash (\) if you are looking for
    a key
    ' RegistryItem's without a backslash (\) will assume you are
    looking for a value
    Function RegistryItemExists (RegistryItem)
    'If there isnt the item when we read it, it will return an error, so
    we need to resume
    On Error Resume Next
    WScript.Echo("Looking for Key or Value")
    'Find out if we are looking for a key or a value
    If (Right(RegistryItem, 1) = "\") Then
    'It's a registry key we are looking for
    'Try reading the key
    WScript.Echo("Looking for Key")
    WScript.echo RegistryItem
    WshShell.RegRead RegistryItem
    'Catch the error
    Select Case Err
    'Error Code 0 = 'success'
    Case 0:
    RegistryItemExists = true
    WScript.Echo("1. " & RegistryItem)
    'This checks for the (Default) value existing (but being blank);
    as well as key's not existing at all (same error code)
    Case &h80070002:
    'Read the error description, removing the registry key from
    that description
    ErrDescription = Replace(Err.description, RegistryItem, "")
    wscript.echo ErrDescription
    'Clear the error
    Err.clear

    'Read in a registry entry we know doesn't exist (to create an
    error description for something that doesnt exist)
    WshShell.RegRead "HKEY_ERROR\"

    'The registry key exists if the error description from the
    HKEY_ERROR RegRead attempt doesn't match the error
    'description from our RegistryKey RegRead attempt
    If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\",
    "")) Then
    RegistryItemExists = true
    WScript.Echo("Check Found Key " & RegistryItem)
    Else
    RegistryItemExists = false
    WScript.Echo("Check Failed for Key " & RegistryItem)
    End If
    'Any other error code is a failure code
    Case Else:
    RegistryItemExists = false
    WScript.Echo("Check Failed: Unknown Failure while looking for " &
    RegistryItem)
    End Select
    Else
    'It's a registry value we are looking for
    'Try reading the value
    WScript.Echo("Looking for Value")
    WshShell.RegRead RegistryItem
    'Catch the error
    Select Case Err
    Case 0:
    'Error Code 0 = 'success'
    RegistryItemExists = true
    WScript.Echo("Check Found Value for " & RegistryItem)
    Case Else
    'Any other error code is a failure code
    RegistryItemExists = false
    WScript.Echo("Check could not find a value for " & RegistryItem)
    End Select
    End If
    'Turn error reporting back on
    On Error Goto 0
    End Function
    [code]

      My System SpecsSystem Spec

  2. #2


    Pegasus [MVP] Guest

    Re: Help with VBScripts

    "Matthew" <mkruer@newsgroup> wrote in message
    news:7e34de29-f141-4ccd-8d99-043f675b9b8e@newsgroup

    > Run from the command line CheckREGKeys.vbs. The script seems to work
    > and least for not finding a key or a value but no matter what valid
    > key I use the script all ways tells me its invalid
    >
    > cscript CheckREGKeys.vbs HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft
    > \Windows\WindowsUpdate
    >
    > Any ideas?
    >
    > All I am trying to do is to see if a key/value exists and then see if
    > that value is equal to some specified value.
    >
    > [code= Save as CheckREGKeys.vbs]
    > '************************************
    > '* Registry Item Exists (Function)
    > '* Returns a value (true / false)
    > '************************************
    > 'This function checks to see if a passed registry key/value exists,
    > and
    > 'returns true if it does
    > 'Original Source http://www.tek-tips.com/faqs.cfm?fid=5864
    > 'Extended by Matthew Kruer to handel external arguments.
    > '
    > 'Requirements: The registry key/value you are looking for
    > (RegistryItem)
    > 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
    > a key
    > ' RegistryItem's without a backslash (\) will assume you are
    > looking for a value
    > option explicit
    > Dim objArgs, strArg
    > Set objArgs = WScript.Arguments
    > If (WScript.Arguments.Count = 1) Then
    > Call RegistryItemExists (objArgs(0))
    > WScript.Quit
    > Else
    > WScript.Echo "USAGE: CheckREGKeys.vbs keyname"
    > WScript.Quit
    > End If
    >
    > '************************************
    > '* Registry Item Exists (Function)
    > '* Returns a value (true / false)
    > '************************************
    > 'This function checks to see if a passed registry key/value exists,
    > and
    > 'returns true if it does
    > '
    > 'Requirements: The registry key/value you are looking for
    > (RegistryItem)
    > 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
    > a key
    > ' RegistryItem's without a backslash (\) will assume you are
    > looking for a value
    > Function RegistryItemExists (RegistryItem)
    > 'If there isnt the item when we read it, it will return an error, so
    > we need to resume
    > On Error Resume Next
    > WScript.Echo("Looking for Key or Value")
    > 'Find out if we are looking for a key or a value
    > If (Right(RegistryItem, 1) = "\") Then
    > 'It's a registry key we are looking for
    > 'Try reading the key
    > WScript.Echo("Looking for Key")
    > WScript.echo RegistryItem
    > WshShell.RegRead RegistryItem
    > 'Catch the error
    > Select Case Err
    > 'Error Code 0 = 'success'
    > Case 0:
    > RegistryItemExists = true
    > WScript.Echo("1. " & RegistryItem)
    > 'This checks for the (Default) value existing (but being blank);
    > as well as key's not existing at all (same error code)
    > Case &h80070002:
    > 'Read the error description, removing the registry key from
    > that description
    > ErrDescription = Replace(Err.description, RegistryItem, "")
    > wscript.echo ErrDescription
    > 'Clear the error
    > Err.clear
    >
    > 'Read in a registry entry we know doesn't exist (to create an
    > error description for something that doesnt exist)
    > WshShell.RegRead "HKEY_ERROR\"
    >
    > 'The registry key exists if the error description from the
    > HKEY_ERROR RegRead attempt doesn't match the error
    > 'description from our RegistryKey RegRead attempt
    > If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\",
    > "")) Then
    > RegistryItemExists = true
    > WScript.Echo("Check Found Key " & RegistryItem)
    > Else
    > RegistryItemExists = false
    > WScript.Echo("Check Failed for Key " & RegistryItem)
    > End If
    > 'Any other error code is a failure code
    > Case Else:
    > RegistryItemExists = false
    > WScript.Echo("Check Failed: Unknown Failure while looking for " &
    > RegistryItem)
    > End Select
    > Else
    > 'It's a registry value we are looking for
    > 'Try reading the value
    > WScript.Echo("Looking for Value")
    > WshShell.RegRead RegistryItem
    > 'Catch the error
    > Select Case Err
    > Case 0:
    > 'Error Code 0 = 'success'
    > RegistryItemExists = true
    > WScript.Echo("Check Found Value for " & RegistryItem)
    > Case Else
    > 'Any other error code is a failure code
    > RegistryItemExists = false
    > WScript.Echo("Check could not find a value for " & RegistryItem)
    > End Select
    > End If
    > 'Turn error reporting back on
    > On Error Goto 0
    > End Function
    > [code]
    I use the script below for this type of task. It's shorter and it works.

    'Enumerate all values found in the nominated registry key.
    'http://www.microsoft.com/technet/scriptcenter/guide/sas_reg_famr.mspx?mfr=True
    Const HKLM=&H80000002
    Const REG_SZ = 1
    Const REG_EXPAND_SZ = 2
    Const REG_BINARY = 3
    Const REG_DWORD = 4
    Const REG_MULTI_SZ = 7
    Const sKeyPath = "SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
    Set oReg=GetObject("winmgmts:\\.\root\default:StdRegProv")
    oReg.EnumValues HKLM, sKeyPath, aEntryNames, aValueTypes

    If VarType(aEntryNames) = 1 Then
    WScript.Echo "No values found"
    WScript.Quit
    End If

    For i = 0 To UBound(aEntryNames)
    WScript.Echo "Name: " & aEntryNames(i)
    Select Case aValueTypes(i)
    Case REG_SZ
    oReg.GetStringValue HKLM, sKeyPath, aEntryNames(i),sValue
    WScript.Echo "String=" & sValue
    Case REG_EXPAND_SZ
    oReg.GetExpandedStringValue HKLM, sKeyPath, aEntryNames(i),
    esValue
    WScript.Echo "Expanded String=" & esValue
    Case REG_BINARY
    oReg.GetBinaryValue HKLM, sKeyPath, aEntryNames(i),aValues
    Line = ""
    For Each byteValue In aValues
    Line = Line & byteValue
    Next
    WScript.Echo "Binary=" & Line
    Case REG_DWORD
    oReg.GetDWORDValue HKLM, sKeyPath, aEntryNames(i),dwValue
    WScript.Echo "DWORD=" & dwValue
    Case REG_MULTI_SZ
    oReg.GetMultiStringValue HKLM, sKeyPath, aEntryNames(i), aValues
    Line = ""
    For Each sValue In aValues
    Line = Line & sValue & vbLf
    Next
    WScript.Echo "Multi String=" & Line
    End Select
    Next



      My System SpecsSystem Spec

  3. #3


    Matthew Guest

    Re: Help with VBScripts

    On Oct 21, 10:13*am, "Pegasus [MVP]" <n...@newsgroup> wrote:

    > "Matthew" <mkr...@newsgroup> wrote in message
    >
    > news:7e34de29-f141-4ccd-8d99-043f675b9b8e@newsgroup
    >
    >
    >

    > > Run from the command line CheckREGKeys.vbs. The script seems to work
    > > and least for not finding a key or a value but no matter what valid
    > > key I use the script all ways tells me its invalid
    >

    > > cscript CheckREGKeys.vbs HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft
    > > \Windows\WindowsUpdate
    >

    > > Any ideas?
    >

    > > All I am trying to do is to see if a key/value exists and then see if
    > > that value is equal to some specified value.
    >

    > > [code= Save as CheckREGKeys.vbs]
    > > '************************************
    > > '* Registry Item Exists (Function)
    > > '* Returns a value (true / false)
    > > '************************************
    > > 'This function checks to see if a passed registry key/value exists,
    > > and
    > > 'returns true if it does
    > > 'Original Sourcehttp://www.tek-tips.com/faqs.cfm?fid=5864
    > > 'Extended by Matthew Kruer to handel external arguments.
    > > '
    > > 'Requirements: The registry key/value you are looking for
    > > (RegistryItem)
    > > 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
    > > a key
    > > ' * * *RegistryItem's without a backslash (\) will assume you are
    > > looking for a value
    > > option explicit
    > > Dim objArgs, strArg
    > > Set objArgs = WScript.Arguments
    > > If (WScript.Arguments.Count = 1) Then
    > > *Call RegistryItemExists (objArgs(0))
    > > WScript.Quit
    > > Else
    > > *WScript.Echo "USAGE: CheckREGKeys.vbs keyname"
    > > *WScript.Quit
    > > End If
    >

    > > '************************************
    > > '* Registry Item Exists (Function)
    > > '* Returns a value (true / false)
    > > '************************************
    > > 'This function checks to see if a passed registry key/value exists,
    > > and
    > > 'returns true if it does
    > > '
    > > 'Requirements: The registry key/value you are looking for
    > > (RegistryItem)
    > > 'Note: RegistryItem MUST end in a backslash (\) if you are looking for
    > > a key
    > > ' * * *RegistryItem's without a backslash (\) will assume you are
    > > looking for a value
    > > Function RegistryItemExists (RegistryItem)
    > > *'If there isnt the item when we read it, it will return an error, so
    > > we need to resume
    > > *On Error Resume Next
    > > * WScript.Echo("Looking for Key or Value")
    > > *'Find out if we are looking for a key or a value
    > > *If (Right(RegistryItem, 1) = "\") Then
    > > * *'It's a registry key we are looking for
    > > * *'Try reading the key
    > > WScript.Echo("Looking for Key")
    > > WScript.echo RegistryItem
    > > WshShell.RegRead RegistryItem
    > > * *'Catch the error
    > > * *Select Case Err
    > > * * *'Error Code 0 = 'success'
    > > * * *Case 0:
    > > * * * *RegistryItemExists = true
    > > WScript.Echo("1. " & RegistryItem)
    > > * * *'This checks for the (Default) value existing (but being blank);
    > > as well as key's not existing at all (same error code)
    > > * * *Case &h80070002:
    > > * * * *'Read the error description, removing the registry key from
    > > that description
    > > * * * *ErrDescription = Replace(Err.description, RegistryItem, "")
    > > wscript.echo ErrDescription
    > > * * * *'Clear the error
    > > * * * *Err.clear
    >

    > > * * * *'Read in a registry entry we know doesn't exist (to create an
    > > error description for something that doesnt exist)
    > > * * * *WshShell.RegRead "HKEY_ERROR\"
    >

    > > * * * *'The registry key exists if the error description from the
    > > HKEY_ERROR RegRead attempt doesn't match the error
    > > * * * *'description from our RegistryKey RegRead attempt
    > > * * * *If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\",
    > > "")) Then
    > > * * * * *RegistryItemExists = true
    > > * WScript.Echo("Check Found Key " & RegistryItem)
    > > * * * *Else
    > > * * * * *RegistryItemExists = false
    > > * WScript.Echo("Check Failed for Key " & RegistryItem)
    > > * * * *End If
    > > * * *'Any other error code is a failure code
    > > * * *Case Else:
    > > * * * *RegistryItemExists = false
    > > WScript.Echo("Check Failed: Unknown Failure while looking for " &
    > > RegistryItem)
    > > * *End Select
    > > *Else
    > > * *'It's a registry value we are looking for
    > > * *'Try reading the value
    > > WScript.Echo("Looking for Value")
    > > * *WshShell.RegRead RegistryItem
    > > * *'Catch the error
    > > * *Select Case Err
    > > * * *Case 0:
    > > * * * *'Error Code 0 = 'success'
    > > * * * *RegistryItemExists = true
    > > WScript.Echo("Check Found Value for " & RegistryItem)
    > > * * *Case Else
    > > * * * *'Any other error code is a failure code
    > > * * * *RegistryItemExists = false
    > > WScript.Echo("Check could not find a value for " & RegistryItem)
    > > * *End Select
    > > *End If
    > > *'Turn error reporting back on
    > > *On Error Goto 0
    > > End Function
    > > [code]
    >
    > I use the script below for this type of task. It's shorter and it works.
    >
    > 'Enumerate all values found in the nominated registry key.
    > 'http://www.microsoft.com/technet/scriptcenter/guide/sas_reg_famr.mspx...
    > Const HKLM=&H80000002
    > Const REG_SZ = 1
    > Const REG_EXPAND_SZ = 2
    > Const REG_BINARY = 3
    > Const REG_DWORD = 4
    > Const REG_MULTI_SZ = 7
    > Const sKeyPath = "SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
    > Set oReg=GetObject("winmgmts:\\.\root\default:StdRegProv")
    > oReg.EnumValues HKLM, sKeyPath, aEntryNames, aValueTypes
    >
    > If VarType(aEntryNames) = 1 Then
    > * *WScript.Echo "No values found"
    > * *WScript.Quit
    > End If
    >
    > For i = 0 To UBound(aEntryNames)
    > * *WScript.Echo "Name: " & aEntryNames(i)
    > * * Select Case aValueTypes(i)
    > * * * *Case REG_SZ
    > * * * * * oReg.GetStringValue HKLM, sKeyPath, aEntryNames(i),sValue
    > * * * * * WScript.Echo "String=" & sValue
    > * * * *Case REG_EXPAND_SZ
    > * * * * * oReg.GetExpandedStringValue HKLM, sKeyPath, aEntryNames(i),
    > esValue
    > * * * * * WScript.Echo "Expanded String=" & esValue
    > * * * *Case REG_BINARY
    > * * * * * oReg.GetBinaryValue HKLM, sKeyPath, aEntryNames(i),aValues
    > * * * * *Line = ""
    > * * * * *For Each byteValue In aValues
    > * * * * * * *Line = Line & byteValue
    > * * * * * Next
    > * * * * * WScript.Echo "Binary=" & Line
    > * * * *Case REG_DWORD
    > * * * * * oReg.GetDWORDValue HKLM, sKeyPath, aEntryNames(i),dwValue
    > * * * * * WScript.Echo "DWORD=" & dwValue
    > * * * *Case REG_MULTI_SZ
    > * * * * * oReg.GetMultiStringValue HKLM, sKeyPath, aEntryNames(i), aValues
    > * * * * *Line = ""
    > * * * * * For Each sValue In aValues
    > * * * * * * *Line = Line & sValue & vbLf
    > * * * * * Next
    > * * * * * WScript.Echo "Multi String=" & Line
    > *End Select
    > Next
    Thanks for the follow up. I have come across that particular script
    before. I should be able to modify it. My requirements are a bit more
    convoluted. In a nutshell all the systems were cloned and deployed
    with the incorrect WSUS information if any. I need to verify the
    server in the registry if the key exists, and if it doesn't then i
    need to delete another key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
    \Windows\CurrentVersion\WindowsUpdate. This will force the system to
    register with the WSUS box. I was hoping to make it modular enough
    that i could use it to check other things in the registry. All this
    would be run during the initial logon for the box.

      My System SpecsSystem Spec

  4. #4


    Pegasus [MVP] Guest

    Re: Help with VBScripts


    "Matthew" <mkruer@newsgroup> wrote in message
    news:eae77240-031e-4454-bd95-507ab390b926@newsgroup
    <snip>
    Thanks for the follow up. I have come across that particular script
    before. I should be able to modify it. My requirements are a bit more
    convoluted. In a nutshell all the systems were cloned and deployed
    with the incorrect WSUS information if any. I need to verify the
    server in the registry if the key exists, and if it doesn't then i
    need to delete another key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
    \Windows\CurrentVersion\WindowsUpdate. This will force the system to
    register with the WSUS box. I was hoping to make it modular enough
    that i could use it to check other things in the registry. All this
    would be run during the initial logon for the box.

    =============

    The script I posted can easily be modified to suit your requirement. You can
    use it to read any value of any type in any hive.



      My System SpecsSystem Spec

Help with VBScripts problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Diff in running VBScripts-WMI routuines from WinTaskSch & SQL Agen Yusuf VB Script 0 18 Mar 2010
Create mutiple PCs in the Domain using vbScripts JeporoxAKO VB Script 6 15 Apr 2009
What is the executable associated with running VBScripts? gimme_this_gimme_that VB Script 2 07 Oct 2008
VBScripts in Vista antique1959 Vista General 6 25 Jun 2007