Jewtus

Getting ODBC driver info

15 posts in this topic

#1 ·  Posted (edited)

I've been trying to figure out a way to query a machine on the network to get the installed programs and drivers available on the machine. I was able to figure out the programs part but I'm struggling with the ODBC Driver list. This is what I've done and it works without any notable issues:

Func GetMachineInstalls($strComputer="localhost")
    local $aReturn[0][2],$colItems
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Product", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
       For $objItem In $colItems
          $sName = $objItem.Name
          $sVersion = $objItem.Version
          _ArrayAdd($aReturn,$sName&"|"&$sVersion,0)
       Next
       Return $aReturn
    Else
       return -1
    Endif
EndFunc

 


I tracked down this article that talks about how to do this. This is the block I need to modify to work in autoit:

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = “.”
Set objRegistry = GetObject(“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)
strKeyPath = “SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers”
objRegistry.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes
For i = 0 to UBound(arrValueNames)
    strValueName = arrValueNames(i)
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
    Wscript.Echo arrValueNames(i) & ” — ” & strValue
Next

Does anyone understand the block above and how I can adapt it to work? I'm also trying to figure out how to get the OS Type(32/64)/name/version using similar logic if anyone is aware of a UDF.

Edited by Jewtus

Share this post


Link to post
Share on other sites



#3 ·  Posted

The WMI method or native Reg* Funcs should be able to do what you want.  One may perform "better" than the other...not sure, and I'm not at a place where I can test; but I do recall the Reg funcs support remote machines.  Otherwise, it's pretty trivial to adapt the vb code to AutoIt.  You are already half way there with your working WMI example.  There also appear to be ODBC WMI classes, but I'm not as familiar with them.

The Win32_OperatingSystem WMI class seems to have the OS info you need:
OSArchitecture
Caption
Version
BuildNumber
OperatingSystemSKU
 

Share this post


Link to post
Share on other sites

#4 ·  Posted

Check "ScriptoMatic" in the Example Scripts forum. This tool generates AutoIt code for you to query WMI.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2016-12-04 - Version 1.2.2.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

20 minutes ago, water said:

Check "ScriptoMatic" in the Example Scripts forum. This tool generates AutoIt code for you to query WMI.

How is it that you always have the droids I'm looking for?

Thanks a lot all, This will be very helpful. I'll share my script when I'm finished.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ok, so I'm a little confused... I used scriptomatic to generate some code and tweaked it to make it fit into what I already build but I'm not actually getting back any values:

 

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ODBCDriverSpecification", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
   For $objItem In $colItems
      ConsoleWrite($objItem.Driver& @CRLF)
      ConsoleWrite($objItem.Description& @CRLF)
      ConsoleWrite($objItem.Name& @CRLF)
      ConsoleWrite($objItem.Version & @CRLF)
      ConsoleWrite("=========================="& @CRLF)
   Next
Endif

When I run this, the objects are there in the $colItems, but nothing gets printed to the console. I checked the data types and the fields I'm grabbing are all strings and I'm using the correct namespace... 

 

I tried a few things out of scriptomatic and I had the same results (nothing writes to the console).

 

Anyone know what I'm doing wrong?

 

EDIT:

This one works but not the ODBC one

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
   For $objItem In $colItems
      ConsoleWrite($objItem.OSArchitecture& @CRLF)
      ConsoleWrite($objItem.Caption& @CRLF)
      ConsoleWrite($objItem.Version& @CRLF)
      ConsoleWrite($objItem.BuildNumber& @CRLF)
      ConsoleWrite($objItem.OperatingSystemSKU & @CRLF)
      ConsoleWrite("=========================="& @CRLF)
   Next
Endif

 

Edited by Jewtus

Share this post


Link to post
Share on other sites

#7 ·  Posted

I also get no results from the ODBC WMI query, but do get (some) data using the registry method.  If the registry has the data you need/want, then it may be the better way to go.

 

Share this post


Link to post
Share on other sites

#8 ·  Posted

I've been trying to figure out how to do the registry method, but I'm grasping at straws (a lot of this WMI and installed software stuff is new to me).

If I do this:

$sVar = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers","")

I get a result, but I'm not sure how to pull the "Name" from the registry.

I feel like this is a really simple step I'm missing. 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@water you forgot about ado.au3 ?

; #FUNCTION# ====================================================================================================================
; Name ..........: _ADO_GetProvidersList
; Description ...: This tool lists all available providers installed on the computer.
; Syntax ........: _ADO_GetProvidersList()
; Parameters ....: None
; Return values .: $aResult - a list of all available providers installed on the computer
; Author ........: water
; Modified ......: mLipok
; Remarks .......: based on: _ADO_OLEDBProvidersList
; Related .......:
; Link ..........: https://www.autoitscript.com/wiki/ADO_Tools
; Example .......: No
; ===============================================================================================================================
Func _ADO_GetProvidersList()
....
....
....

 

Edited by mLipok

NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

@mLipok I took a look at that script and It does execute and pull back OLE DB providers, but I need ODBC providers.  I made some minor tweaks to it to see if I could adapt it, but I don't know enough about registry stuff.

I tried this, but I'm getting no results:

 

EDIT: I put the wrong HK ref in so I fixed it and the progress bar gets stuck at 0%

Func _ADO_GetProvidersList()
    Local $sKey = "HKLM\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers" ; HKLM not HKMH
    Local $iIndexReg = 1, $iIndexResult = 0
    Local $iMax = 100000, $iMin = 1, $iPrevious = $iMin, $iCurrent = $iMax / 2
    Local $aResult[200][3]

    ProgressOn("", "Processing the Registry", "", Default, Default, $DLG_MOVEABLE)

    ; Count the number of keys
    While 1
        RegEnumKey($sKey, $iCurrent)
        If @error = -1 Then ; Requested subkey (key instance) out of range
            $iMax = $iCurrent
            $iCurrent = Int(($iMin + $iMax) / 2)
            $iPrevious = $iMax
        Else
            If $iPrevious <= ($iCurrent + 1) And $iPrevious >= ($iCurrent - 1) Then ExitLoop
            $iMin = $iCurrent
            $iCurrent = Int(($iMin + $iMax) / 2)
            $iPrevious = $iMin
        EndIf
    WEnd

    Local $iPercent = 0
    Local $sKeyValue = '', $sSubKey = ''
    ; Process registry
    While 1
        If Mod($iIndexReg, 10) = 0 Then
            $iPercent = Int($iIndexReg * 100 / $iCurrent)
            ProgressSet($iPercent, $iIndexReg & " keys of " & $iCurrent & " processed (" & $iPercent & "%)")
        EndIf
        $sSubKey = RegEnumKey($sKey, $iIndexReg)
        If @error Then ExitLoop

        $sKeyValue = RegRead($sKey & "\" & $sSubKey, "REG_SZ")
        If @error = 0 Then
            $aResult[$iIndexResult][0] = $sKey & "\" & $sSubKey
            $aResult[$iIndexResult][1] = RegRead($sKey & "\" & $sSubKey, "")
            $iIndexResult += 1
        EndIf

        $iIndexReg += 1
    WEnd

    ProgressOff()
    ReDim $aResult[$iIndexResult][3]

    Return SetError("", "", $aResult)
EndFunc

 

Edited by Jewtus

Share this post


Link to post
Share on other sites

#11 ·  Posted

; List of the ODBC Drivers installed on this computer
Local $HKEY_LOCAL_MACHINE = 0x80000002
Local $strComputer = "."
Local $objRegistry = ObjGet("winmgmts:\\" & $strComputer & "\root\default:StdRegProv")
Local $strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
Local $arrValueNames, $arrValueTypes, $strValue
$objRegistry.EnumValues($HKEY_LOCAL_MACHINE, $strKeyPath, $arrValueNames, $arrValueTypes)
For $i = 0 to UBound($arrValueNames) - 1
    $strValueName = $arrValueNames[$i]
    $objRegistry.GetStringValue($HKEY_LOCAL_MACHINE,$strKeyPath,$strValueName,$strValue)
    ConsoleWrite($arrValueNames[$i] & " -- " & $strValue & @CRLF)
Next
Exit

;)

1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted

13 hours ago, Jewtus said:

How is it that you always have the droids I'm looking for?

By accident ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2016-12-04 - Version 1.2.2.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#13 ·  Posted

10 hours ago, mLipok said:

@water you forgot about ado.au3 ?

No, I didn't :)
But I noticed that the user was asking for ODBC drivers not OLE providers. So I remained silent ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2016-12-04 - Version 1.2.2.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#14 ·  Posted

10 hours ago, PACaleala said:
; List of the ODBC Drivers installed on this computer
Local $HKEY_LOCAL_MACHINE = 0x80000002
Local $strComputer = "."
Local $objRegistry = ObjGet("winmgmts:\\" & $strComputer & "\root\default:StdRegProv")
Local $strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
Local $arrValueNames, $arrValueTypes, $strValue
$objRegistry.EnumValues($HKEY_LOCAL_MACHINE, $strKeyPath, $arrValueNames, $arrValueTypes)
For $i = 0 to UBound($arrValueNames) - 1
    $strValueName = $arrValueNames[$i]
    $objRegistry.GetStringValue($HKEY_LOCAL_MACHINE,$strKeyPath,$strValueName,$strValue)
    ConsoleWrite($arrValueNames[$i] & " -- " & $strValue & @CRLF)
Next
Exit

;)

That'll do

Thanks everyone. I'll post the full script in a few.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

For anyone interested:

Pass it a host name and get an array back. Crude but functional.

Func GetMachineInstalls($strComputer="localhost")
    local $aReturn[0][2],$colItems
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Product", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
       For $objItem In $colItems
          $sName = $objItem.Name
          $sVersion = $objItem.Version
          _ArrayAdd($aReturn,$sName&"|"&$sVersion,0)
       Next
       Return $aReturn
    Else
       return -1
    Endif
EndFunc

Func GetMachineODBC($strComputer="localhost")
    $HKEY_LOCAL_MACHINE = 0x80000002
    $objRegistry = ObjGet("winmgmts:\\" & $strComputer & "\root\default:StdRegProv")
    $strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
    Local $arrValueNames, $arrValueTypes, $strValue, $aReturn[0][2]
    $objRegistry.EnumValues($HKEY_LOCAL_MACHINE, $strKeyPath, $arrValueNames, $arrValueTypes)
    For $i = 0 to UBound($arrValueNames) - 1
        $strValueName = $arrValueNames[$i]
        $objRegistry.GetStringValue($HKEY_LOCAL_MACHINE,$strKeyPath,$strValueName,$strValue)
        _ArrayAdd($aReturn,$arrValueNames[$i]&"|"&$strValue)
    Next
    If UBound($aReturn)>0 then
        Return $aReturn
    Else
        Return -1
    EndIf
EndFunc   

Func GetMachineOS($strComputer="localhost")
    local $aReturn[0][5],$colItems
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
       For $objItem In $colItems
          $sArch = $objItem.OSArchitecture
          $sName = $objItem.Caption
          $sVersion = $objItem.Version
          $sBuild = $objItem.BuildNumber
          $sSKU = $objItem.OperatingSystemSKU
          _ArrayAdd($aReturn,$sArch&"|"&$sName&"|"&$sVersion&"|"&$sBuild&"|"&$sSKU,0)
       Next
       Return $aReturn
    Else
       return -1
    Endif
EndFunc

 

Edited by Jewtus

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now