Albert Jan Schot
 

Start crawling with PowerShell part 2

05

Oct

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!

  1: # Get or create SPEnterpriseSearchMetadataCrawledProperty 
  2: if (Get-SPEnterpriseSearchMetadataCrawledProperty 
  3:         -SearchApplication $searchapp -Name "proptocreate" 
  4:         -ea "silentlycontinue") {
  5:   $crawlprop = Get-SPEnterpriseSearchMetadataCrawledProperty 
  6:           -SearchApplication $searchapp -Name "proptocreate" 
  7: } else {
  8:   $crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty 
  9:           -SearchApplication $searchapp -VariantType 31 
 10:           -Name proptocreate -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"
 11: }
 12: 
 13: if (Get-SPEnterpriseSearchMetadataManagedProperty 
 14:         -SearchApplication $searchapp -Identity "MetadataPorpertyExample" 
 15:         -ea "silentlycontinue") {
 16:    write-host -f Green "MetaProperty already exists, 
 17:           we delete it so it can be reacreated "
 18:    $prop = Get-SPEnterpriseSearchMetadataManagedProperty 
 19:           -SearchApplication $searchapp -Identity "PublishingPageContent"
 20:    $prop.DeleteAllMappings()
 21:    $prop.Delete()
 22:    $searchapp.Update()
 23: } else {}
 24: 
 25: write-host -f Green "Try to create MetaProperty"
 26: $prop = New-SPEnterpriseSearchMetadataManagedProperty
 27:    -SearchApplication $searchapp -Name "PublishingPageContent" -Type 1
 28: $prop.EnabledForScoping = $true
 29: $prop.Update()
 30: 
 31: New-SPEnterpriseSearchMetadataMapping -SearchApplication 
 32: $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:

  1: if (Get-SPEnterpriseSearchQueryScope -SearchApplication $searchapp 
  2:         -Identity "ScopeExample" -ea "silentlycontinue") {
  3:   write-host -f Green "Scope already exists, 
  4:            we delete it so it can be reacreated "
  5:   $scope= Get-SPEnterpriseSearchQueryScope -SearchApplication 
  6:             $searchapp -Identity "ScopeExample" 
  7:   $scope.Delete();
  8:   $searchapp.Update();
  9: } else {}
 10: 
 11: # Create "ScopeExample" scope
 12: $scope = New-SPEnterpriseSearchQueryScope -Name "ScopeExample" 
 13:    -Description "Doorzoek alle informatie" -SearchApplication $searchapp 
 14:    -DisplayInAdminUI $true
 15: 
 16: New-SPEnterpriseSearchQueryScopeRule -RuleType AllContent 
 17:   -Url $url -scope $scope

 

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

  1: Write-Host -f Green "Delete current index ...";
  2: $searchapp.Reset($true, $true)
  3: 
  4: Write-Host -f Green "Start indexing and scopecompilation ";
  5: $searchapp.StartScopesCompilation()
  6: 
  7: $CrawlContents = Get-SPEnterpriseSearchServiceApplication 
  8:     | Get-SPEnterpriseSearchCrawlContentSource
  9: 
 10: foreach ($crawlcontent in $CrawlContents) 
 11:     { $crawlcontent.StartFullCrawl(); }

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot