Search code examples
oraclepowershellpowershell-3.0

Execute sqlplus sessions with jobs in powershell


I want to execute some sessions with various different scripts into a Oracle database. I want to do it with open jobs simultaneous but i think that i have problems with parameter pass. The $item_actual appears is null when start-job try to use.

$lista_scripts = @("insert","update","select")  
For ($i=0; $i -le 2; $i++)
{
     $item_actual=$lista_scripts[$i]
     Write-Host "Ejecutandose $item_actual"
     start-job {date;cat "$item_actual";cat $item_actual | sqlplus xxx/xxx@"(description=(address=(protocol=TCP)(host=xxx)(port=xxx))(connect_data=(SERVICE_NAME=xxx)))"; date;}
}

Thanks


Solution

  • $username = "scott"
    $password = "tiger"
    $connect_string ="test-ecdu"
    $log_file = "c:\powershell_script\test\log.txt"
    
    $lista_scripts = @("select 1 ,sysdate from dual ;","select 2,sysdate from dual;","select 3,sysdate from dual;")  
    
    For ($i=0; $i -le 2; $i++)
    {
         $item_actual=$lista_scripts[$i]
    
         Write-Host "Ejecutandose $item_actual"
         start-job -ArgumentList $username, $password,$connect_string,$item_actual, $log_file  -ScriptBlock {param($username_, $password_,$connect_string_ , $item_actual_, $log_file_)
         date >>$log_file_; 
         Write-output "RUN $item_actual_" >>$log_file_ ;
         $sqlOutput= $item_actual_ | sqlplus -s  $username_/$password_@$connect_string_; 
         Write-output $sqlOutput >>$log_file_
         date >>$log_file_;
                 }
    }
    
    
    c:\powershell_script\test\log.txt
    
    6 июля 2018 г. 13:20:45
    RUN select 1 ,sysdate from dual ;
    
         1 SYSDATE
    ---------- --------
         1 06.07.18
    
    Elapsed: 00:00:00.00
    
    6 июля 2018 г. 13:20:45
    
    6 июля 2018 г. 13:20:45
    RUN select 2,sysdate from dual;
    
         2 SYSDATE
    ---------- --------
         2 06.07.18
    
    Elapsed: 00:00:00.00
    
    6 июля 2018 г. 13:20:45
    
    6 июля 2018 г. 13:20:46
    RUN select 3,sysdate from dual;
    
         3 SYSDATE
    ---------- --------
         3 06.07.18
    
    Elapsed: 00:00:00.00
    
    6 июля 2018 г. 13:20:46