Pythian Blog: Technical Track

An Oracle "oraenv" script solution for Windows with PowerShell

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: https://github.com/jmrprieto/Oracle-PowerShell
# --------------------------------------------------
 # 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
 $ORACLE_SID=$args[0]
 
 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
 $OHKEY_VALUE= -split $OHKEY_VALUE
 $ORACLE_HOME=$OHKEY_VALUE[2] 
 
 $OBASEKEY_VALUE=reg query $OHKEY /V ORACLE_BASE| Select-String REG_SZ
 $OBASEKEY_VALUE= -split $OBASEKEY_VALUE
 $ORACLE_BASE=$OBASEKEY_VALUE[2]
 
 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:ORACLE_SID=$ORACLE_SID
 $Env:ORACLE_HOME=$ORACLE_HOME
 $Env:ORACLE_BASE=$ORACLE_BASE
 $Env:Path = $Env:Path + ";$ENV:ORACLE_HOME\bin"
 
 

No Comments Yet

Let us know what you think

Subscribe by email