Wednesday, 12 August 2009

Ruby on Windows

I'm a contractor Windows/Novell/Linux system administrator. Mainly Windows, nowadays.

Whenever I start a new contract (hint - I'm looking now...) I like to install Ruby on my Windows workstation. Almost invariably I'll be asked to produce an Excel sheet with some information gathered from Windows. For me, the easiest way is simply to fire up a Ruby console, get it talking to Excel and type the commands in manually to prepare the report. For more complicated work, I often just prepare the commands in notepad and paste them into the Ruby interpreter.

Here's how easy it is to extract some user information from a system and put it into Excel.

require 'win32ole'
excel = WIN32OLE.connect("Excel.Application")
worksheet = excel.worksheets("Sheet1")
row = 2
server = WIN32OLE.connect("WinNT://replaceWithYourServerName")
server.filter = [ 'user' ]
server.each do |u|
   wb.cells(row,1).value = u.name
   wb.cells(row,2).value = u.fullName
   wb.cells(row,3).value = u.description
   row += 1
end


As soon as you type end and press return, your excel worksheet populates with the user information from the system. You would need an authenticated connection with admin rights (being logged into the same domain as the server with admin rights is enough).

Hope this was useful to somebody. I haven't seen many posts about using Ruby on Windows and talking to the Windows APIs.