Login   /   Register

Retrieve a list of the system DSNs installed

Rate this article
     2 votes, average: 2 out of 52 votes, average: 2 out of 52 votes, average: 2 out of 52 votes, average: 2 out of 52 votes, average: 2 out of 5
Loading ... Loading ...
April 1st, 2008 by daniva

If you have no idea what we’re talking about System DSNs are simply a shortcut method for connecting to databases and other data sources. You can view a list of the System DSNs available on a computer by bringing up the ODBC Data Source Administrator dialog box and looking on the System DSN tab

clip_image002

That’s fine if you’re working on the local machine. But what if you’re interested in retrieving a list of the System DSNs on a remote machine, or what if you’d like to inventory the System DSNs on a whole bunch of computers? How do you do something like that?

Const HKLM = &H80000002
 
Set reg = GetObject( "winmgmts:\\.\root\default:StdRegProv" )
keyPath = "SOFTWARE\ODBC\ODBC.INI\ODBC DATA SOURCES"
reg.EnumValues HKLM, keyPath, valueNamesArr, valueTypeArr
 
For i = 0 to UBound( valueNamesArr )
    reg.GetStringValue HKLM, keyPath, valueNamesArr( i ), valueStr    
    Print valueNamesArr( i ) & " –> " & valueStr
Next

For some reason, there’s no WMI class or other COM object designed to retrieve System DSNs. But that’s OK: because this information is stored in the registry we can still write a script to grab and return the DSNs. As you might expect, that’s exactly what the preceding script does: it opens the registry, zips down to HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC DATA SOURCES, and then returns the names and values of the all the registry entries found there. Each entry will consist of a name (representing the DSN name) and a value (representing the DSN driver). We’ll return and print both the name and the value, thus replicating the information found in the dialog box.

Our script begins by defining a constant named HKLM and setting the value to &H80000002; we’ll use this constant to indicate the registry hive we want to work with. We then bind to the WMI service, connecting to the StdRegProv class

Following that, we assign the registry path within HKLM to a variable named keyPath. With that done we can then use this line of code to call the EnumValues method and return a list of all the registry values stored in HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC DATA SOURCES:

reg.EnumValues HKLM, keyPath, valueNamesArr, valueTypeArr

As you can see, we pass EnumValues four parameters. The first two – HKLM and keyPath – are "in parameters" that represent the registry hive and registry path. The second two – valueNamesArr and valueTypesArr – are "out parameters;" that means they represent information that the EnumValues method returns to us. After EnumValues runs, valueNamesArr will be populated with the names of all the registry values found in HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC DATA SOURCES; valueTypesArr, meanwhile, will be populated with the registry data type for all those values.

At this point in time we have the name of each registry entry; if all we wanted to do was echo back the name we’d practically be done. However, we also wanted to echo back the value (that is, the driver name) for each DSN. To do that, we need to connect to each individual registry entry and return the value. And to do that we need to set up a For Next loop that walks through the array of registry entries. For each item in that array, we assign the DSN. We then call the GetStringValue method to return the value assigned to that registry entry:

reg.GetStringValue HKLM, keyPath, valueNamesArr( i ), valueStr  

In this script, strValue is an out parameter that contains the registry value. We now have the DSN name in one variable (strValueName) and the DSN driver in another variable (strValue). All that’s left is to display that information .

Print valueNamesArr( i ) & " –> " & valueStr

Based on "How Can I Retrieve a List of the System DSNs on a Computer?" from the Scripting Guys.

Posted in ADODB

One Response to “Retrieve a list of the system DSNs installed”

  1. Home Inventory System. | 7Wins.eu Says:

    [+]

    [...] Books Online | steve-olson.com How to Make a Crappy CMS | Search Commander Blog - Scott HendisonRetrieve a list of the syste... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 696 times