0% found this document useful (0 votes)
33 views5 pages

Add Oracle Database to Cluster Group

This PowerShell script adds an Oracle database to an Oracle cluster group. It begins by checking if the script is running with elevated privileges. It then defines functions to get input from the user for the database SID, cluster name, database group name, parameter file path, and to confirm actions. The main script body validates the user input, removes the database from any existing group, and adds it to the new group, handling errors and logging messages to a file.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Add Oracle Database to Cluster Group

This PowerShell script adds an Oracle database to an Oracle cluster group. It begins by checking if the script is running with elevated privileges. It then defines functions to get input from the user for the database SID, cluster name, database group name, parameter file path, and to confirm actions. The main script body validates the user input, removes the database from any existing group, and adds it to the new group, handling errors and logging messages to a file.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

###################################################################################

#######
# Purpose: The script calls the Oracle Failsafe Cmdlet that adds the Oracle
database to
# the according and previously created Oracle Cluster Group.
#
# History:
# 02/2014 Created -mb-
# 01/2015 Write important messages and errors to a logfile.
#
#
###################################################################################
#######

function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $[Link]
}

$logpath = $HOME
$logfile = $logpath+"\"+"[Link]"
$seenerror = 0

# Get the ID and security principal of the current user account


$myWindowsID=[[Link]]::GetCurrent()
$myWindowsPrincipal=new-object
[Link]($myWindowsID)

# Get the security principal for the Administrator role


$adminRole=[[Link]]::Administrator

# Check to see if we are currently running "as Administrator"


if ($[Link]($adminRole))
{
# We are running "as Administrator" - so change the title and background color
to indicate this
$[Link] = $[Link] + "(Elevated)"
$[Link] = "DarkRed"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object [Link] "PowerShell";
# Specify the current script path and name as a parameter
$[Link] = $[Link];
# Indicate that the process should be elevated
$[Link] = "runas";
# Start the new process
[[Link]]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}

function log_message([string]$logmessage)
{
$outtext = Get-Date -UFormat "%Y.%m.%d %H:%M:%S"
$outtext = $outtext +" " + $logmessage
$outtext | Out-File $logfile -Append

write-host $outtext

if($logmessage -eq "Errorstack")


{
$_.Exception|format-list -force | Out-File $logfile -Append
}
}

function FGetClusterHostName
{
$cluster = Get-Cluster
$res = $[Link]
return $res
}

function FGetDBSID
{
$regexp = "^[a-zA-Z][a-zA-Z0-9][a-zA-Z0-9]"
$forbidden =
"ADD","ALL","AND","ANY","ASC","AUX","COM","CON","DBA","END","EPS","FOR","GID","IBM"
,"INT","KEY","LOG","LPT","MON","NIX","NOT","NUL","OFF","OMS","PRN","RAW","ROW","SAP
","SET","SGA","SHG","SID","SQL","SYS","TMP","UID","USR","VAR"
do
{
$res = Read-Host -Prompt "Please enter the 3-letter/digit DBSID of the
Oracle database "
$res = $[Link]()

if ($[Link] -ne 3)
{
write-host "invalid length"
$res = ""
}
if ($res -notmatch $regexp)
{
write-host "invalid input"
$res = ""
}
if ($res -contains $forbidden)
{
write-host "invalid/reserved DBSID"
$res = ""
}
} until ($[Link])
return $res
}

function FGetClusterHostName
{
$cluster = Get-Cluster
$res = $[Link]
return $res
}

function FGetDBHostName
{
do
{
$res = Read-Host -Prompt "Please enter the virtual hostname (DNS) of the db
host "
$res = $[Link]()
} until ($[Link])
return $res
}

function FGetPFILE
{
do
{
$res = Read-Host -Prompt "Please enter path and filename of your Oracle
parameter file on the shared cluster disk "
$res = $[Link]()
# if(-Not (Test-Path $res))
# {
# write-host "file not found"
# $res = ""
# }
} until ($[Link])
return $res
}

function FGetYESNO($question)
{
do
{
$res = Read-Host -Prompt $question
$res = $[Link]()

if($res -ne "Y" -and $res -ne "N")


{
write-host "invalid input"
$res = ""
}

} until ($[Link])
return $res
}

function FGetOracleHomePassword
{
write-host "If you are using Oracle release 11.x or 12.x *WITHOUT* Oracle Home
User just press ENTER"
write-host "If you are using Oracle release 12.x *WITH* Oracle Home User please
enter the password of the user"
$res = Read-Host -Prompt "Please enter the password or press ENTER to continue
" -AsSecureString
return $res
}

log_message("=== BEGIN OF SCRIPT EXECUTION ===")

write-host "This script will add the Oracle Database to the Oracle Cluster Group"
write-host
"============================================================================"
try
{
$DBSID = FGetDBSID
$CLDNS = FGetClusterHostName
$DBGROUPNAME = "ORACLE"+$DBSID
$DBPFILE = FGetPFILE
$OHOMEPWD = FGetOracleHomePassword

log_message("DBSID = "+$DBSID)
log_message("CLDNS = "+$CLDNS)
log_message("DBGROUPNAME = "+$DBGROUPNAME)
log_message("DBPFILE = "+$DBPFILE)

write-host
write-host
"============================================================================"
write-host "PLEASE VERIFY IF THE DATA BELOW IS CORRECT BEFORE YOU CONTINUE"
write-host
"----------------------------------------------------------------------------"
write-host "CLUSTERNAME (virtual hostname of the cluster in DNS)
:"$CLDNS
write-host "DBSID (DB SID of the database)
:"$DBSID
write-host "Name of the Oracle Cluster Group
:"$DBGROUPNAME
write-host "Path+Filename of the Oracle initialization parameter file on shared
cluster disk :"$DBPFILE
write-host "This will add the Oracle database "$DBSID" to the cluster group
"$DBGROUPNAME
write-host
write-host "WARNING: If the Oracle database "$DBSID" is already part of a cluster
group it will be remove from the group first"

$CONT = FGetYESNO("DO YOU WANT TO CONTINUE? (y/n) ")

if($cont -eq "Y")


{
log_message("USER ANSWER -> CONTINUE")
log_message("removing OracleClusterResource if already existing...")
Remove-OracleClusterResource -name $DBSID -Verbose -Confirm:$true -Force
-ErrorAction SilentlyContinue

log_message("adding OracleClusterResource...")
$db = Get-OracleClusterResource -Available $DBSID -Verbose -ErrorAction
SilentlyContinue
$[Link] = $DBPFILE
if($[Link] -gt 0)
{
log_message("using Oracle Home User Password")
$[Link]=$OHOMEPWD
}
$db | Add-OracleClusterResource -Group $DBGROUPNAME -Verbose -Confirm:$true
}
else
{
write-host "Exiting without modifications."
}
} # try
catch [Exception]
{
$seenerror = 1
$scripterror = $_.[Link]
$scriptline = $_.[Link]
$scriptname = $_.[Link]

log_message("=== FATAL ERROR ===")


log_message("Script " + $scriptname + " Line "+$scriptline)
log_message("Errormessage "+$scripterror)
log_message("Errorstack")
echo $_.Exception|format-list -force
log_message("===================")
}

if($seenerror -eq 0)
{
log_message("=== END OF SCRIPT EXECUTION: SUCCESS ===")
}
else
{
log_message("=== END OF SCRIPT EXECUTION: WITH ERROR(S) ===")
}

Write-Host "Press any key to continue ..."


$x = $[Link]("NoEcho,IncludeKeyDown")

You might also like