Re: Reading vs. Checking Registry Key



Can you help me with the usage of the function? In "MY CODE" below, I do a
simple test for sKey1. When I run it, it always returns "exists", even if I
change the skey1 variable to a non-existant key.

MY CODE
***************

Dim WshShell, bKey, sKey, sValueBad, sValueGood
Set WshShell = WScript.CreateObject("WScript.Shell")
sKey1 = "HKLM\Software\Webroot\Enterprise\CommAgent\Su"

If KeyExists(sKey1) = True Then
WScript.Echo "exists"
Else
WScript.Echo "Key does not exist"
End If



Function KeyExists(sKeyPath)
keyExists= false: if (sKeyPath="") then exit function

on error resume next
createobject("wscript.shell").regRead sKeyPath

select case err
case 0: keyExists= true

case &h80070002: dim sErrMsg
sErrMsg= replace(err.description, sKeyPath, "")
err.clear

createobject("wscript.shell").regRead "HKEY_ERROR\"
keyExists= not (sErrMsg=replace(err.description, _
"HKEY_ERROR\", ""))

case else: keyExists= false
end select
on error goto 0
End function






"Joe Earnest" <jearnest3-SPAM@xxxxxxxxxxxxx> wrote in message
news:%23MFJmtvPFHA.3668@xxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> "scott" <sbailey@xxxxxxxxxxxxxxx> wrote in message
> news:OwsGkGvPFHA.2748@xxxxxxxxxxxxxxxxxxxxxxx
>>I can read the below key value fine, but I need to be able to check and
>>see
>>if the key exists before reading it to avoid script errors.
>>
>> To my knowledge, there are only 3 methods available in the WSH Library to
>> access registry values: regread, regwrite and regdelete.
>>
>> Anyone have a trick to testing if a key exists before taking action on
>> it?
>>
>> SCRIPT
>> =========
>> WshShell.RegRead("HKLM\Software\Webroot\Enterprise\CommAgent\TheKey")
>
> If you don't want to use WMI, here's my version of a trick that Torgeir
> Bakken taught me, that he attributes to Walter Zachary. It's an
> error-trapped attempt to read a key's default value, distinguishing
> between
> the error description for no default value assigned and key not found,
> which
> have the same error number, but different descriptions. The way it's set
> up, it's applicable to any language version. The function below doesn't
> test syntax and expects the key path to have a final backslash, in order
> for
> the test to be valid.
>
> ---
> function KeyExists (sKeyPath)
> keyExists= false: if (sKeyPath="") then exit function
>
> on error resume next
> createobject("wscript.shell").regRead sKeyPath
>
> select case err
> case 0: keyExists= true
>
> case &h80070002: dim sErrMsg
> sErrMsg= replace(err.description, sKeyPath, "")
> err.clear
>
> createobject("wscript.shell").regRead "HKEY_ERROR\"
> keyExists= not (sErrMsg=replace(err.description, _
> "HKEY_ERROR\", ""))
>
> case else: keyExists= false
> end select
> on error goto 0
> end function
> ---
>
> Joe Earnest
>
>
>


.



Relevant Pages

  • Re: xcopy date problem
    ... together into US format if the shortdate setting in the registry is ... Set wshShell = CreateObject ... 'Reads the shortdate format from the system registry ... Dim wshShell, dtmDate, strKey, strDateFormat, arrDate ...
    (microsoft.public.windows.server.scripting)
  • How to pass values to a vbscript function?
    ... I pulled the following client-side script from an ASP page. ... Dim WshShell, oExec, HostIP ... Set WshShell = CreateObject ... Set oExec = WshShell.Exec("C:\Program Files\Netop Remote ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Login/Logout auditing
    ... Dim WSHShell ... Dim WSHNetW ... Set WSHShell = WScript.CreateObject ... and for logoff just change the Logged on to Logged Off... ...
    (microsoft.public.backoffice.smallbiz2000)
  • Re: Which Regsitry Settings Are Used by Windows 2003?
    ... Dim WshShell ... Set WshShell = WScript.CreateObject ... Microsoft MVP [Windows] ... | that 'regedit' displays the registry keys and values, ...
    (microsoft.public.windows.server.general)
  • Re: Which Regsitry Settings Are Used by Windows 2003?
    ... Dim WshShell ... Set WshShell = WScript.CreateObject ... Microsoft MVP [Windows] ... | that 'regedit' displays the registry keys and values, ...
    (microsoft.public.win2000.registry)