none
Registry key check failing in Win 2003, works fine in other OSs ?

    Question

  • Hi,

    My script does the below;

    -takes a registry key input as a parameter from user with option k

    -checks if the registry entered exists than exports that into a text file.

    -if registry key doesn't exist than says: unable to open reg.......

    The script below works fine in almost all operating systems except in Windows 2003.

    The KeyExists function always returns a false result even though the entered key is correct and only in Win 2003 OS.

    Set colNamedArguments = WScript.Arguments.Named
    Set winsh = CreateObject("WScript.Shell")
    StrFileName = "C:\reg.log"
    
    If colNamedArguments.Exists("k") Then
      StrKey = colNamedArguments.Item("k")
      If KeyExists(StrKey) = True Then
        Wscript.echo "Scanning entered registry: " & StrKey & "..."
        StrKey = trim(StrKey)
        winsh.run "regedit /e " & chr(34) & StrFileName & chr(34) & " """& StrKey &"""",0,True
      Else
        Wscript.echo "Unable to open reg entered: " & StrKey
      End If
    End If
    
    Function KeyExists(ByVal key)
     If right(key, 1) <> "\" Then key = key & "\"
     On Error Resume Next
     winsh.RegRead trim(key)
     If Err.Number <> 0 Then
       keyExists = False
     Else
       keyExists = True
     End If
    End Function

    Any help please ?

    Thanks!

    Thursday, July 25, 2013 12:43 PM

Answers

  • Hi,

    Here's a working example:

    'Declare constants
    Const HKCU = &H80000001 'HKEY_CURRENT_USER
    const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    
    'Check for key 
    If keyExists("7-Zip", "Software\", HKLM) Then
    	MsgBox "KEY FOUND"
    Else
    	MsgBox "KEY MISSING"
    End If
    
    
    Function keyExists(strKeyName, strKeyPath, regCategory)
    
         retVal = false
         strComputer = "."
         Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
       
         oReg.EnumKey regCategory, strKeyPath, arrSubKeys
         
         For Each subkey In arrSubKeys
              If strKeyName = subkey Then
                   retVal = true
                   Exit For
              End If
         Next
         keyExists = retVal
    	 
    End Function

    EDIT: Here's a good reference:

    http://www.activexperts.com/network-monitor/windowsmanagement/adminscripts/registry/

    EDIT2: I wish I could remember where I stole that keyExists function from so I could give credit where credit's due, but I can't.


    Don't retire TechNet!



    • Edited by Mike Laughlin Thursday, July 25, 2013 4:04 PM asdf
    • Marked as answer by Subu88 Monday, July 29, 2013 11:52 AM
    Thursday, July 25, 2013 3:58 PM

All replies

  • What key?  YOU need to report the exact error and not just assume all errors are that the key does not exist.

    ¯\_(ツ)_/¯

    Thursday, July 25, 2013 1:16 PM
  • Here - try this and you will see that it works correctly on all versions of Windows.

    KeyExists "HKLM\Software\"
    Function KeyExists( regkey)
    	 Set shell = CreateObject("WScript.Shell")
    	 On Error GoTo 0
    	 keyVal = shell.RegRead(regkey)
    	 If Err.Number <> 0 Then
    		WScript.Echo Err.Message
    		KeyExists = False
    	 Else
    		KeyExists = True
    	 End If
    End Function



    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, July 25, 2013 1:33 PM xxxx
    Thursday, July 25, 2013 1:33 PM
  • Hello,

    Whatever registry key may I enter, its giving out the "Unable to open reg entered: <registry Key>"

    And this is happening only in Windows 2003.

    The results are the same after using the function given by you.

    For example;

    output when I use my original Function KeyExists;

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

    Unable to open reg entered: HKEY_LOCAL_MACHINE\SOFTWARE\

    output when I use your given function;

    C:\Documents and Settings\Administrator\Desktop\LS\t.vbs(25, 3) WshShell.RegRead
    : Unable to open registry key "HKEY_LOCAL_MACHINE\SOFTWARE\" for reading.

    The above both the outputs are the results of the below command line input;

    cscript t.vbs /k:"HKEY_LOCAL_MACHINE\SOFTWARE"

    Am I missing something ?

    Thanks for your time!



    • Edited by Subu88 Thursday, July 25, 2013 2:31 PM Added
    Thursday, July 25, 2013 2:27 PM
  • You did not follow my instructions.  Run my script exactly as it is. Copy and paste it. DO NOT change it.  It will work.  You changed it and created the same error that was in your original script.


    ¯\_(ツ)_/¯

    Thursday, July 25, 2013 2:38 PM
  • I cannot hard code the Registry key while calling the function KeyExists as per the requirements.

    And below is the code I am trying to make work with your modified function but still same results.

    Set colNamedArguments = WScript.Arguments.Named
    Set winsh = CreateObject("WScript.Shell")
    StrFileName = "C:\reg.log"
    
    If colNamedArguments.Exists("k") Then
      StrKey = colNamedArguments.Item("k")
      If KeyExists(StrKey) = True Then
        Wscript.echo "Scanning entered registry: " & StrKey & "..."
        StrKey = trim(StrKey)
        winsh.run "regedit /e " & chr(34) & StrFileName & chr(34) & " """& StrKey &"""",0,True
      Else
        Wscript.echo "Unable to open reg entered: " & StrKey
      End If
    End If
    
    
    Function KeyExists( regkey)
    	 Set shell = CreateObject("WScript.Shell")
    	 On Error GoTo 0
    	 keyVal = shell.RegRead(regkey)
    	 If Err.Number <> 0 Then
    		WScript.Echo Err.Message
    		KeyExists = False
    	 Else
    		KeyExists = True
    	 End If
    End Function

    output is:

    C:\Documents and Settings\Administrator\Desktop\LS\t.vbs(20, 3) WshShell.RegRead
    : Unable to open registry key "HKEY_LOCAL_MACHINE\SOFTWARE" for reading.

    Also tried only the code you gave with the command cscript t.vbs and the output is:

    C:\Documents and Settings\Administrator\Desktop\LS\t.vbs(5, 3) WshShell.RegRead:
     Unable to open registry key "HKLM\Software\" for reading.

    Thanks for all your help!


    • Edited by Subu88 Thursday, July 25, 2013 3:37 PM Added
    Thursday, July 25, 2013 3:05 PM
  • Till now I have tried the code in Win 2003 64bit, and now I tried my original code in Win 2003 32bit and it works till "HKEY_LOCAL_MACHINE\Software" but if i give "HKEY_LOCAL_MACHINE\Software\Microsoft" its not working and coming out saying the same as earlier.

    Now, this problem is getting more critical.I don't know what is going wrong and how to proceed. :-(

    Please help me guys!!

    Thursday, July 25, 2013 3:47 PM
  • Hi,

    Here's a working example:

    'Declare constants
    Const HKCU = &H80000001 'HKEY_CURRENT_USER
    const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    
    'Check for key 
    If keyExists("7-Zip", "Software\", HKLM) Then
    	MsgBox "KEY FOUND"
    Else
    	MsgBox "KEY MISSING"
    End If
    
    
    Function keyExists(strKeyName, strKeyPath, regCategory)
    
         retVal = false
         strComputer = "."
         Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
       
         oReg.EnumKey regCategory, strKeyPath, arrSubKeys
         
         For Each subkey In arrSubKeys
              If strKeyName = subkey Then
                   retVal = true
                   Exit For
              End If
         Next
         keyExists = retVal
    	 
    End Function

    EDIT: Here's a good reference:

    http://www.activexperts.com/network-monitor/windowsmanagement/adminscripts/registry/

    EDIT2: I wish I could remember where I stole that keyExists function from so I could give credit where credit's due, but I can't.


    Don't retire TechNet!



    • Edited by Mike Laughlin Thursday, July 25, 2013 4:04 PM asdf
    • Marked as answer by Subu88 Monday, July 29, 2013 11:52 AM
    Thursday, July 25, 2013 3:58 PM
  • I cannot hard code the Registry key while calling the function KeyExists as per the requirements.

    And below is the code I am trying to make work with your modified function but still same results.

    Set colNamedArguments = WScript.Arguments.Named
    Set winsh = CreateObject("WScript.Shell")
    StrFileName = "C:\reg.log"
    
    If colNamedArguments.Exists("k") Then
      StrKey = colNamedArguments.Item("k")
      If KeyExists(StrKey) = True Then
        Wscript.echo "Scanning entered registry: " & StrKey & "..."
        StrKey = trim(StrKey)
        winsh.run "regedit /e " & chr(34) & StrFileName & chr(34) & " """& StrKey &"""",0,True
      Else
        Wscript.echo "Unable to open reg entered: " & StrKey
      End If
    End If
    
    
    Function KeyExists( regkey)
    	 Set shell = CreateObject("WScript.Shell")
    	 On Error GoTo 0
    	 keyVal = shell.RegRead(regkey)
    	 If Err.Number <> 0 Then
    		WScript.Echo Err.Message
    		KeyExists = False
    	 Else
    		KeyExists = True
    	 End If
    End Function

    output is:

    C:\Documents and Settings\Administrator\Desktop\LS\t.vbs(20, 3) WshShell.RegRead
    : Unable to open registry key "HKEY_LOCAL_MACHINE\SOFTWARE" for reading.

    Also tried only the code you gave with the command cscript t.vbs and the output is:

    C:\Documents and Settings\Administrator\Desktop\LS\t.vbs(5, 3) WshShell.RegRead:
     Unable to open registry key "HKLM\Software\" for reading.

    Thanks for all your help!


    I am trying to show you where your error is.  When I ask to run the script I posted I mean just that script in the file and nothing else.  Every time you change it you are creating the same exact error.  Put only my code in a file.  This code.  I am adding one messag eto it so you can se better wht is happening.

    Put the following in a file with a CVBS extension. DO NOT Change or ad to it.  Just run it.

    KeyExists "HKLM\Software\"
    Function KeyExists( regkey)
    	 Set shell = CreateObject("WScript.Shell")
    	 On Error GoTo 0
    	 keyVal = shell.RegRead(regkey)
    	 If Err.Number <> 0 Then
    		WScript.Echo Err.Message
    		KeyExists = False
    	 Else
                    Wscript.Echo "Key was found"
    		KeyExists = True
    	 End If
    End Function


    ¯\_(ツ)_/¯

    Thursday, July 25, 2013 4:30 PM
  • Till now I have tried the code in Win 2003 64bit, and now I tried my original code in Win 2003 32bit and it works till "HKEY_LOCAL_MACHINE\Software" but if i give "HKEY_LOCAL_MACHINE\Software\Microsoft" its not working and coming out saying the same as earlier.

    Now, this problem is getting more critical.I don't know what is going wrong and how to proceed. :-(

    Please help me guys!!

    If you follow my instructions you will eventually see what you are doing wrong but first you must figure out how to run that simple script.  If you annot make it work then we will never be able to show you what you are doing wrong.  It has to do with the particular structure of the keys and variations on how RegRead treats a key.  Once you understand it will never be an issue. Until you see what I am trying to show you will be forever having this issue if you continue to use RegRead.

    Just run the scrip and let me know if it has run.  Copy and paste it DO NOT TRY TO RETYPE IT>


    ¯\_(ツ)_/¯

    Thursday, July 25, 2013 4:33 PM
  • Mike - that is excellent however the same issue will occur if the OP does not see how this shows him how the registry is working and why it seems to fail on one system and not another. It has nothing to do with WS2003 or with 32/64 bits. It has to do with the default structure of a key and hwo we address a key.


    ¯\_(ツ)_/¯

    Thursday, July 25, 2013 4:35 PM
  • Mike - that is excellent however the same issue will occur if the OP does not see how this shows him how the registry is working and why it seems to fail on one system and not another. It has nothing to do with WS2003 or with 32/64 bits. It has to do with the default structure of a key and hwo we address a key.


    ¯\_(ツ)_/¯


    Yep, we shall see.

    Don't retire TechNet!

    Thursday, July 25, 2013 5:20 PM
  • Thanks for all the explanations.

    It's really good to learn from you guys as always.

    Jrv: I just copied your code as you said as t.vbs and this is the output when I run it;

    C:\Documents and Settings\Administrator\Desktop\t.vbs(5, 3) WshShell.RegRead: Un
    able to open registry key "HKLM\Software\" for reading.


    Please have your feedback on this.
    • Edited by Subu88 Friday, July 26, 2013 7:46 AM Added
    Friday, July 26, 2013 7:45 AM
  • Thanks for all the explanations.

    It's really good to learn from you guys as always.

    Jrv: I just copied your code as you said as t.vbs and this is the output when I run it;

    C:\Documents and Settings\Administrator\Desktop\t.vbs(5, 3) WshShell.RegRead: Un
    able to open registry key "HKLM\Software\" for reading.


    Please have your feedback on this.

    I can run that on WS2003 so I am sure you still didn't run it correctly.  It is impossible that that key is unreadable.  This post also shows that you ran different code than the first time so I am not wondering what you my have done to make the script unusable.

    I know you did something to damage the code because the numbers that the error message have printed out are wrong.  What did you do to alter the code?  Can you please post the exact file contents that you have run.

    Either something is wrong with your copy and past or the VBScript engine on your server is corrupted which I doubt because WS2003 uses VBScript for many things.

    If that key is unreadable you would have issues with the OS.


    ¯\_(ツ)_/¯

    Friday, July 26, 2013 1:26 PM
  • I can run that on WS2003 so I am sure you still didn't run it correctly.  It is impossible that that key is unreadable.  This post also shows that you ran different code than the first time so I am not wondering what you my have done to make the script unusable.

    I know you did something to damage the code because the numbers that the error message have printed out are wrong.  What did you do to alter the code?  Can you please post the exact file contents that you have run.

    Either something is wrong with your copy and past or the VBScript engine on your server is corrupted which I doubt because WS2003 uses VBScript for many things.

    If that key is unreadable you would have issues with the OS.


    ¯\_(ツ)_/¯

    I did as you instructed, and for sure i didn't changed anything in the code given by you while running.

    This is the file content that I pasted in t.vbs file and ran;

    KeyExists "HKLM\Software\"
    Function KeyExists( regkey)
    	 Set shell = CreateObject("WScript.Shell")
    	 On Error GoTo 0
    	 keyVal = shell.RegRead(regkey)
    	 If Err.Number <> 0 Then
    		WScript.Echo Err.Message
    		KeyExists = False
    	 Else
                    Wscript.Echo "Key was found"
    		KeyExists = True
    	 End If
    End Function

    And as said earlier, in win2003 64bit the code is throwing the error. And in win2003 32bit the code is working fine but throwing same error when I give KeyExists "HKLM\Software\Microsoft\"

    I tired your exact code also in a XP machine and there too it errors out.

    I don't know what I am doing wrong but I am doing exactly as per your instructions. :-(

    Thanks!!



    • Edited by Subu88 Friday, July 26, 2013 1:51 PM Added
    Friday, July 26, 2013 1:46 PM
  • Hmm, that code is also failing for me on WS2003x64 with the same error message. Try mine, it seems to work.

    EDIT:

    Tested this on several OS versions:

    WS2003 R2(x86 and x64): Fails with above error message

    WS2008 SP2 x64: Works

    WS2008 R2: Works

    WS2012: Works

    Win7 x64: Works


    Don't retire TechNet!


    Friday, July 26, 2013 3:50 PM
  • Put this in a file with a VBS extension.

    Be sure the file is saved as an ANSI file and NOT Unicode.  Only use notepad to create the files and save it.

    Set shell = CreateObject("WScript.Shell")
    keyVal = shell.RegRead("HKLM\Software\")

    After doing that try this and see what happens.  At a command prompt paste this and tell us what you see:

     reg query hklm\software


    ¯\_(ツ)_/¯

    Friday, July 26, 2013 5:04 PM
  • Here's my output (from WS2003x64):

    I might not get back to this thread for a while, so running with this is up to the OP.


    Don't retire TechNet!


    Friday, July 26, 2013 5:15 PM
  • Incidentally 80070002 = Win32 error 2 = ERROR_FILE_NOT_FOUND.

    Bill

    Friday, July 26, 2013 5:19 PM
  • you need to run from SystWow64 if you are on 64 bit.  There are very few WS2003 64 so I didn't think of that.

    C:\Windows\SysWow64\cscript t.vbs

    This should work.  The issue with 64 bits is that you cannot see the keys under some circumstances if you are running in the wrong mode.

    To be sure also try

    c:\windows\System32\cscript t.vbs


    ¯\_(ツ)_/¯

    Friday, July 26, 2013 5:37 PM
  • I just tested both of the above on my 64 bits 2003 system and they both work with no issues.

    Perhaps you system is corrupted.

    Does Mike's WMI method work?


    ¯\_(ツ)_/¯

    Friday, July 26, 2013 5:39 PM
  • Hi,

    Here's a working example:

    'Declare constants
    Const HKCU = &H80000001 'HKEY_CURRENT_USER
    const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    
    'Check for key 
    If keyExists("7-Zip", "Software\", HKLM) Then
    	MsgBox "KEY FOUND"
    Else
    	MsgBox "KEY MISSING"
    End If
    
    
    Function keyExists(strKeyName, strKeyPath, regCategory)
    
         retVal = false
         strComputer = "."
         Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
       
         oReg.EnumKey regCategory, strKeyPath, arrSubKeys
         
         For Each subkey In arrSubKeys
              If strKeyName = subkey Then
                   retVal = true
                   Exit For
              End If
         Next
         keyExists = retVal
    	 
    End Function

    EDIT: Here's a good reference:

    http://www.activexperts.com/network-monitor/windowsmanagement/adminscripts/registry/

    EDIT2: I wish I could remember where I stole that keyExists function from so I could give credit where credit's due, but I can't.


    Don't retire TechNet!



    Thanks a lot!!!

    I used the function in my code with a slight modification and it is working in almost all the Win platforms as expected.

    This is great!!! :-)

    Monday, July 29, 2013 11:54 AM
  • Thanks to all of you guys !!!!

    Appreciate that!!But, still in a dilemma as what is the exact reason of the inappropriate behavior for RegRead

    across different platforms.

    Will do as jrv said and return back here .

    Thanks Again!

    Monday, July 29, 2013 12:00 PM
  • you need to run from SystWow64 if you are on 64 bit.  There are very few WS2003 64 so I didn't think of that.

    C:\Windows\SysWow64\cscript t.vbs

    This should work.  The issue with 64 bits is that you cannot see the keys under some circumstances if you are running in the wrong mode.

    To be sure also try

    c:\windows\System32\cscript t.vbs


    ¯\_(ツ)_/¯

    Tried in Win2003 x64.The SysWow64 path works, but the System32 fails.

    C:\Documents and Settings\Administrator\Desktop>C:\Windows\SysWow64\cscript t.vbs
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
    
    Key was found
    
    C:\Documents and Settings\Administrator\Desktop>c:\windows\System32\cscript t.vbs
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
    
    C:\Documents and Settings\Administrator\Desktop\t.vbs(5, 3) WshShell.RegRead: Unable to open registry key "HKLM\Software
    \" for reading.

    I tired to attach a screenshot for the command line output here by copying and pasting it here, which is pasting but not coming up after I submit ?

    Thanks!



    • Edited by Subu88 Monday, July 29, 2013 1:33 PM Added
    Monday, July 29, 2013 1:07 PM