PowerShell scripts for setting up Oracle 21c/XE with ROA application: - Automated tablespace, user creation and imports - sqlnet.ora config for Instant Client 11g/ODBC compatibility - Oracle 21c read-only Home path handling (homes/OraDB21Home1) - Listener restart + 10G password verifier for legacy auth - Tested on VM 302 with CONTAFIN_ORACLE schema import Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
373 lines
12 KiB
PowerShell
373 lines
12 KiB
PowerShell
# =============================================================================
|
|
# ROA Oracle Database Setup Configuration
|
|
# =============================================================================
|
|
# This file is sourced by setup scripts. It auto-detects Oracle paths and
|
|
# prompts for user input when needed.
|
|
#
|
|
# Usage:
|
|
# 1. Copy to config.ps1: Copy-Item config.example.ps1 config.ps1
|
|
# 2. Run any setup script - it will prompt for missing values
|
|
# 3. Or edit config.ps1 manually to set values
|
|
#
|
|
# If a value is $null or empty, the script will auto-detect or prompt.
|
|
# =============================================================================
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Oracle Installation Paths (auto-detected if empty)
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Oracle Home - leave empty to auto-detect from registry/common paths
|
|
$ORACLE_HOME = $null
|
|
|
|
# Oracle Base - derived from ORACLE_HOME if empty
|
|
$ORACLE_BASE = $null
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Database Connection (auto-detected if empty)
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Service Name - leave empty to auto-detect (XEPDB1 for XE, ROA for SE)
|
|
$SERVICE_NAME = $null
|
|
|
|
# Database host - auto-detected from listener if empty
|
|
$DB_HOST = $null
|
|
|
|
# Database port
|
|
$DB_PORT = 1521
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Passwords - MUST be set (will prompt if empty)
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# SYS/SYSTEM password (set during Oracle installation)
|
|
$SYSTEM_PASSWORD = $null
|
|
|
|
# CONTAFIN_ORACLE schema password
|
|
$CONTAFIN_PASSWORD = "ROMFASTSOFT"
|
|
|
|
# Default password for new company schemas
|
|
$DEFAULT_COMPANY_PASSWORD = "ROMFASTSOFT"
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Paths (auto-detected if empty)
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Directory for DMP files (import/export)
|
|
$DMPDIR = "C:\DMPDIR"
|
|
|
|
# Oracle DIRECTORY object name
|
|
$DMPDIR_NAME = "DMPDIR"
|
|
|
|
# Datafile directory - auto-detected from existing datafiles if empty
|
|
$DATAFILE_DIR = $null
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Tablespace Configuration
|
|
# -----------------------------------------------------------------------------
|
|
|
|
$TABLESPACE_NAME = "ROA"
|
|
$TABLESPACE_SIZE = "2G"
|
|
$TABLESPACE_MAXSIZE = "UNLIMITED"
|
|
$TABLESPACE_AUTOEXTEND = $true
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Import Settings
|
|
# -----------------------------------------------------------------------------
|
|
|
|
$PARALLEL_JOBS = 2
|
|
$TABLE_EXISTS_ACTION = "REPLACE"
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Logging
|
|
# -----------------------------------------------------------------------------
|
|
|
|
$LOG_DIR = "$PSScriptRoot\logs"
|
|
$VERBOSE_LOGGING = $true
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Advanced Settings
|
|
# -----------------------------------------------------------------------------
|
|
|
|
$NLS_CHARACTERSET = "WE8MSWIN1252"
|
|
$USER_PROFILE = "DEFAULT"
|
|
$DEFAULT_TABLESPACE = "USERS"
|
|
$TEMP_TABLESPACE = "TEMP"
|
|
|
|
# =============================================================================
|
|
# AUTO-DETECTION AND PROMPTING FUNCTIONS
|
|
# =============================================================================
|
|
|
|
function Initialize-Config {
|
|
<#
|
|
.SYNOPSIS
|
|
Initialize configuration by auto-detecting values and prompting for missing ones.
|
|
#>
|
|
|
|
Write-Host ""
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host " ROA Oracle Setup - Configuration" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# Auto-detect Oracle Home
|
|
if (-not $script:ORACLE_HOME) {
|
|
$script:ORACLE_HOME = Find-OracleHome
|
|
if ($script:ORACLE_HOME) {
|
|
Write-Host "[AUTO] Oracle Home: $script:ORACLE_HOME" -ForegroundColor Green
|
|
} else {
|
|
$script:ORACLE_HOME = Read-Host "Enter Oracle Home path (e.g., C:\app\oracle\product\21c\dbhomeXE)"
|
|
}
|
|
} else {
|
|
Write-Host "[SET] Oracle Home: $script:ORACLE_HOME" -ForegroundColor Gray
|
|
}
|
|
|
|
# Derive Oracle Base
|
|
if (-not $script:ORACLE_BASE) {
|
|
# Go up from dbhomeXE to product to 21c to oracle to app
|
|
$script:ORACLE_BASE = Split-Path (Split-Path (Split-Path $script:ORACLE_HOME -Parent) -Parent) -Parent
|
|
Write-Host "[AUTO] Oracle Base: $script:ORACLE_BASE" -ForegroundColor Green
|
|
}
|
|
|
|
# Prompt for SYSTEM password if not set
|
|
if (-not $script:SYSTEM_PASSWORD) {
|
|
$securePass = Read-Host "Enter SYSTEM password" -AsSecureString
|
|
$script:SYSTEM_PASSWORD = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
|
|
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePass))
|
|
}
|
|
|
|
# Auto-detect DB_HOST from listener
|
|
if (-not $script:DB_HOST) {
|
|
$script:DB_HOST = Find-ListenerHost
|
|
if ($script:DB_HOST) {
|
|
Write-Host "[AUTO] Database Host: $script:DB_HOST" -ForegroundColor Green
|
|
} else {
|
|
$script:DB_HOST = "localhost"
|
|
Write-Host "[DEFAULT] Database Host: $script:DB_HOST" -ForegroundColor Yellow
|
|
}
|
|
} else {
|
|
Write-Host "[SET] Database Host: $script:DB_HOST" -ForegroundColor Gray
|
|
}
|
|
|
|
# Build connection string
|
|
$script:DB_CONNECT = "${script:DB_HOST}:${script:DB_PORT}"
|
|
|
|
# Auto-detect Service Name
|
|
if (-not $script:SERVICE_NAME) {
|
|
$script:SERVICE_NAME = Find-ServiceName
|
|
if ($script:SERVICE_NAME) {
|
|
Write-Host "[AUTO] Service Name: $script:SERVICE_NAME" -ForegroundColor Green
|
|
} else {
|
|
$script:SERVICE_NAME = Read-Host "Enter Service Name (XEPDB1 for XE, or your database name)"
|
|
}
|
|
} else {
|
|
Write-Host "[SET] Service Name: $script:SERVICE_NAME" -ForegroundColor Gray
|
|
}
|
|
|
|
# Full connection string
|
|
$script:DB_CONNECT = "${script:DB_HOST}:${script:DB_PORT}/${script:SERVICE_NAME}"
|
|
Write-Host "[INFO] Connection: $script:DB_CONNECT" -ForegroundColor Cyan
|
|
|
|
# Auto-detect datafile directory
|
|
if (-not $script:DATAFILE_DIR) {
|
|
$script:DATAFILE_DIR = Find-DatafileDir
|
|
if ($script:DATAFILE_DIR) {
|
|
Write-Host "[AUTO] Datafile Dir: $script:DATAFILE_DIR" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "[WARN] Could not auto-detect datafile directory" -ForegroundColor Yellow
|
|
$script:DATAFILE_DIR = Read-Host "Enter datafile directory path"
|
|
}
|
|
} else {
|
|
Write-Host "[SET] Datafile Dir: $script:DATAFILE_DIR" -ForegroundColor Gray
|
|
}
|
|
|
|
# Create log directory
|
|
if (-not (Test-Path $script:LOG_DIR)) {
|
|
New-Item -ItemType Directory -Path $script:LOG_DIR -Force | Out-Null
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "Configuration complete!" -ForegroundColor Green
|
|
Write-Host ""
|
|
}
|
|
|
|
function Find-OracleHome {
|
|
<#
|
|
.SYNOPSIS
|
|
Auto-detect Oracle Home from registry or common paths.
|
|
#>
|
|
|
|
# Try ORACLE_HOME environment variable
|
|
if ($env:ORACLE_HOME -and (Test-Path "$env:ORACLE_HOME\bin\sqlplus.exe")) {
|
|
return $env:ORACLE_HOME
|
|
}
|
|
|
|
# Try registry
|
|
$regPaths = @(
|
|
'HKLM:\SOFTWARE\Oracle\KEY_OraDB21Home1',
|
|
'HKLM:\SOFTWARE\Oracle\KEY_OraDB18Home1',
|
|
'HKLM:\SOFTWARE\Oracle\KEY_XE',
|
|
'HKLM:\SOFTWARE\Wow6432Node\Oracle\KEY_OraDB21Home1'
|
|
)
|
|
|
|
foreach ($regPath in $regPaths) {
|
|
if (Test-Path $regPath) {
|
|
$oraHome = (Get-ItemProperty -Path $regPath -ErrorAction SilentlyContinue).ORACLE_HOME
|
|
if ($oraHome -and (Test-Path "$oraHome\bin\sqlplus.exe")) {
|
|
return $oraHome
|
|
}
|
|
}
|
|
}
|
|
|
|
# Try common paths (including user-specific)
|
|
$currentUser = $env:USERNAME
|
|
$commonPaths = @(
|
|
"C:\app\$currentUser\product\21c\dbhomeXE",
|
|
"C:\app\$currentUser\product\21c\dbhome_1",
|
|
'C:\app\oracle\product\21c\dbhomeXE',
|
|
'C:\app\oracle\product\21c\dbhome_1',
|
|
'C:\app\romfast\product\21c\dbhomeXE',
|
|
"D:\app\$currentUser\product\21c\dbhomeXE",
|
|
'D:\app\oracle\product\21c\dbhomeXE'
|
|
)
|
|
|
|
foreach ($path in $commonPaths) {
|
|
if (Test-Path "$path\bin\sqlplus.exe") {
|
|
return $path
|
|
}
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Find-ListenerHost {
|
|
<#
|
|
.SYNOPSIS
|
|
Get the host from listener configuration.
|
|
#>
|
|
|
|
if (-not $script:ORACLE_HOME) { return $null }
|
|
|
|
try {
|
|
$lsnrctl = Join-Path $script:ORACLE_HOME "bin\lsnrctl.exe"
|
|
if (-not (Test-Path $lsnrctl)) { return $null }
|
|
|
|
$output = & $lsnrctl status 2>&1 | Out-String
|
|
|
|
# Parse HOST from listener output
|
|
if ($output -match "HOST=([^\)]+)\)") {
|
|
$host = $Matches[1]
|
|
# If it's 0.0.0.0 or localhost variant, try to get actual IP
|
|
if ($host -eq "0.0.0.0" -or $host -match "^127\." -or $host -eq "localhost") {
|
|
# Get first non-loopback IPv4 address
|
|
$ip = (Get-NetIPAddress -AddressFamily IPv4 |
|
|
Where-Object { $_.IPAddress -notmatch "^127\." } |
|
|
Select-Object -First 1).IPAddress
|
|
if ($ip) { return $ip }
|
|
}
|
|
return $host
|
|
}
|
|
} catch {
|
|
# Ignore errors
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Find-ServiceName {
|
|
<#
|
|
.SYNOPSIS
|
|
Auto-detect service name from listener.
|
|
#>
|
|
|
|
if (-not $script:ORACLE_HOME) { return $null }
|
|
|
|
try {
|
|
$lsnrctl = Join-Path $script:ORACLE_HOME "bin\lsnrctl.exe"
|
|
if (-not (Test-Path $lsnrctl)) { return $null }
|
|
|
|
$output = & $lsnrctl status 2>&1 | Out-String
|
|
|
|
# Look for XEPDB1 first (Oracle XE PDB)
|
|
if ($output -match 'Service "xepdb1"') {
|
|
return "XEPDB1"
|
|
}
|
|
# Look for ROA
|
|
if ($output -match 'Service "roa"') {
|
|
return "ROA"
|
|
}
|
|
# Look for XE
|
|
if ($output -match 'Service "XE"') {
|
|
return "XE"
|
|
}
|
|
} catch {
|
|
# Ignore errors
|
|
}
|
|
|
|
# Default to XEPDB1 for modern Oracle XE
|
|
return "XEPDB1"
|
|
}
|
|
|
|
function Find-DatafileDir {
|
|
<#
|
|
.SYNOPSIS
|
|
Auto-detect datafile directory from existing database files.
|
|
#>
|
|
|
|
if (-not $script:ORACLE_HOME -or -not $script:SYSTEM_PASSWORD -or -not $script:SERVICE_NAME) {
|
|
return $null
|
|
}
|
|
|
|
try {
|
|
$sqlplus = Join-Path $script:ORACLE_HOME "bin\sqlplus.exe"
|
|
$connStr = "system/$script:SYSTEM_PASSWORD@${script:DB_HOST}:${script:DB_PORT}/${script:SERVICE_NAME}"
|
|
|
|
$sql = @"
|
|
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF LINESIZE 500
|
|
SELECT SUBSTR(file_name, 1, INSTR(file_name, '\', -1) - 1)
|
|
FROM dba_data_files
|
|
WHERE tablespace_name = 'SYSTEM' AND ROWNUM = 1;
|
|
EXIT;
|
|
"@
|
|
|
|
$result = $sql | & $sqlplus -s $connStr 2>$null
|
|
$path = ($result -split "`n" | Where-Object { $_ -match "^[A-Z]:\\" } | Select-Object -First 1)
|
|
|
|
if ($path) {
|
|
return $path.Trim()
|
|
}
|
|
} catch {
|
|
# Ignore errors
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Get-OracleConnection {
|
|
return "system/$script:SYSTEM_PASSWORD@$script:DB_CONNECT"
|
|
}
|
|
|
|
function Get-SysdbaConnection {
|
|
return "sys/$script:SYSTEM_PASSWORD@$script:DB_CONNECT as sysdba"
|
|
}
|
|
|
|
function Get-SqlPlusPath {
|
|
return Join-Path $script:ORACLE_HOME "bin\sqlplus.exe"
|
|
}
|
|
|
|
function Get-ImpdpPath {
|
|
return Join-Path $script:ORACLE_HOME "bin\impdp.exe"
|
|
}
|
|
|
|
function Get-ExpdpPath {
|
|
return Join-Path $script:ORACLE_HOME "bin\expdp.exe"
|
|
}
|
|
|
|
# =============================================================================
|
|
# AUTO-INITIALIZE when sourced (if not already configured)
|
|
# =============================================================================
|
|
|
|
# Check if this is being sourced by a setup script
|
|
if ($MyInvocation.InvocationName -ne '.') {
|
|
Write-Host "This file should be dot-sourced: . .\config.ps1" -ForegroundColor Yellow
|
|
}
|