# Read windows registry Key values

**URL:** https://discourse.chef.io/t/read-windows-registry-key-values/6355
**Category:** Chef Infra (archive)
**Created:** [February 26, 2015, 6:53pm UTC](https://discourse.chef.io/t/read-windows-registry-key-values/6355 "2015-02-26T18:53:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Sachin\_Gupta](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Sachin\_Gupta](https://discourse.chef.io/u/Sachin_Gupta)
#### Post date: [February 26, 2015, 6:53pm UTC](https://discourse.chef.io/t/read-windows-registry-key-values/6355/1 "2015-02-26T18:53:51Z")

</div>

Hi All,

I am working on a use case where I need to read two registry key values and  
compare there data in a recipe

HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName

HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName

both are having a value name(ComputerName) and value data(may be same or  
different). I need to compare there value data.

Do I need to write like this:; or some other better way??

Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameComp, access) do |reg|  
reg.each{|name, value, keys | compName = keys}  
end  
Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameActive, access) do |reg|  
reg.each{|name, value, keys | compNameActive = keys}  
end

Any idea??

Thanks & Regards,  
Sachin Gupta

---

<div class="post-metadata">

### Author: ![Adam\_Edwards1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/adam_edwards1/32/27_2.png) [@Adam\_Edwards1](https://discourse.chef.io/u/Adam_Edwards1)
#### Post date: [February 26, 2015, 9:29pm UTC](https://discourse.chef.io/t/read-windows-registry-key-values/6355/2 "2015-02-26T21:29:48Z")

</div>

Sachin, have you looked at the docs for the registry\_key resource:  
[https://docs.chef.io/resource\_registry\_key.html](https://docs.chef.io/resource_registry_key.html),  
[http://docs.chef.io/release/12-0/dsl\_recipe.html#registry-get-values](http://docs.chef.io/release/12-0/dsl_recipe.html#registry-get-values) ?

The registry\_key\_get\_values? DSL element seems like a good place to start,  
the docs should have examples that clarify the usage.

-Adam

_From:_ Sachin Gupta [[mailto:guptasachin1112mrt@gmail.com](mailto:guptasachin1112mrt@gmail.com)]  
_Sent:_ Thursday, February 26, 2015 10:54 AM  
_To:_ [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
_Cc:_ sachin kumar  
_Subject:_ [chef] Read windows registry Key values

Hi All,

I am working on a use case where I need to read two registry key values and  
compare there data in a recipe

HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName

HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName

both are having a value name(ComputerName) and value data(may be same or  
different). I need to compare there value data.

Do I need to write like this:; or some other better way??

Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameComp, access) do |reg|  
reg.each{|name, value, keys | compName = keys}  
end  
Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameActive, access) do |reg|  
reg.each{|name, value, keys | compNameActive = keys}  
end

Any idea??

Thanks & Regards,  
Sachin Gupta

---

<div class="post-metadata">

### Author: ![Sachin\_Gupta](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Sachin\_Gupta](https://discourse.chef.io/u/Sachin_Gupta)
#### Post date: [March 2, 2015, 10:29am UTC](https://discourse.chef.io/t/read-windows-registry-key-values/6355/3 "2015-03-02T10:29:07Z")

</div>

Thanks Adam for the useful links.

I am trying to execute this code from a recipe to get the values of registry

require 'win32/registry'

keynameComp=  
"SYSTEM\CurrentControlSet\Control\computername\computername"  
keynameActive=  
"SYSTEM\CurrentControlSet\Control\computername\activecomputername"  
compName = ""  
compNameActive = ""  
access =Win32::Registry::KEY\_ALL\_ACCESS

Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameComp, KEY\_READ) do |reg|  
reg.each{|name, value, keys | compName = keys}  
end

::Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameActive, KEY\_READ) do |reg|  
reg.each{|name, value, keys | compNameActive = keys}  
end

I am getting error messages as "[2015-03-02T09:52:32+00:00] FATAL:  
NameError: uninitialized constant Chef::Win32::Registry::KEY\_ALL\_ACCESS"

Would like to know, do I need to include any other gem?

Thanks & Regards,  
Sachin Gupta

On Fri, Feb 27, 2015 at 2:59 AM, Adam Edwards [adamed@getchef.com](mailto:adamed@getchef.com) wrote:

> Sachin, have you looked at the docs for the registry\_key resource:  
> [registry\_key Resource](https://docs.chef.io/resource_registry_key.html),  
> [http://docs.chef.io/release/12-0/dsl\_recipe.html#registry-get-values](http://docs.chef.io/release/12-0/dsl_recipe.html#registry-get-values) ?
> 
> The registry\_key\_get\_values? DSL element seems like a good place to start,  
> the docs should have examples that clarify the usage.
> 
> -Adam
> 
> _From:_ Sachin Gupta [[mailto:guptasachin1112mrt@gmail.com](mailto:guptasachin1112mrt@gmail.com)]  
> _Sent:_ Thursday, February 26, 2015 10:54 AM  
> _To:_ [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> _Cc:_ sachin kumar  
> _Subject:_ [chef] Read windows registry Key values
> 
> Hi All,
> 
> I am working on a use case where I need to read two registry key values  
> and compare there data in a recipe
> 
> HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName
> 
> HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName
> 
> both are having a value name(ComputerName) and value data(may be same or  
> different). I need to compare there value data.
> 
> Do I need to write like this:; or some other better way??
> 
> Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameComp, access) do |reg|  
> reg.each{|name, value, keys | compName = keys}  
> end  
> Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameActive, access) do |reg|  
> reg.each{|name, value, keys | compNameActive = keys}  
> end
> 
> Any idea??
> 
> Thanks & Regards,  
> Sachin Gupta

---

<div class="post-metadata">

### Author: ![Sachin\_Gupta](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Sachin\_Gupta](https://discourse.chef.io/u/Sachin_Gupta)
#### Post date: [March 3, 2015, 12:15pm UTC](https://discourse.chef.io/t/read-windows-registry-key-values/6355/4 "2015-03-03T12:15:43Z")

</div>

Hi Adam,

I am already having windows gems installed on my machine

C:\Users\sachkkum\chef-repo\>gem list | grep win32  
win32-api (1.5.1 universal-mingw32)  
win32-dir (0.4.5)  
win32-event (0.6.1)  
win32-ipc (0.6.1)  
win32-mmap (0.4.0)  
win32-mutex (0.4.1)  
win32-process (0.7.3)  
win32-service (0.8.2)  
win32console (1.3.2 x86-mingw32)

would like to know , do I need to add any other gem to get rid of this error  
"[2015-03-02T09:52:32+00:00] FATAL: NameError: uninitialized constant  
Chef::Win32::Registry::KEY\_ALL\_ACCESS"

Thanks  
Sachin

On Mon, Mar 2, 2015 at 3:59 PM, Sachin Gupta [guptasachin1112mrt@gmail.com](mailto:guptasachin1112mrt@gmail.com)  
wrote:

> Thanks Adam for the useful links.
> 
> I am trying to execute this code from a recipe to get the values of  
> registry
> 
> require 'win32/registry'
> 
> keynameComp=  
> "SYSTEM\CurrentControlSet\Control\computername\computername"  
> keynameActive=  
> "SYSTEM\CurrentControlSet\Control\computername\activecomputername"  
> compName = ""  
> compNameActive = ""  
> access =Win32::Registry::KEY\_ALL\_ACCESS
> 
> Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameComp, KEY\_READ) do |reg|  
> reg.each{|name, value, keys | compName = keys}  
> end
> 
> ::Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameActive, KEY\_READ) do  
> |reg|  
> reg.each{|name, value, keys | compNameActive = keys}  
> end
> 
> I am getting error messages as "[2015-03-02T09:52:32+00:00] FATAL:  
> NameError: uninitialized constant Chef::Win32::Registry::KEY\_ALL\_ACCESS"
> 
> Would like to know, do I need to include any other gem?
> 
> Thanks & Regards,  
> Sachin Gupta
> 
> On Fri, Feb 27, 2015 at 2:59 AM, Adam Edwards [adamed@getchef.com](mailto:adamed@getchef.com) wrote:
> 
> > Sachin, have you looked at the docs for the registry\_key resource:  
> > [registry\_key Resource](https://docs.chef.io/resource_registry_key.html),  
> > [http://docs.chef.io/release/12-0/dsl\_recipe.html#registry-get-values](http://docs.chef.io/release/12-0/dsl_recipe.html#registry-get-values) ?
> > 
> > The registry\_key\_get\_values? DSL element seems like a good place to  
> > start, the docs should have examples that clarify the usage.
> > 
> > -Adam
> > 
> > _From:_ Sachin Gupta [[mailto:guptasachin1112mrt@gmail.com](mailto:guptasachin1112mrt@gmail.com)]  
> > _Sent:_ Thursday, February 26, 2015 10:54 AM  
> > _To:_ [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> > _Cc:_ sachin kumar  
> > _Subject:_ [chef] Read windows registry Key values
> > 
> > Hi All,
> > 
> > I am working on a use case where I need to read two registry key values  
> > and compare there data in a recipe
> > 
> > HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName
> > 
> > HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName
> > 
> > both are having a value name(ComputerName) and value data(may be same or  
> > different). I need to compare there value data.
> > 
> > Do I need to write like this:; or some other better way??
> > 
> > Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameComp, access) do |reg|  
> > reg.each{|name, value, keys | compName = keys}  
> > end  
> > Win32::Registry::HKEY\_LOCAL\_MACHINE.open(keynameActive, access) do |reg|  
> > reg.each{|name, value, keys | compNameActive = keys}  
> > end
> > 
> > Any idea??
> > 
> > Thanks & Regards,  
> > Sachin Gupta
