"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