Last active
October 26, 2020 19:02
-
-
Save Dan1el42/78d344e873ec1aba85207e0dbfa80c63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$crlf = [System.Environment]::NewLine | |
[string] $TNS = 'LINKED_SERVER' | |
$sqlfiles = "c:\temp\script1.sql", "c:\temp\script2.sql" | |
$connection=New-Object DATA.OracleClient.OracleConnection("Data Source=$TNS;User Id=TEST;Password=XXXXXXXX") | |
foreach ($sqlfile in $sqlfiles){ | |
$FileLines = Get-Content $sqlfile | |
$query = [string]::Join($crlf,$FileLines) | |
#log start time | |
[datetime]$Now = Get-Date | |
[string] $TSbegin = $Now.ToString("yyyy-MM-dd HH:mm:ss") | |
echo "`n`nExecuting script $sqlfiles Started at: $TSbegin" | |
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null | |
$connection.Open() | |
$res = (new-Object DATA.OracleClient.OracleCommand($query,$connection)) | |
$output = $res.ExecuteScalar() | |
#log end time | |
[string]$TSend = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
$Duration = New-TimeSpan -Start ($TSbegin) -End (Get-Date) | |
echo "`n$('{0:N0}' -f $output) Rows returned" | |
echo "`nExecuting script $sqlfiles completed at: $TSend" | |
$Duration |Format-Table -AutoSize | |
# Remove-Variable res | |
} | |
$connection.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I want to run Oracle Sql Script using powershell. When I use your script I see. what needs to be changed. How do I change this to 64bit ?
Exception calling "Open" with "0" argument(s): "Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit
Oracle client components installed."
At D:\Jams\script\try\onemore.ps1:16 char:5
Exception calling "ExecuteScalar" with "0" argument(s): "Invalid operation. The connection is closed."
At D:\Jams\script\try\onemore.ps1:18 char:2