Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

verifying a registry key exists

98 views
Skip to first unread message

Mark

unread,
Feb 9, 2004, 3:29:39 PM2/9/04
to
does anyone know of a good way to verify if a a registry keys exists using
WMI? I'm not aware a clean way to do this.

Any suggestions would be great.

thanks.

Mark


Torgeir Bakken (MVP)

unread,
Feb 9, 2004, 4:28:58 PM2/9/04
to
Mark wrote:

> does anyone know of a good way to verify if a a registry keys exists using
> WMI? I'm not aware a clean way to do this.

Hi

Two functions below, one for testing the existence of a key, the other
for a value...


Const HKLM = &H80000002
Set oReg =GetObject("Winmgmts:root\default:StdRegProv")

sKeyPath = "Software\Microsoft\Windows\CurrentVersion"
sValue = "ProgramFilesDir"

If RegKeyExists(HKLM, sKeyPath) Then
WScript.Echo "Key exists"
Else
WScript.Echo "Key does not exist"
End If

If RegValueExists(HKLM, sKeyPath, sValue) Then
WScript.Echo "Value exists"
Else
WScript.Echo "Value does not exist"
End If


Function RegKeyExists(sHive, sRegKey)
Dim aValueNames, aValueTypes
If oReg.EnumValues(sHive, sKeyPath, aValueNames, aValueTypes) = 0 Then
RegKeyExists = True
Else
RegKeyExists = False
End If
End Function


Function RegValueExists(sHive, sRegKey, sRegValue)
Dim aValueNames, aValueTypes
RegValueExists = False ' init value
If oReg.EnumValues(sHive, sKeyPath, aValueNames, aValueTypes) = 0 Then
If IsArray(aValueNames) Then
For i = 0 To UBound(aValueNames)
If LCase(aValueNames(i)) = LCase(sRegValue) Then
RegValueExists = True
End If
Next
End If
End If
End Function

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


0 new messages