Automated Cluster setup script
Automated Cluster setup script I wrote. I found that there were already scripts out there that did pieces of it, but none I found that ran the test parsed it and then continue to building it. It could probably be better and there are probably more "status codes" it could throw, but it has worked well enough for us so far. I won't post the whole thing as there is some stuff I would have to rip out for proprietary reasons, but at least the portion that looks at the report and continues. Logwrite is a custom function we use to write a log file. We are setting up AOA clusters with no shared storage so that is why we use the -nostorage during the cluster build.
######################################################################
# Configure Cluster
######################################################################
LogWrite "Testing cluster configuration this will take a few minutes on $arrcomputers" -color white
Test-Cluster -node $ArrComputers -ReportName $path\Clusterreport
$report =Test-Path -path "$path\Clusterreport.mht"
$uri = "$path\Clusterreport.mht"
#Needed because sometimes Server 2016 makes it an htm.
if ($report -eq $false)
{
$report = Test-Path -path "$path\Clusterreport.htm"
if ($report = $true)
{
$uri="$path\Clusterreport.htm"
}
}
$html = Invoke-WebRequest -URI $URI
if (($html.RawContent -match 'The configuration appears to be suitable for clustering.') -or ($html.RawContent -match 'configuration is suitable for clustering') -or ($html.RawContent -match 'Testing has completed for the tests you selected. You should review the warnings in the Report.'))
{
LogWrite "Report indicates that $ArrComputers are ready for clustering" -Color Green
do {
$clustername = Read-Host "Enter Cluster object Name (Example xxxxxxxx)"
if (( $clustername -eq [String]::Empty))
{LogWrite "Cluster name is empty. Please Input" -Color Red}
$exists=get-cluster $clustername -Erroraction silentlycontinue
if ( $exists -ne $null)
{
LogWrite "Cluster $clustername already exists. Exiting script" -Color Red
return
}
try
{
#Test to make sure IP is empty
Do{
$clusterIP= Read-Host "Enter the name of the Cluster IP"
LogWrite "Testing Cluster IP $clusterIP to make sure it is not in use" -Color White
$IPTest=Test-Connection $clusterIP -Quiet
if ($IPTest -eq $true) {LogWrite "IP $clusterIP is in use verify and enter again" -Color red}
Else {LogWrite "IP $clusterIP not in use" -Color Green}
}
Until ($IPTest -eq $false)
New-Cluster -name "$clustername" -node $ArrComputers -StaticAddress "$clusterIP" -nostorage -ErrorAction Stop|Out-Null
Logwrite "Cluster build succeeded on $clustername with IP $clusterIP" -color Green
}
Catch
{
LogWrite "Cluster Installation failed Review parameters and try again" -color Red
}
}
While (( $clustername -eq [String]::Empty) -or ($clusterIP -eq [String]::Empty ))
}
foreach ($computer in $ArrComputers) {
foreach ($instance in $instances){
try {
Enable-SqlAlwaysOn -serverinstance "$computer\$instance" -Force -ErrorAction Stop
LogWrite "$computer SQL Always on enabled" -Color Green
$agentfull =$agentname + $instance
Get-service -computername $computer -name $agentfull |Start-Service -Verbose
}
catch {
LogWrite "$computer doesn't exist, SQL instance is not installed or enablement of Always on failed, try again" -Color Red
$allExist = $false
}
}
}
######################################################################
# Configure Cluster
######################################################################
LogWrite "Testing cluster configuration this will take a few minutes on $arrcomputers" -color white
Test-Cluster -node $ArrComputers -ReportName $path\Clusterreport
$report =Test-Path -path "$path\Clusterreport.mht"
$uri = "$path\Clusterreport.mht"
#Needed because sometimes Server 2016 makes it an htm.
if ($report -eq $false)
{
$report = Test-Path -path "$path\Clusterreport.htm"
if ($report = $true)
{
$uri="$path\Clusterreport.htm"
}
}
$html = Invoke-WebRequest -URI $URI
if (($html.RawContent -match 'The configuration appears to be suitable for clustering.') -or ($html.RawContent -match 'configuration is suitable for clustering') -or ($html.RawContent -match 'Testing has completed for the tests you selected. You should review the warnings in the Report.'))
{
LogWrite "Report indicates that $ArrComputers are ready for clustering" -Color Green
do {
$clustername = Read-Host "Enter Cluster object Name (Example xxxxxxxx)"
if (( $clustername -eq [String]::Empty))
{LogWrite "Cluster name is empty. Please Input" -Color Red}
$exists=get-cluster $clustername -Erroraction silentlycontinue
if ( $exists -ne $null)
{
LogWrite "Cluster $clustername already exists. Exiting script" -Color Red
return
}
try
{
#Test to make sure IP is empty
Do{
$clusterIP= Read-Host "Enter the name of the Cluster IP"
LogWrite "Testing Cluster IP $clusterIP to make sure it is not in use" -Color White
$IPTest=Test-Connection $clusterIP -Quiet
if ($IPTest -eq $true) {LogWrite "IP $clusterIP is in use verify and enter again" -Color red}
Else {LogWrite "IP $clusterIP not in use" -Color Green}
}
Until ($IPTest -eq $false)
New-Cluster -name "$clustername" -node $ArrComputers -StaticAddress "$clusterIP" -nostorage -ErrorAction Stop|Out-Null
Logwrite "Cluster build succeeded on $clustername with IP $clusterIP" -color Green
}
Catch
{
LogWrite "Cluster Installation failed Review parameters and try again" -color Red
}
}
While (( $clustername -eq [String]::Empty) -or ($clusterIP -eq [String]::Empty ))
}
foreach ($computer in $ArrComputers) {
foreach ($instance in $instances){
try {
Enable-SqlAlwaysOn -serverinstance "$computer\$instance" -Force -ErrorAction Stop
LogWrite "$computer SQL Always on enabled" -Color Green
$agentfull =$agentname + $instance
Get-service -computername $computer -name $agentfull |Start-Service -Verbose
}
catch {
LogWrite "$computer doesn't exist, SQL instance is not installed or enablement of Always on failed, try again" -Color Red
$allExist = $false
}
}
}
Comments
Post a Comment