Skip to content

Instantly share code, notes, and snippets.

@stelf
Created July 19, 2021 09:16
Show Gist options
  • Save stelf/46cdcae9bf5c807c3c7b7805c8d2958a to your computer and use it in GitHub Desktop.
Save stelf/46cdcae9bf5c807c3c7b7805c8d2958a to your computer and use it in GitHub Desktop.
establish Oracle connection via .NET Core from Powershell using TNS config
# this GIST is available under the conditions of
# https://creativecommons.org/licenses/by/4.0/
using namespace Oracle.ManagedDataAccess.Core
# the following is needed if the connector is already globally installed
# but IS OKAY even without dependencies, as these install half gig of the .NET core libs anyway
#
# $dest = "$(gl)\packages\"
# Install-Package -Name Oracle.ManagedDataAccess.Core -Destination $dest -SkipDependencies
# $dllLocation = Get-ChildItem -Name Oracle.ManagedDataAccess.dll -Path $dest -Recurse
# Add-Type -Path $dllLocation
# if we need the TNS from somewhere
# $locTNS = 'https://private/location/tnsnames.ora'
# Invoke-WebRequest -Uri $locTNS -OutFile 'tnsnames.ora'
# in case the TNS is not in the local directory
[Oracle.ManagedDataAccess.Client.OracleConfiguration]::TnsAdmin ||= (Get-Location)
[string]$dsn = "Data Source=$($ENV:ORA_INST);User Id=$($ENV:ORA_USER);Password=$($ENV:ORA_PASS);Connection Timeout=3;"
[Oracle.ManagedDataAccess.Client.OracleConnection] $con =
[Oracle.ManagedDataAccess.Client.OracleConnection]::new($dsn)
try {
$con.Open()
Write-Host connection established
$con.Close()
} catch {
Write-Error $_.Exception.ToString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment