Powershell function to test if an ODBC connection is able to connect with the database or not.  The function returns $true if a successful connection is made, and $false if it cannot connect. It will print an error message to the console if there is an error somewhere, like the ODBC connection do not exist.

 

Source code:

PowerShell
Edit|Remove
Function Test-ODBCConnection { 
    [CmdletBinding()] 
    param( 
        [Parameter(Mandatory=$True, 
                    HelpMessage="DSN name of ODBC connection")] 
                    [string]$DSN 
    ) 
    $conn = new-object system.data.odbc.odbcconnection 
    $conn.connectionstring = "(DSN=$DSN)" 
     
    try { 
        if (($conn.open()) -eq $true) { 
            $conn.Close() 
            $true 
        } 
        else { 
            $false 
        } 
    } catch { 
        Write-Host $_.Exception.Message 
        $false 
    } 
}
For more documentation on this script, please see my blog at: https://www.andersrodland.com