10

I am trying to log in to the the Oracle DB using PowerShell and run a script called "C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql", When I execute the PS nothing happens.

Here is what I have.

$adminLogon = "sys as sysdba/manager@ORCL"
$logon = "sqlplus\sql/manager@ORCL"


$mydata = Invoke-SqlPlus -inputfile       "@C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql" $logon

I've also tried this.

$database = "ORCL";
$user = "sys as sysdba";
$pw = "manager";

sqlplus.exe -d $database -U $user -P $pw -I "@C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql"

I tried this.

& 'C:\app\Administrator\product\11.2.0\client_1\BIN\sqlplus.exe' 'QE-JDBC-1/manager@ORCL sys as sysdba' '@C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql'

I get the error, "& : The module 'sqlplus' could not be loaded. For more information, run 'Import-Module sqlplus'. At line:5 char:3 + & $mydata Invoke-SqlPlus -inputfile "@C:\Users\Administrator\Desktop\oracle\Orac ... + ~~~~~~~ + CategoryInfo : ObjectNotFound: (sqlplus\sql/manager@ORCL:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CouldNotAutoLoadModule"

user3826435
  • 101
  • 1
  • 1
  • 5
  • Have you looked at "How to run exe in powershell with parameters with spaces and quotes", http://stackoverflow.com/questions/1673967/how-to-run-exe-in-powershell-with-parameters-with-spaces-and-quotes. Keith Hill's answer works for me. – Patrick Bacon Jul 10 '14 at 17:24
  • Hey, just in case you still want to run SQL*Plus on PowerShell, take a look at this Module: https://www.powershellgallery.com/packages/JS.OracleDatabase – Jesus A. Sanchez Dec 13 '17 at 17:46

5 Answers5

12

I use the call operator, &, as Keith Hill has suggested with the question, How to run an EXE file in PowerShell with parameters with spaces and quotes.

& 'path\sqlplus.exe' 'system/password@dbase as sysdba'

I placed the username, password in quotes due to the spaces.

To start a script, I add another parameter as follows:

 & 'path\sqlplus.exe' 'system/password@dbase as sysdba' '@my_script.sql'

If you are receiving the ORA-12154 error, and you know that other users have established connections (which implies that the database listener is running properly); I would then examine if SQL*Plus can find my tnsname file.

My first task would be to see if I can tnsping as follows in Windows cmd.exe:

tnsping orcl

It will confirm that a connection can (or can not be established).

If it cannot, I would check to see if the environment variable, ORACLE_HOME, is set. SQL*Plus uses this to find tnsname.ora file.

If it is not set, I would execute this statement in PowerShell (to establish this environment variable):

[Environment]::SetEnvironmentVariable("ORACLE_HOME", "C:\app\Administrator\product\11.2.0\client_1" , "User")

Next, I would retry to tnsping (identified above).

Once successful, I would re-try to execute the script running command above.

Community
  • 1
  • 1
Patrick Bacon
  • 4,490
  • 1
  • 26
  • 33
  • I get this error, "ERROR: ORA-12154: TNS:could not resolve the connect identifier specified", when I use, "& 'C:\app\Administrator\product\11.2.0\client_1\BIN\sqlplus.exe' 'system/manager@ORCL as sysdba' '@C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql'". – user3826435 Jul 10 '14 at 18:17
  • Have you set your ORACLE_HOME environment variable? Can you connect in Windows cmd.exe mode (e.g. can you tnsping orcl)? – Patrick Bacon Jul 10 '14 at 18:39
  • Do I leave the "[Environment]::SetEnvironmentVariable("ORACLE_HOME", "oracle_home_path" , "User")" as is or do I need to change it? If I do change it what should each one be changed to? – user3826435 Jul 10 '14 at 19:06
  • I ran the environment change and rebooted and tried again, I still get the same results. Should I of left the "User" unchanged or changed it something else? I left it as just "User". – user3826435 Jul 10 '14 at 20:18
  • 1
    In windows cmd.exe mode, can you tnsping orcl? If you cannot, it means that SQL*Plus is having issues with your configuration. Do you have an entry in the tnsname.ora file for orcl? – Patrick Bacon Jul 10 '14 at 20:25
  • The tnsping cannot ping the DB, but I know that I have made and can still make a connection with JDBC to this database using the same information. – user3826435 Jul 10 '14 at 20:26
  • = [ (DESCRIPTION_LIST = (DESCRIPTION= [ (SDU=2048) ] [ (ADDRESS_LIST= (ADDRESS= [ (COMMUNITY=) ] (PROTOCOL=tcp) (HOST=) (PORT=) ) [ (ADDRESS= (PROTOCOL=ipc) (KEY=) ) ] [ (ADDRESS= [ (COMMUNITY=) ] (PROTOCOL=decnet) (NODE=) (OBJECT=) ) ] [ ) ] [ (CONNECT_DATA= (SID=) [ (GLOBAL_NAME=) ] – user3826435 Jul 10 '14 at 20:32
  • With most configurations, SQL*Plus relies on the tnsnames.ora file to establish a connection with your database. If you cannot tnsping, you will not be able to establish a connection (cmd.exe nor PowerShell). Compare your jdbc connection with the jdbc parameters. By the way, 'User" is acceptable. – Patrick Bacon Jul 10 '14 at 20:35
  • ) ] [ (SOURCE_ROUTE=yes) ] ) (DESCRIPTION= [ (SDU=2048) ] [ (ADDRESS_LIST= ] (ADDRESS [ (COMMUNITY=) ] (PROTOCOL=tcp) (HOST=) (PORT=) ) [ (ADDRESS= (PROTOCOL=ipc) (KEY=) ) ] [ ) ] [ (CONNECT_DATA= (SID=) [ (GLOBAL_NAME=) ] ) ] [ (SOURCE_ROUTE=yes) ] ) [ (CONNECT_DATA= (SID=) [ (GLOBAL_NAME=) ] [ ) ] – user3826435 Jul 10 '14 at 20:35
3

I use this:

$cmd = "cmd.exe"
$args = ("/c sqlplus {0}/{1}@{2}:{3}/{4} @{5} {6}" -f $userName, $password, $tnsAlias, $port, $dbInstance, $sqlScript, $outputFileName)
&$cmd $args 
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Vik
  • 333
  • 5
  • 14
1

In your Windows PowerShell command prompt the code does not require variable setting or anything fancy. Just do this:

sqlplus ElBankoUser\SupaSecretyPass "@C:\Users\ElBankoUser\Documents\MaFancySckrp.sql"

PaSe
  • 93
  • 1
  • 1
1

You can use .NET Oracle library DLL, just make sure you have the required DLL file under the lib folder

Add-Type -Path "lib\Oracle.ManagedDataAccess.dll"

$query = "select  1 as Col1, 2 as Col2, 3 as Col3 from dual
          union
          select  4 as Col1, 5 as Col2, 6 as Col3 from dual
          union
          select  7 as Col1, 8 as Col2, 9 as Col3 from dual"

$cn   = New-Object Oracle.ManagedDataAccess.Client.OracleConnection -ArgumentList "TNS-ConnectionString-Here"
$cmd  = New-Object Oracle.ManagedDataAccess.Client.OracleCommand    -ArgumentList $query

$cmd.Connection = $cn

try {
    $cn.Open()

    $reader = $cmd.ExecuteReader()

    while ($reader.Read()) {
        $col1 = $reader["Col1"]
        $col2 = $reader["Col2"]
        $col3 = $reader["Col3"]

        Write-Host $col1, $col2, $col3
    }

} catch {
    Write-Error $_.Exception.Message
} finally {
    $cmd.Dispose()
    $cn.Dispose()
}
Ostati
  • 4,623
  • 3
  • 44
  • 48
0

Why not use this?

sqlplus -s $adminLogin "@C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql"

-s just suppresses the sqlplus banner.

Chuck
  • 119
  • 2