1

I have a little windows application running Ruby 1.9.3 (MRI), and I need it to query the "ComputerName" of a remote registry of a remote server. I have the code for checking the local registry, which looks as follows:

#Gemfile
gem "win32-service", "0.8.2", :platforms => :mingw

# regchecker.rb
require 'win32/registry'

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



puts(value_exists?("SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName", 'ComputerName'))

Running this code yields a printout of the local ComputerName registry key as well as a boolean.

However, I cannot find a way to use Ruby and the win32-service gem (or any other gem for that matter) to read the ComputerName from a remote computer for which I have the userid and password. How can I do this using Ruby and win32-service, or some other gem, or even some other command?

Jay Godse
  • 15,163
  • 16
  • 84
  • 131
  • I think [`win32ole`](http://ruby-doc.org/stdlib-1.9.3/libdoc/win32ole/rdoc/WIN32OLE.html) would help you. Follow the [link](http://msdn.microsoft.com/en-us/library/aa389290%28v=vs.85%29.aspx). Try this to convert in Ruby. Before that - create an object like `require 'win32ole'; obj_swbem_locator = WIN32OLE.new("WbemScripting.SWbemLocator")` – Arup Rakshit Mar 07 '14 at 16:07
  • If you are able, please post the code here as an answer. I will be leaving my desk, otherwise I could help you out. – Arup Rakshit Mar 07 '14 at 16:09
  • I followed the links for win32ole, but it did not tell me the magical incantations to connect to a remote server or to read the registry. – Jay Godse Mar 07 '14 at 18:22
  • Now you got the point.. right ? – Arup Rakshit Mar 07 '14 at 18:24
  • I guess that the point is that I need to get up to speed in Windows programming. I also realized now that the http://msdn.microsoft.com/en-us/library/aa389290%28v=vs.85%29.aspx has some useful information. – Jay Godse Mar 07 '14 at 18:39
  • In my office I use *windows*, but your post I saw at exit time :). So I couldn't code for you. Every object windows, you just need to create using `WIN32OLE.new(..)`. what Microsoft did using `CreateObject(..)`.. That's the trick.. – Arup Rakshit Mar 07 '14 at 18:41
  • Thank you for the pointer. I'll have a go at it. – Jay Godse Mar 07 '14 at 18:51
  • Once done, post your code. This is indeed a Good question. +1 – Arup Rakshit Mar 07 '14 at 18:52
  • I tried require 'win32ole'; locator = WIN32OLE.new("WbemScripting.SWbemLocator"); computer = "192.168.100.100"; namespace = "root\\cimv2"; user = "Administrator"; password = "mypassword" ; locator.connectServer(computer, namespace, user, password). As a result, I got: WIN32OLERuntimeError: (in OLE method `connectServer': ) OLE error code:800706BA in SWbemLocator The RPC server is unavailable. HRESULT error code:0x80020009 Exception occurred. from (irb):17:in `method_missing' from (irb):17 from C:/Ruby193/bin/irb:12:in `
    '
    – Jay Godse Mar 07 '14 at 19:11

0 Answers0