Apply High Availability Search Topology – SharePoint 2013 using Power Shell


high2

$App1 = Get-SPEnterpriseSearchServiceInstance -Identity “app1”
$App2 = Get-SPEnterpriseSearchServiceInstance -Identity “app2”
$fe1 = Get-SPEnterpriseSearchServiceInstance -Identity “wfe1”
$fe2 = Get-SPEnterpriseSearchServiceInstance -Identity “wfe2”
$fe3 = Get-SPEnterpriseSearchServiceInstance -Identity “wfe3”
$fe4 = Get-SPEnterpriseSearchServiceInstance -Identity “wfe4”
$searchName = “High Availability Search Service”
$searchDB = “High Availability Search Service_DB”
$searchAcct = “imam\FarmAdminUserName”
$searchManagedAcct = Get-SPManagedAccount | Where {$_.username-eq ‘imam\FarmAdminUserName’}
$searchAppPoolName = “High Availability Search Services Application Pool”
IF((Get-spserviceapplicationPool | Where {$_.name -eq “High Availability Search Services Application Pool”}).name -ne “High Availability Search Services Application Pool”){
$searchAppPool = New-SPServiceApplicationPool -Name $searchAppPoolName -Account $searchManagedAcct}

Write-Host “Starting Search Service Instances on App1…..”

IF((Get-SPEnterpriseSearchServiceInstance -Identity $App1).Status -eq ‘Disabled’)

{
Start-SPEnterpriseSearchServiceInstance -Identity $App1
Write-Host “Starting Search Service Instance on” $App1.Server.Name
Do
{
Start-Sleep 5;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchServiceInstance -Identity $App1).Status -eq ‘Online’)
Write-Host -ForegroundColor Green “Search Service Instance Started on” $App1.Server.Name
}
ELSE { Write-Host -f Green “Search Service Instance is already running on” $App1.Server.Name }
Write-Host “Starting Search Service Instances on App2…..”

IF((Get-SPEnterpriseSearchServiceInstance -Identity $App2).Status -eq ‘Disabled’)

{
Start-SPEnterpriseSearchServiceInstance -Identity $App2
Write-Host “Starting Search Service Instance on” $App2.Server.Name
Do
{
Start-Sleep 5;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchServiceInstance -Identity $App2).Status -eq ‘Online’)
Write-Host -ForegroundColor Green “Search Service Instance Started on” $App2.Server.Name
}
ELSE { Write-Host -f Green “Search Service Instance is already running on” $App2.Server.Name }
Write-Host “Starting Search Service Instances on fe1…..”

IF((Get-SPEnterpriseSearchServiceInstance -Identity $fe1).Status -eq ‘Disabled’)

{
Start-SPEnterpriseSearchServiceInstance -Identity $fe1
Write-Host “Starting Search Service Instance on” $fe1.Server.Name
Do
{
Start-Sleep 5;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchServiceInstance -Identity $fe1).Status -eq ‘Online’)
Write-Host -ForegroundColor Green “Search Service Instance Started on” $fe1.Server.Name
}
ELSE { Write-Host -f Green “Search Service Instance is already running on” $fe1.Server.Name }
Write-Host “Starting Search Service Instances on fe2…..”

IF((Get-SPEnterpriseSearchServiceInstance -Identity $fe2).Status -eq ‘Disabled’)

{
Start-SPEnterpriseSearchServiceInstance -Identity $fe2
Write-Host “Starting Search Service Instance on” $fe2.Server.Name
Do
{
Start-Sleep 5;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchServiceInstance -Identity $fe2).Status -eq ‘Online’)
Write-Host -ForegroundColor Green “Search Service Instance Started on” $fe2.Server.Name
}
ELSE { Write-Host -f Green “Search Service Instance is already running on” $fe2.Server.Name }
Write-Host “Starting Search Service Instances on fe3…..”

IF((Get-SPEnterpriseSearchServiceInstance -Identity $fe2).Status -eq ‘Disabled’)

{
Start-SPEnterpriseSearchServiceInstance -Identity $fe3
Write-Host “Starting Search Service Instance on” $fe3.Server.Name
Do
{
Start-Sleep 5;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchServiceInstance -Identity $fe3).Status -eq ‘Online’)
Write-Host -ForegroundColor Green “Search Service Instance Started on” $fe3.Server.Name
}
ELSE { Write-Host -f Green “Search Service Instance is already running on” $fe3.Server.Name }
Write-Host “Starting Search Service Instances on fe4…..”

IF((Get-SPEnterpriseSearchServiceInstance -Identity $fe4).Status -eq ‘Disabled’)

{
Start-SPEnterpriseSearchServiceInstance -Identity $fe4
Write-Host “Starting Search Service Instance on” $fe4.Server.Name
Do
{
Start-Sleep 5;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchServiceInstance -Identity $fe4).Status -eq ‘Online’)
Write-Host -ForegroundColor Green “Search Service Instance Started on” $fe4.Server.Name
}
ELSE { Write-Host -f Green “Search Service Instance is already running on” $fe4.Server.Name }

Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $App1.server.Name
Do {
Start-Sleep 3;
Write-host -NoNewline “.”
}

While ((Get-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance | Where {$_.Server.Name -eq $App1.server.Name}).status -ne ‘Online’)
Write-Host -ForegroundColor Green “Query and Site Settings Service Instance Started on” $App1.Server.Name

Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $App2.server.Name
Do {
Start-Sleep 3;
Write-host -NoNewline “.”
}
While ((Get-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance | Where {$_.Server.Name -eq $App2.server.Name}).status -ne ‘Online’)
Write-Host -ForegroundColor Green “Query and Site Settings Service Instance Started on” $App2.Server.Name
$searchAppPool = Get-SPServiceApplicationPool -Identity “High Availability Search Services Application Pool”

IF ((Get-SPEnterpriseSearchServiceApplication).Status -ne ‘Online’){
Write-Host “Provisioning. Please wait…”
$searchApp = New-SPEnterpriseSearchServiceApplication -Name $searchName -ApplicationPool $searchAppPool -AdminApplicationPool $searchAppPool -DatabaseName $searchDB
DO {start-sleep 2;
write-host -nonewline “.” } While ( (Get-SPEnterpriseSearchServiceApplication).status -ne ‘Online’)
Write-Host -f green “Provisioned Search Service Application”
} ELSE { write-host -f green “Search Service Application already provisioned.”
$searchApp = Get-SPEnterpriseSearchServiceApplication
}

$AdminComponent = $searchApp | Get-SPEnterpriseSearchAdministrationComponent | Set-SPEnterpriseSearchAdministrationComponent -SearchServiceInstance $App1
$initialTopology = Get-SPEnterpriseSearchTopology -SearchApplication $searchApp
$cloneTopology = New-SPEnterpriseSearchTopology -SearchApplication $searchApp -Clone -SearchTopology $initialTopology
$AdminTopology = New-SPEnterpriseSearchAdminComponent -SearchServiceInstance $App1 -SearchTopology $cloneTopology
$CrawlTopology = New-SPEnterpriseSearchCrawlComponent -SearchServiceInstance $App1 -SearchTopology $cloneTopology
$AnalyticsTopology = New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchServiceInstance $App1 -SearchTopology $cloneTopology
$ContentProcessingTopology = New-SPEnterpriseSearchContentProcessingComponent -SearchServiceInstance $App1 -SearchTopology $cloneTopology
$IndexTopology = New-SPEnterpriseSearchIndexComponent -SearchServiceInstance $App1 -SearchTopology $cloneTopology -IndexPartition 0
$AdminTopology = New-SPEnterpriseSearchAdminComponent -SearchServiceInstance $App2 -SearchTopology $cloneTopology
$CrawlTopology = New-SPEnterpriseSearchCrawlComponent -SearchServiceInstance $App2 -SearchTopology $cloneTopology
$AnalyticsTopology = New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchServiceInstance $App2 -SearchTopology $cloneTopology
$ContentProcessingTopology = New-SPEnterpriseSearchContentProcessingComponent -SearchServiceInstance $App2 -SearchTopology $cloneTopology
$IndexTopology = New-SPEnterpriseSearchIndexComponent -SearchServiceInstance $App2 -SearchTopology $cloneTopology -IndexPartition 0
$fe1 = Get-SPEnterpriseSearchServiceInstance -Identity “sp-ife1”
$QueryTopology = New-SPEnterpriseSearchQueryProcessingComponent -SearchServiceInstance $fe1 -SearchTopology $cloneTopology

$fe2 = Get-SPEnterpriseSearchServiceInstance -Identity “sp-ife2”
$QueryTopology = New-SPEnterpriseSearchQueryProcessingComponent -SearchServiceInstance $fe2 -SearchTopology $cloneTopology

$fe3 = Get-SPEnterpriseSearchServiceInstance -Identity “sp-efe1”
$QueryTopology = New-SPEnterpriseSearchQueryProcessingComponent -SearchServiceInstance $fe3 -SearchTopology $cloneTopology

$fe4 = Get-SPEnterpriseSearchServiceInstance -Identity “sp–efe2”
$QueryTopology = New-SPEnterpriseSearchQueryProcessingComponent -SearchServiceInstance $fe4 -SearchTopology $cloneTopology
Set-SPEnterpriseSearchTopology -Identity $cloneTopology

$initialTopology = Get-SPEnterpriseSearchTopology -SearchApplication $searchApp | where {($_.State) -eq “Inactive”}
Remove-SPEnterpriseSearchTopology -Identity $initialTopology -Confirm:$false

$searchAppProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name “$searchName Proxy” -SearchApplication $searchApp