Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby registry checking

I have the following snippet which I am trying to use to validate registry keys (OS is Windows 2008R2 or Win7)

def value_exists?(path,key)
  reg_type = Win32::Registry::KEY_READ 
  Win32::Registry::HKEY_LOCAL_MACHINE.open(path, reg_type) do |reg|
    begin
      regkey = reg[key]
      return true
    rescue
      return false
    end
  end
end

When I do the following 2 commands the output is expected (in my case false is returned) :

puts(value_exists?("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\",'PendingFileRenameOperations'))
puts(value_exists?("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\",'RebootPending'))

When I perform a

puts(value_exists?("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\",'RebootRequired')) 

I get the following error

C:/Ruby187/lib/ruby/1.8/win32/registry.rb:528:in `open': The system cannot find the file specified. (Win32::Registry::Error)
        from C:/Ruby187/lib/ruby/1.8/win32/registry.rb:608:in `open'
        from ./reg2.rb:7:in `value_exists?'
        from ./reg2.rb:21

I don't really understand what to do to make this work. I suspect it might have something to do with the systems being x64 and it failing to find the key in the right location. but I am not sure what I need to do to remedy this.

Thanks for your help in advance!

like image 517
rismoney Avatar asked Nov 26 '12 03:11

rismoney


2 Answers

I figured it out - http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx

reg_type = Win32::Registry::KEY_READ | 0x100

This solved the issue! My guess is that you guys were not testing on x64?

like image 74
rismoney Avatar answered Sep 22 '22 14:09

rismoney


your issue is that following path SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\ does not exist in your registry. Have you checked it using regedit?

You should modify your code to -

def value_exists?(path,key)
    reg_type = Win32::Registry::KEY_READ 
    begin
        Win32::Registry::HKEY_LOCAL_MACHINE.open(path, reg_type) {|reg| regkey = reg[key]}
    rescue
        false
    end
end
like image 38
saihgala Avatar answered Sep 26 '22 14:09

saihgala