Applying Oracle patches on Windows often feels repetitive and error-prone — stopping services, updating OPatch, applying patches, running datapatch
, and restarting services.
To save time ⏱ and reduce mistakes ⚠, I created a PowerShell automation script that performs the patching process end-to-end 🚀.
🔹 Why Automate Oracle Patching?
-
⏱ Save time by automating repetitive steps
-
⚙ Avoid manual errors during patching
-
📜 Maintain logs for auditing and troubleshooting
-
🛡 Ensure consistent, reliable patching
🔹 Full PowerShell Script
🔹 Breakdown of the Script
Here’s what each part of the script does:
1️⃣ Ensure-Opatch
-
Checks if the OPatch ZIP exists
-
Backs up the existing OPatch folder
-
Extracts the new OPatch utility
2️⃣ Stop-OracleServices
-
Stops the main database service (
OracleServiceORCL
) -
Stops all dependent Oracle services (Listener, MTS Recovery, VSS Writer, etc.)
3️⃣ Apply-Patch
-
Navigates to the patch directory
-
Executes
opatch apply -silent
to apply the patch -
Validates the exit code to ensure patching succeeded
4️⃣ Start-DbService
-
Starts only the database service
-
Keeps the instance running so that datapatch can be executed
5️⃣ Run-Datapatch
-
Runs
datapatch.bat
to update the Oracle data dictionary -
Ensures the database registry is updated with the patch
6️⃣ Start-OtherServices
-
Restarts the remaining Oracle services
-
Brings the environment back online fully
7️⃣ Logging (Transcript)
-
Every action is logged to
oracle_patch_log.txt
for auditing and troubleshooting
🔹 How to Create the Script
-
Open Notepad or any text editor
-
Paste the entire script above
-
Save the file as:
-
Place the script in a convenient directory (e.g.,
C:\patches\scripts\
)
🔹 How to Run the Script
-
📂 Extract your Oracle Patch ZIP to a directory
-
📦 Place the latest OPatch ZIP in the specified location
-
🖥 Open PowerShell as Administrator
-
Navigate to the script location:
-
Run the script with execution policy bypass:
-
📜 Monitor the console output and check logs at:
🔹 Troubleshooting
-
❌ Files in use → Make sure all Oracle services are stopped before running patch
-
🔒 Datapatch lock errors → Check registry locks (refer Oracle MOS Note 1609718.1)
-
📖 Patch failed → Review OPatch logs and the generated transcript file
⚠ Disclaimer
This script is provided as-is.
✔ Always test in a non-production environment before applying in production.
✔ Ensure you have valid backups before patching.
Comments
Post a Comment