vee
vee

Reputation: 429

Get list of Windows 7 ODBC Data Sources through Command Line

I need to retrieve information from my computer, I can get it through a PHP script but preferably a Perl Script if possible. Basically I need the list of User Defined ODBC Data sources and their server URLS if defined.

I saw this answer on how to extract it from the registry using PHP, but I need the Server URL as well.

Upvotes: 3

Views: 1798

Answers (1)

stevieb
stevieb

Reputation: 9296

I threw this Perl script together very, very quickly, but it appears to do what you need. It puts the connection name in a hash as the key, then puts the server string as its value. Note that as per your question, this will only fetch the user defined sources (it would be trivial to adapt to collect system DSNs though):

use warnings;
use strict;

use Win32::TieRegistry; 

my $odbc_key = $Registry->{'CUser\\Software\\ODBC\\ODBC.INI'};

my %sources;

for (keys %$odbc_key) {
    next if /ODBC Data Sources/;
    s/\\//g;
    $sources{$_} = $odbc_key->{"$_\\Server"};
}

for my $dsn (keys %sources) {
    print "dsn: $dsn, server: $sources{$dsn}\n";
}

Output:

dsn: Test, server: localhost
dsn: Blah, server: yay.blah.com

Upvotes: 1

Related Questions