An Oracle “oraenv” script solution for Windows with PowerShell

Posted in: Oracle, Technical Track

The idea

Due to the nature of my consulting work, I mostly work in Linux environments and use bash for scripting. But in some cases, I hit a Windows server hosting an Oracle database and all the fancy bash scripts and shortcuts I use on a daily basis are gone, oraenv being one of them.

In this situation, I turn to PowerShell, which is as powerful as bash and a bit more friendly. It even has an ISE (Integrated Scripting Environment) to quickly develop your scripts, either big or small.

Of all the things I miss from the Linux environment for Oracle, one is the oraenv script. There are several versions of this script around, but I haven’t found one that I really like, so I’ve created a small one that simply finds the required information from the Windows Registry and loads the basic environment variables: ORACLE_SID, ORACLE_HOME, ORACLE_BASE and PATH.

My oraenv script

I’ve been longing for such a simple script for so long. I thought someone else may find it useful, so here it is.
For the latest version, you can get it from my GitHub repo:

# --------------------------------------------------
# Script pythian_oraenv.ps1
# Created ON July 3, 2018
# Author Jose Rodriguez - Pythian
# Notes
# This script receives the ORACLE_SID AS CASE sensitive parameter AND loads the required environment VARIABLES based ON the ORACLE_HOME found IN the Windows registry
# GET AND validate script INPUT parameters
IF ($ORACLE_SID -eq $null) {
Write-Host -ForegroundColor Red "Invalid number of parameters. Usage: pythian_oraenv.ps1 ORACLE_SID" 
exit 1
$OHKEY=reg query HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\ /s /e /f $ORACLE_SID /c | Select-String "\\KEY"
IF ($OHKEY -eq $null) {
Write-Host -ForegroundColor Red "Oracle SID '$ORACLE_SID' not found in the registry. Please verify SID name and case."
exit 2
$OHKEY_VALUE=reg query $OHKEY /V ORACLE_HOME| Select-String REG_SZ
Write-Host  -ForegroundColor Green "Using $ORACLE_HOME to set the environment variables for database $ORACLE_SID."
Write-Host  -ForegroundColor Green "Setting ORACLE_BASE to $ORACLE_BASE"
$Env:Path = $Env:Path + ";$ENV:ORACLE_HOME\bin"

Interested in working with Jose? Schedule a tech call.

About the Author

Oracle Database Consultant
First of all father, then husband and finally Oracle database consultant. I love technology in general and managing data in particular. Trying to learn one new thing every day.

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *