Sunteți pe pagina 1din 5

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

#######
# 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 $Invocation.MyCommand.Path
}

$logpath = $HOME
$logfile = $logpath+"\"+"AddOracleDbToOracleClusterGroup.log"
$seenerror = 0

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


$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object
System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security principal for the Administrator role


$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

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


if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color
to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "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 System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::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 = $cluster.Name
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 = $res.ToUpper()

if ($res.Length -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 ($res.Length)
return $res
}

function FGetClusterHostName
{
$cluster = Get-Cluster
$res = $cluster.Name
return $res
}

function FGetDBHostName
{
do
{
$res = Read-Host -Prompt "Please enter the virtual hostname (DNS) of the db
host "
$res = $res.ToUpper()
} until ($res.Length)
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 = $res.ToUpper()
# if(-Not (Test-Path $res))
# {
# write-host "file not found"
# $res = ""
# }
} until ($res.Length)
return $res
}

function FGetYESNO($question)
{
do
{
$res = Read-Host -Prompt $question
$res = $res.ToUpper()

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


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

} until ($res.Length)
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
$db.ParametersFile = $DBPFILE
if($OHOMEPWD.Length -gt 0)
{
log_message("using Oracle Home User Password")
$db.Home.ServiceUserPassword=$OHOMEPWD
}
$db | Add-OracleClusterResource -Group $DBGROUPNAME -Verbose -Confirm:$true
}
else
{
write-host "Exiting without modifications."
}
} # try
catch [Exception]
{
$seenerror = 1
$scripterror = $_.Exception.Message
$scriptline = $_.InvocationInfo.ScriptLineNumber
$scriptname = $_.InvocationInfo.ScriptName

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 = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")