Wednesday, 1 July 2015

SharePoint 2010: Create new Search Services with Custom DB names Powershell

When you try to create Search services in SharePoint 2010 using the wizard, it gives you nightmare with the Database names with horrible GUIDs...

here is a simple script to ease your life:

## Parameters used

## change the variables below to your own servers and naming conventions



$databaseServerName = "shrpt-db-server"

$searchServerName = "shrpt-apps-server"

$searchSAName = "Search Service Application"

$saAppPoolName = "SharePoint Web Services Default"

$searchDBName = "Search_Services_Application_DB"



## First remove the old search

## This will require an id - namely a guid so get the service guid before doing this



$spapp = Get-SPServiceApplication -Name $searchSAName

Remove-SPServiceApplication $spapp -RemoveData



##START SEARCH

Write-Host "Creating Search Service and Proxy..."

Write-Host "  Starting Services..."

Start-SPEnterpriseSearchServiceInstance $searchServerName

Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $searchServerName



Write-Host "  Creating Search Application..."

$searchApp = New-SPEnterpriseSearchServiceApplication -Name $searchSAName -ApplicationPool $saAppPoolName -DatabaseServer $databaseServerName -DatabaseName $searchDBName

$searchInstance = Get-SPEnterpriseSearchServiceInstance $searchServerName



Write-Host "  Creating Administration Component..."

$searchApp | Get-SPEnterpriseSearchAdministrationComponent | Set-SPEnterpriseSearchAdministrationComponent -SearchServiceInstance $searchInstance



##Crawl



Write-Host "  Creating Crawl Component..."

$InitialCrawlTopology = $searchApp | Get-SPEnterpriseSearchCrawlTopology -Active

$CrawlTopology = $searchApp | New-SPEnterpriseSearchCrawlTopology

$CrawlDatabase = ([array]($searchApp | Get-SPEnterpriseSearchCrawlDatabase))[0]

$CrawlComponent = New-SPEnterpriseSearchCrawlComponent -CrawlTopology $CrawlTopology -CrawlDatabase $CrawlDatabase -SearchServiceInstance $searchInstance

$CrawlTopology | Set-SPEnterpriseSearchCrawlTopology -Active



Write-Host -ForegroundColor white "  Waiting for the old crawl topology to become inactive" -NoNewline

do {write-host -NoNewline .;Start-Sleep 6;} while ($InitialCrawlTopology.State -ne "Inactive")

$InitialCrawlTopology | Remove-SPEnterpriseSearchCrawlTopology -Confirm:$false

Write-Host  



##Query



Write-Host "  Creating Query Component..."

$InitialQueryTopology = $searchApp | Get-SPEnterpriseSearchQueryTopology -Active

$QueryTopology = $searchApp | New-SPEnterpriseSearchQueryTopology -Partitions 1

$IndexPartition= (Get-SPEnterpriseSearchIndexPartition -QueryTopology $QueryTopology)

$QueryComponent = New-SPEnterpriseSearchQuerycomponent -QueryTopology $QueryTopology -IndexPartition $IndexPartition -SearchServiceInstance $searchInstance

$PropertyDatabase = ([array]($searchApp | Get-SPEnterpriseSearchPropertyDatabase))[0]

$IndexPartition | Set-SPEnterpriseSearchIndexPartition -PropertyDatabase $PropertyDatabase

$QueryTopology | Set-SPEnterpriseSearchQueryTopology -Active

## Proxy



Write-Host "  Creating Proxy..."

$searchAppProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name "$searchSAName Proxy" -SearchApplication $searchSAName > $null



#####END SEARCH