Start crawling with PowerShell part 2

Start crawling with PowerShell part 2 header image

A while ago I blogged about starting a new crawl with PowerShell in this post, but today I got a bit further, each rollout I do on my dev machine a whole new site collection gets created using PowerShell, after that there is some test content provisioned (also using PowerShell), and finally the search settings are added. It kinda feels like everything is done through PowerShell. The point however is that I not only want to start a new crawl, but make sure that ‘old’ content is deleted. Step by step; I create some Crawled properties, create some Managed Metadata properties, create some scopes and finally delete the old content and crawl the new one.

The first part creating crawled properties and metadata properties i used this blog post. Getting me something like the following, that checks if the crawled property exists and if so gets it, otherwise creates it, then setting it into a metadata property that can be used. Nice to know is that the –propset property even though the documentation says its optional is in fact required!

# Get or create SPEnterpriseSearchMetadataCrawledProperty
if (Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name "proptocreate" -ea "silentlycontinue") {
  $crawlprop = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name "proptocreate"
}
else {
  $crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -VariantType 31  -Name proptocreate -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"
}
if (Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity "MetadataPorpertyExample" -ea "silentlycontinue")
{
write-host -f Green "MetaProperty already exists, we delete it so it can be reacreated "
$prop = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity "PublishingPageContent"
$prop.DeleteAllMappings()
$prop.Delete()
$searchapp.Update()
}
else {}

write-host -f Green "Try to create MetaProperty"
$prop = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name "PublishingPageContent" -Type 1
$prop.EnabledForScoping = $true
$prop.Update()

New-SPEnterpriseSearchMetadataMapping -SearchApplication
$searchapp -ManagedProperty $prop -CrawledProperty $crawlprop

After that i can create scopes (my colleague Peter found it out), where I check if a scope already exists and if so delete it so it can be recreated again:

if (Get-SPEnterpriseSearchQueryScope -SearchApplication $searchapp -Identity "ScopeExample" -ea "silentlycontinue") {
write-host -f Green "Scope already exists, we delete it so it can be reacreated "
$scope= Get-SPEnterpriseSearchQueryScope -SearchApplication $searchapp -Identity "ScopeExample"

$scope.Delete();
$searchapp.Update();
} else {}

# Create "ScopeExample" scope
$scope = New-SPEnterpriseSearchQueryScope -Name "ScopeExample"  -Description "Doorzoek alle informatie" -SearchApplication $searchapp -DisplayInAdminUI $true

New-SPEnterpriseSearchQueryScopeRule -RuleType AllContent -Url $url -scope $scope

And after that in finally can reset my index, and make sure everything is found:

Write-Host -f Green "Delete current index ...";
$searchapp.Reset($true, $true)

Write-Host -f Green "Start indexing and scopecompilation ";
$searchapp.StartScopesCompilation()
$CrawlContents = Get-SPEnterpriseSearchServiceApplication | Get-SPEnterpriseSearchCrawlContentSource

foreach ($crawlcontent in $CrawlContents) {
  $crawlcontent.StartFullCrawl();
}
Loading comments…