0

I wrote this script and trying to schedule it and run it using DBMS Scheduler but it isn't running it and throwing me an error that the PowerShell commands aren't recognized:

REM deletion of files after 5 mins of modification 
powershell -Command "Get-ChildItem 'E:\JDEBKP\Databases' -Recurse -File -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddMinutes(-5) } | Remove-Item -Force"
powershell -Command "Get-ChildItem 'E:\JDEBKP\Databases' -Recurse -File -Filter '*.DMP' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddMinutes(-5) } | Remove-Item -Force"

REM export of DD920  
expdp SYSTEM/password1@JDEDB schemas=DD920 directory=JDE_BKP dumpfile=DD920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.dmp logfile=DD920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log 
set "backup_result1=%errorlevel%"
if %backup_result1% NEQ 0 (
    echo DD920 backup failed! >>DD920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log
    echo Error Code: %backup_result1% >>DD920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log
    blat -to marchelou33@gmail.com -f marchelou8@gmail.com -s "Backup failure" -server smtp.server.com
) else (
    echo DD920 backup completed! >>E:\JDEDBK\DatabasesBackup\DD920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log
)
REM export of OL920
expdp SYSTEM/password1@JDEDB schemas=OL920 directory=JDE_BKP dumpfile=OL920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.dmp logfile=OL920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log 
set "backup_result2=%errorlevel%"
if %backup_result2% NEQ 0 (
   echo "OL920 backup failed!" >>E:\JDEDBK\DatabasesBackup\OL920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log
    echo Error Code: %backup_result2% >> OL920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log
    set /a "error_counter=error_counter+1" 
    call :send_email_on_failure "OL920"
) else (
    echo "OL920 backup completed!" >>E:\JDEDBK\Databases\OL920_%date:~-4,4%%date:~-7,2%%date:~-10,2%.log
)

I searched for solutions but nothing useful popped up.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • Someone has done it for this post, but please [format your posts properly](https://stackoverflow.com/help/formatting) in the future. – mklement0 Aug 23 '23 at 14:31
  • Please [edit] your question to include the error messages. – mklement0 Aug 23 '23 at 14:32
  • 1
    It would be better to have your scheduler job call the DBMS_DATAPUMP API package directly, within the database, than to call an external shell script to do the same thing. – pmdba Aug 23 '23 at 15:55
  • 1
    Please take a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) It would be best using `%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe` in the batch file, but a fix of the __system__ environment variable `Path` starting by Windows default with `%SystemRoot%\System32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0\;` would be also highly recommended. – Mofi Aug 23 '23 at 18:35

0 Answers0