Execute a cmd.exe batch file
Batch file
First, we need to create a batch file to be executed. For simplicity, it is just one liner which writes the execution date-time into the file executions.txt
:
echo Batch file was executed at %date%:%time% >> %~dp0\executions.txt
Create the credential
We also need to specify the
user account (the username and password) with which the batch file will executed.
Such a username/password pair is stored in a
credential object:
begin
dbms_credential.create_credential(
credential_name => 'RENES_CREDENTIALS',
username => 'rene',
password => '…'
);
end;
/
Create and execute the job
We're now ready to create a (run-once) job to execute the batch file:
begin
dbms_scheduler.create_job (
job_name => 'RUN_BATCH_FILE',
job_type => 'EXTERNAL_SCRIPT',
job_action => 'C:\Users\Rene\scheduler-job\batch-file.bat > nul',
credential_name => 'RENES_CREDENTIALS',
enabled => true,
auto_drop => true
);
end;
/
Show execution in log and on command line
After the execution of the job, we find its execution in the
data dictionary:
select *
from
user_scheduler_job_run_details
where
job_name = 'RUN_BATCH_FILE';
The execution is also recorded in executions.txt
:
C:\> type C:\Users\Rene\scheduler-job\executions.txt
Cleaning up
The job was created with auto_drop => true
, so it needs not be deleted. However, the logs and the credentials are still lingering around, so we drop them:
begin
dbms_credential.drop_credential('RENES_CREDENTIALS');
dbms_scheduler.purge_log (
job_name => 'RUN_BATCH_FILE'
);
end;
/
Executing a PowerShell script