Saturday, December 25, 2010

"ruby, any messages for me?"

i have successfully consolidated all of my email accounts to gmail. this makes it easy for me to read all my messages from my android phone or other portable internet devices. during the work week i'm rather tied to my email, but it does no good to leave a web browser tab open on the gmail site while i'm teaching class, and it eats up resources unnecessarily. so, i knocked off this little ruby script to check the google xml feed and determine if i have any new mail messages. this simply displays in my conky resource monitor on my desktop, but you can do something fancier, even use this in a more elaborate mail fetching script, if you wish.

01 #!/usr/bin/ruby
02 # gmail.rb
03 # Checks a gmail account and displays the number of unread (new) messages
04 # waiting on the server.
05 # Robert Ritter <rritter@centriq.com>
06 # December 20, 2010
07
08 # Use 'nokogiri' to parse XML.
09 require 'nokogiri'
10 require 'open-uri'
11
12 username = 'not_a_real_user@gmail.com'
13 password = 'not_a_real_password_either'
13
14 tree = Nokogiri.XML(open('https://mail.google.com/mail/feed/atom',
15 :http_basic_authentication=>[username, password]))
16
17 puts tree.xpath('//xmlns:fullcount').first.content
18

until next time!

Monday, December 20, 2010

three wee scripts in powershell 2...

'tis the season of giving. several of my students have been asking for advanced user interface examples in powershell, so here are three scripts (and a module) for them, you, and anyone else who cares to look. they are heavily commented so i'll not go into details, but this is what i got you for christmas...

screenctl.psm1 is a module that contains three useful functions used by the scripts in this set. set-cursor places the cursor exactly where you want it on the screen, so that whatever you print starts displaying there. clear-lines clears a specific number of lines from the screen, rather than the whole thing. combined with set-cursor you can clear just a small section of the screen if you wish. out-wrappedstring is a variation of the text-wrapping function i talked about here.

addusers.ps1 walks you through adding a user account to active directory. it uses the screenctl module to customize the output and the activedirectory module to read and write the database.

get-weather.ps1 provides current conditions and forecast from an internet xml stream. it uses the screenctl module to wrap long forecast strings.

netcfg.ps1 provides a graphical interface to the network information for machines listed in active directory. it uses the active directory module to locate computers.

read the example scripts, improve them, have fun with them this holiday season. it's better than listening to uncle lloyd snore in front of the television, right?