Friday, November 16, 2007

Get full name for Windows user from Ruby

Lately I've been writing a few automation programs and it was suggested by a colleague that I write one for logging a job with EDS, our "service" provider.

The web page is nasty to navigate, I think they make it hard so people decide to give up and go get a beer instead of logging a job :)

Anyway, one of the fields on the page requires the full name of the person logging the job. It's trivial to grab the username of the logged on user:

1 puts ENV['USERNAME']

However, I wasn't sure of the easiest way to grab the user's full name. There are a few packages out there, for example the sys-admin gem. It didn't work though, I dug around in the source and it hardcodes a cimv2 path, no good.

Seeing this is a quick hack running on windows for windows, rah rah rah, might as well boogey with it.

Enter ADSI.

The following interrogates our AD and grabs what I want. Too easy.

1 require 'win32ole'
2 puts WIN32OLE.connect("WinNT://your.ad.box.sa.gov.au/#{ENV['USERNAME']}").FullName

Wednesday, November 14, 2007

Blowing away IE

ASP.NET development is great fun, especially when you are targeting Internet Explorer 7 (as if there are any other browsers anyway) :).

It doesn't take much to confuse IE so I find myself in the continuous loop of trying a code change, clearing the cache in IE, rebuilding the app and running it again. Numerous times a day, too many. Dancing the Microsoft Visual Studio developer jig if you will.

Well, enough is enough. Time to automate it.

Consider the following ruby script that uses WSH. I'm aware of watir and other winole jiggery, but why not just send the keystrokes, I say. This script blows out the IE cache, quits it, goes into visual studio and restarts the project, hurrah. Obviously you'll want to replace the AppActivate parameters to match your window titles.

 1 require 'win32ole'
 2
 3 wsh = WIN32OLE.new('Wscript.Shell')
 4 wsh.AppActivate('Generic Auditing Tool')
 5 sleep(2)
 6 wsh.SendKeys('%{T}') #ALT-T: select 'Tools'
 7 sleep(1)
 8 wsh.SendKeys('o') #select 'Internet Options'
 9 sleep(1)
10 wsh.SendKeys('%{D}') #select 'Delete'
11 sleep(1)
12 wsh.SendKeys('a') #select 'Delete all'
13 sleep(1)
14 wsh.SendKeys('d') #select 'also delete files and settings stored by add-ons'
15 sleep(1)
16 wsh.SendKeys('y') #select 'Yes'
17 sleep(8)
18 wsh.SendKeys('%{F4}') #quit 'Internet Options'
19 sleep(1)
20 wsh.SendKeys('%{F4}') #quit IE
21
22 wsh.AppActivate('NAT - Microsoft Visual Studio') #grab the visual studio window
23 sleep(2)
24 wsh.SendKeys('^{F5}') #start the project without debugging