Windows registry_key > only_if/not_if > registry_value_exists help

Hi all,

Trying to implement “only_if {registry_value_exists?()}” or “not_if {registry_value_exists?()}” and it’s not working. Without the not_if or only_if, the cookbook works perfectly, but I don’t want it to run if the value is already set properly. This is what I have to disable IEC for Admins. Basically, only change it to ‘0’ if it’s currently ‘1’ :

registry_key ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}’ do
architecture :x86_64
values [{ :name => ‘IsInstalled’, :type => :dword, :data => ‘0’},

    only_if {registry_value_exists?(
            'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}',
            { :name => 'IsInstalled', :type => :dword, :data => '1' } )}

    action :create


Thanks a lot.

I saw this question after you first posted it, played a bit with registry_value_exists? and came to the conclusion that it was just plain broken. Today I set out to fix it, and I think stumbled upon some “archaeology” that may help you work around your problem now (if you haven’t already).

What you really want to use is registry_data_exists? and NOT registry_value_exists?. registry_data_exists? does what you would expect of registry_value_exists?. Personally I think this is a design and documentation flaw and you can weigh in on that if you like in this github issue. However, for now, I think you will have better luck using registry_data_exists?.

The one final thing to note is that you need to remove the quotes around the :data since you are dealing with a dword. So it should be:

            'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}',
            { :name => 'IsInstalled', :type => :dword, :data => 1 } )