PSOraenv.psm1

<#
 .Synopsis
  Show a list of Oracle SID's.
 
 .Description
  Show a list of Oracle SID's and their associated ORACLE_HOME values, pulled from the registry of the local machine.
 
 .Example
  Get-OraSID
#>


function Get-OraSID 
{
    #Create an empty object for display
    $display = @()

    #Get a list of Oracle homes from the registry
    $homes = Get-ChildItem -Path HKLM:\SOFTWARE\Oracle | where {$_.Name -match 'KEY_Ora'}

    #Loop through each home and add the desired information to the display object
    foreach ($path in $homes)
    {
        $dir = Split-Path $path.Name -Leaf
        $oraObject = Get-ItemProperty "HKLM:\SOFTWARE\Oracle\$dir"
      
        $objDisplay = New-Object System.Object
        $objDisplay | Add-Member -type NoteProperty -name OracleSid -value $oraObject.ORACLE_SID
        $objDisplay | Add-Member -type NoteProperty -name OracleHome -value $oraObject.ORACLE_HOME

        $display += $objDisplay
    }

    #Display the completed display object
    $display
}

<#
 .Synopsis
  Displays the current value of your Oracle environment variables.
 
 .Description
  Displays the current value of ORACLE_SID, ORACLE_HOME, and ORACLE_BASE.
 
 .Example
  Get-OraEnv
 
#>

function Get-OraEnv
{
    #If ORACLE_SID has a value, display it
    if ($env:ORACLE_SID -ne $null)
    {
        Write-Host "ORACLE_SID: " $env:ORACLE_SID
    }

    else
    {
        Write-Host "ORACLE_SID is empty"
    }

    #If ORACLE_HOME has a value, display it
    if ($env:ORACLE_HOME -ne $null)
    {
        Write-Host "ORACLE_HOME: " $env:ORACLE_HOME
    }

    else
    {
        Write-Host "ORACLE_HOME is empty"
    }

    #If ORACLE_BASE has a value, display it
    if ($env:ORACLE_BASE -ne $null)
    {
        Write-Host "ORACLE_BASE: " $env:ORACLE_BASE
    }

    else
    {
        Write-Host "ORACLE_BASE is empty"
    }
}

<#
 .Synopsis
  Sets the Oracle environment variables based on a provided SID.
 
 .Description
  Sets the Oracle environment variables based on a provided SID.
  Information is obtained from the registry to set ORACLE_SID, ORACLE_HOME, and ORACLE_BASE.
 
 .Example
  # Set the current environment for the database DB1.
   Set-OraEnv DB1
 
#>

function Set-OraEnv 
{
param(
    [Parameter(Mandatory=$true)]
    [string]$oraSid
    )
    
    #Get a list of Oracle homes from the registry
    $homes = Get-ChildItem -Path HKLM:\SOFTWARE\Oracle | where {$_.Name -match 'KEY_Ora'}

    #Loop through each home to find the one we're working with based on the provided SID.
    #Then use the key's values to set our environment variables.
    foreach ($path in $homes)
    {
        $dir = Split-Path $path.Name -Leaf
        
        $oraObject = Get-ItemProperty "HKLM:\SOFTWARE\Oracle\$dir"
        
        if ($oraObject.ORACLE_SID -eq $oraSid)
        {
            $oraHome = $oraObject.ORACLE_HOME
            $oraBase = $oraObject.ORACLE_BASE
            
            Write-Host "Changing environment to:"
            Write-Host "ORACLE_SID : " $oraSid
            Write-Host "ORACLE_HOME: " $oraHome
            Write-Host "ORACLE_BASE: " $oraBase

            $env:ORACLE_SID = $oraSid
            $env:ORACLE_HOME = $oraHome
            $env:ORACLE_BASE = $oraBase
        }
    }  
}

#Make our functions available for use
export-modulemember -function Get-OraSID
export-modulemember -function Get-OraEnv
export-modulemember -function Set-OraEnv