tags:

views:

88

answers:

3

I am writing ruby script which accesses folders on other networked machines (windows). I need to know the environment variables on that machine, how do I do this? Once I get access to the remote environment variables, It will help me know where the software has been installed.

Thanks N.I

A: 

You need to deploy simple HTTP-server with helper script that print to the output stream information you need (i.e environment variables, etc). Next you have to call that script from remote machine, and parse results.

Or, without server, add scheduled task to your remote machine, that writes once per day required information to some known file (I mean to file at fixed path). It's much simpler, however you'll see delayed information changes.

UncleMiF
@UncleMiF,Thanks! Your 2nd suggestion was what I had planned as something that I would do, if nothing else works out :)
Narayan Iyer
+2  A: 

Does your solution need to be pure Ruby? If not, you could use the PsExec command. The following will output the environment variables on remote (for the current user):

psexec \\remote cmd /C set

This works by executing cmd remotely and passing it the command set to run.

The following Ruby code will run PsExec and return the remote environment variables as a Hash:

Hash[*`psexec \\\\remote cmd /C set`.split("\n").
  collect {|i| i.split('=', 2)}.flatten]
Phil Ross
There is no need to launch a remote `cmd` instance. PowerShell can deal with environment variables just fine: they are supported via an `Env` provider, which means they show up as a virtual "drive" named `Env:` just like the registry does, for example. If you want to list all environment variables you can do `Get-ChildItem Env:` (or its aliases `dir Env:` or `ls Env:`). If you want to access a specific environment variable you use `Get-Content Env:\PROGRAMFILES` (or `cat` or `type`) just like you would with any other file or variable. You can also use the shorthand `$Env:PROGRAMFILES` syntax.
Jörg W Mittag
Thanks Phil. I shall try this out and let you know.
Narayan Iyer
A: 

I would recommend the ruby WMI interface. As long as you have a fairly modern Windows (XP and up IIRC), you can fetch environment variables from remote machines easily. Google for more information.

D.Shawley