manoj´ssharepointblog

SharePoint Blog

Creating Search Service Application using PowerShell In SharePoint

Leave a comment

It might be necessary at some point to use PowerShell to provision search service applications. Below is the script which can be used for creating a Search Service Application With comments

Below is the Power Shell Script which I used for the creation Search Service Application

$searchapp = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction silentlycontinue
//Check if the SearchService Application Already exists
if($searchapp -eq $null)
{
//For the content Source after the creation of the search service application
$contentSourceURls = “URL1″,”URL2″,”URL3″,”URL4”;
// Name of Application pool
$ManagedMetaDataAppPool =”MySearchServiceApplicationPoolAccount”
$ManagedAccount = Get-SPManagedAccount | select -First 1
// 1. Creating a an application pool for your search service application and throwing the object into a variable called $ app:
$ApplicationPool = Get-SPServiceApplicationPool $ManagedMetaDataAppPool -ErrorAction SilentlyContinue
if ($ApplicationPool -eq $null) {
$ApplicationPool = New-SPServiceApplicationPool -Name $ManagedMetaDataAppPool -Account $ManagedAccount
}
// “2. Creating search Service.”
$searchapp = New-SPEnterpriseSearchServiceApplication -name $SearchServiceApplicationName -applicationpool $ApplicationPool
//Note: Add the -partitioned switch after -name if the search service application will be consumed in a hosted environment.
// “3. Creating Search Service proxy.”
$proxy = New-SPEnterpriseSearchServiceApplicationProxy -name $SearchServiceApplicationName -Uri $searchapp.uri.absoluteURI
$si = Get-SPEnterpriseSearchServiceInstance –local
// “4. Start Services search services for SSI”
Start-SpEnterpriseSearchServiceInstance -identity $si
Set-SPEnterpriseSearchAdministrationComponent –searchapplication $searchapp –searchserviceinstance $si
// “5.Create a new Crawl Topology.”
// By default, a search application created in PowerShell has a crawl topology but is missing the following:
// · crawl component
// · query component
// You cannot add a crawl\query component to the default crawl\query topology because it’s set as active and the property is read // only. The easiest way around this is creating a new crawl topology and new query topology. After creating both, they will be // set as inactive by default. This allows for both crawl components to be added to crawl topology and query component to be added // to newly created query topology. Finally, you can set this new crawl topology to active.
$ct = $searchapp | New-SPEnterpriseSearchCrawlTopology
$csid = $SearchApp.CrawlStores | select id
$CrawlStore = $SearchApp.CrawlStores.item($csid.id)
// “6.Create a new Crawl Component.”
New-SPEnterpriseSearchCrawlComponent -crawltopology $ct -crawldatabase $Crawlstore -searchserviceinstance $SearchServiceInstance
net stop sptimerv4
net start sptimerv4
$done = $false
do
{
$ct | Set-SPEnterpriseSearchCrawlTopology -Active -ErrorVariable $err
if ($ct.State -eq “Active”)
{
$done = $true
}
Start-Sleep -Seconds 10
Write-Host $ct.State
}
until ($done -eq $true)
// “7. Create a new Query Topology.”
$qt = $searchapp | new-spenterprisesearchquerytopology -partitions 1
$p1 = ($qt | get-spenterprisesearchindexpartition)
// “8. Create a Query Component.”
new-spenterprisesearchquerycomponent -indexpartition $p1 -querytopology $qt -searchserviceinstance $si
$PSID = $SearchApp.PropertyStores | Select id
$PropDB = $SearchApp.PropertyStores.Item($PSID.id)
$p1 | set-spenterprisesearchindexpartition -PropertyDatabase $PropDB
$done = $false
do
{
$qt | Set-SPEnterpriseSearchQueryTopology -Active -ErrorVariable $err -ErrorAction SilentlyContinue
if ($qt.State -eq “Active”)
{
$done = $true
}
Start-Sleep -Seconds 10
Write-Host $qt.State
}
until ($done -eq $true)
// “9. Adding the content source urls.”
$contentSource = New-SPEnterpriseSearchCrawlContentSource -Name “My ContentSource” -SearchApplication $searchapp -Type SharePoint -SharePointCrawlBehavior “CrawlSites”
foreach ($contentSourceURl in $contentSourceURls)
{
$contentSource.StartAddresses.Add($($Url.Trim(“/”)+$contentSourceURl))
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s