Skip to content

Instantly share code, notes, and snippets.

@ismits
Last active May 1, 2019 19:46
Show Gist options
  • Save ismits/b03a04f162c797e35162c8adf9f40b90 to your computer and use it in GitHub Desktop.
Save ismits/b03a04f162c797e35162c8adf9f40b90 to your computer and use it in GitHub Desktop.
PowerShell module for Oracle DB
<#
.NOTES
Name: Oracle.psm1
Author: Immanuel Smits
Version History:
1.0 - 7/12/2016 - Initial Release.
.SYNOPSIS
Module to provide database access to PowerShell
.DESCRIPTION
Expanded description of what the script does.
#>
$AssemblyFile = "C:\workshop\progr-files\oracle\product\11.2.0\dbhome_1\ODP.NET\bin\2.x\Oracle.DataAccess.dll"
function Main
{
$ConnectionString = Get-ConnectionString 'server' 'sid' 'port' 'username' 'password'
$OracleConnection = Get-Connection $ConnectionString
$Results = Get-DataTable $ConnectionString 'SELECT SOMETHING FROM DUAL'
$Results
}
function Get-Connection
{
param (
[string]$ConnectionString
)
try
{
[Reflection.Assembly]::LoadFile($AssemblyFile) | Out-Null
$OracleConnection = New-Object -TypeName Oracle.DataAccess.Client.OracleConnection
$OracleConnection.ConnectionString = $ConnectionString
$OracleConnection.Open()
return $OracleConnection
}
catch
{
return $null
}
}
function Get-ConnectionString
{
param (
[string]$hostname,
[string]$servicename,
[string]$port,
[string]$username,
[string]$password
)
$ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=$hostname)(PORT=$port)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=$servicename)));User Id=$username;Password=$password;"
return $ConnectionString
}
function Get-DataTable
{
param (
$OracleConnection,
$CommandText
)
try
{
$OracleCommand = New-Object -TypeName Oracle.DataAccess.Client.OracleCommand
$OracleCommand.CommandText = $CommandText
$OracleCommand.Connection = $OracleConnection
$OracleDataAdapter = New-Object -TypeName Oracle.DataAccess.Client.OracleDataAdapter
$OracleDataAdapter.SelectCommand = $OracleCommand
$DataSet = New-Object -TypeName System.Data.DataSet
$OracleDataAdapter.Fill($DataSet) | Out-Null
$OracleDataAdapter.Dispose()
$OracleCommand.Dispose()
$OracleConnection.Close()
return $DataSet.Tables[0]
}
catch
{
return $null
}
}
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment