Friday 7 November 2014

Export and Import Search Scopes and Rules using PowerShell scripts

I had a requirement to export and import the search scopes and rules from one farm to another by using the PowerShell scripts. The script written below will export the search scopes and search rules into xml file.

Export Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell

$SSAName = "Search Service Application"

#----------------Getting Scope function---------------------------------------------
Function GetSSAScopes()
{
    #Log in Excel file
    $xmlPath = "$((pwd).path)/CreateScopes_Rules.xml";
$resultInXml = new-object xml
$decl = $resultInXml.CreateXmlDeclaration("1.0", $null, $null)
$rootNode = $resultInXml.CreateElement("Scopes");
$resultInXml.InsertBefore($decl, $resultInXml.DocumentElement)
$resultInXml.AppendChild($rootNode);
 
    $ssa=Get-SPEnterPriseSearchServiceApplication -Identity $SSAName
    $scopeCollection=Get-SPEnterpriseSearchQueryScope -SearchApplication $ssa
    #Add SSA Name element
    $XSSAName = $resultInXml.CreateElement("SSAName")
$XSSAName.SetAttribute("Name",$SSAName)
$rootNode.AppendChild($XSSAName);
    #Add ScopeRules element
    $XScopeRules= $resultInXml.CreateElement("ScopeRules")
$rootNode.AppendChild($XScopeRules);
 
    foreach($scope in $scopeCollection)
    {  
        if($scope -ne $null)
        {      
            write-host -ForegroundColor Magenta Adding scope $scope.Name in xml file
            $Xscope = $resultInXml.CreateElement("Scope")
$Xscope.SetAttribute("Name",$scope.Name)
$Xscope.SetAttribute("Description",$scope.Description)
$rootNode.AppendChild($Xscope);          
            write-host -ForegroundColor Green $scope.Name is added successfully
         
            write-host -ForegroundColor Green “Get all scope rules..”
            $allScopeRules = Get-SPEnterpriseSearchQueryScopeRule -SearchApplication $searchServiceApplicationName -Scope $scope
            if($allScopeRules -ne $null)
            {
              foreach($rule in $allScopeRules)
              {          
                #Adding eack rule in the xml file
                write-host -ForegroundColor Magenta Adding rule in xml file..
                $Xscoperule = $resultInXml.CreateElement("ScopeRule")
                $Xscoperule.SetAttribute("ScopeName",$scope.Name)  
   $Xscoperule.SetAttribute("Type",$rule.RuleType)
                $Xscoperule.SetAttribute("Behaviour",$rule.FilterBehavior)            
                $Xscoperule.SetAttribute("ManagedProperty",$rule.Property.Name)
                $Xscoperule.SetAttribute("MatchingString",$rule.MatchingString)
                $Xscoperule.SetAttribute("RuleType",$rule.UrlRuleType)
                $Xscoperule.SetAttribute("PropertyValue",$rule.value)
                $Xscoperule.SetAttribute("URL",$rule.Url)                                                              
   $XScopeRules.AppendChild($Xscoperule);          
                write-host -ForegroundColor Green Rule is added successfully.
               }
            }      
        }    
    }
 
    $resultInXml.Save($xmlPath)
}
#Getting scopes from SSA
GetSSAScopes

Once we will generate the 'CreateScopes_Rules.xml' file, run the script below to move the search rules and scopes to another farm environment.

Import Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell
#----------------Get the xml file---------------------------------------------------------------
# Check path name for the XML file
#[xml]$xmlData=Get-Content "C:\\Karthik\Scripts\CreateScopes_Rules.xml"
[xml]$xmlData=Get-Content "$((pwd).path)/CreateScopes_Rules.xml"

#----------------Create New Scope function---------------------------------------------
Function CreateNewScope()
{
    write-host -ForegroundColor green "SSA Name: " $xmlData.Scopes.SSAName.Name
    $ssa=Get-SPEnterPriseSearchServiceApplication -Identity $xmlData.Scopes.SSAName.Name
    $scopeCollection=Get-SPEnterpriseSearchQueryScope -SearchApplication $ssa
 
    foreach($_ in $xmlData.Scopes.Scope)
    {
        $newScopeName=$_.Name
        $newScopeDescription=$_.Description      
        $scope=Get-SPEnterpriseSearchQueryScope -Identity $newScopeName -SearchApplication $ssa -ea "silentlycontinue"
     
        if($scope -eq $null)
        {
            write-host -ForegroundColor Magenta Creating scope.. $_.scope
            New-SPEnterpriseSearchQueryScope -Name $newScopeName -Description $newScopeDescription -SearchApplication $ssa -DisplayInAdminUI $true
            write-host -ForegroundColor Green $newScopeName is created successfully
        }
        else
        {
            write-host -ForegroundColor yellow $newScopeName scope already exists
            write-host -ForegroundColor yellow Deleting $newScopeName scope..
            Remove-SPEnterpriseSearchQueryScope -Identity $newScopeName -SearchApplication $ssa -confirm:0
            write-host -ForegroundColor green $newScopeName scope is deleted successfully
            write-host -ForegroundColor Magenta Creating $newScopeName scope..
            New-SPEnterpriseSearchQueryScope -Name $newScopeName -Description $newScopeDescription -SearchApplication $ssa -DisplayInAdminUI $true
            write-host -ForegroundColor green $newScopeName scope is successfully created
        }
    }  
}
Function CreateNewRule()
{
    $ssa=Get-SPEnterPriseSearchServiceApplication -Identity $xmlData.Scopes.SSAName.Name
    #$scopeCollection=Get-SPEnterpriseSearchQueryScope -SearchApplication $ssa
 
     foreach($_ in $xmlData.Scopes.ScopeRules.ScopeRule)
      {
            $ScopeName=$_.ScopeName
            $scope = Get-SPEnterpriseSearchQueryScope -Identity $ScopeName -SearchApplication $ssa                    
            $newType=$_.Type          
            $newRuleBehaviour=$_.Behaviour          
            $newRuleURL=$_.URL          
            $newManagedProperty=$_.ManagedProperty          
            $newMatchingString=$_.MatchingString          
            $newPropertyValue=$_.PropertyValue          
            $newRuleType=$_.RuleType
         
         
            if($newType -eq "AllContent")
            {
                  Write-Host -ForegroundColor Magenta Creating new rule..
                  New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -url $newRuleURL -scope $scope -SearchApplication $ssa
                  Write-Host -ForegroundColor Green New Rule created successfully
            }
            else
            {
                  if($newType -eq "URL")
                  {
                        Write-Host -ForegroundColor Magenta Creating new rule..                  
                        New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -MatchingString $newMatchingString -UrlScopeRuleType $newRuleType -Filterbehavior $newRuleBehaviour -url $newRuleURL -scope $scope -SearchApplication $ssa
                        Write-Host -ForegroundColor Green New Rule created successfully
                  }
                  else
                  {
                        if($newType -eq "PropertyQuery")
                        {
                              Write-Host -ForegroundColor Magenta Creating new rule..
                              New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -ManagedProperty $newManagedProperty -PropertyValue $newPropertyValue -Filterbehavior $newRuleBehaviour -url $newRuleURL -scope $scope -searchapplication $ssa
                              Write-Host -ForegroundColor Green New Rule created successfully
                        }
                        else
                        {
                              Write-Host No more rules to create
                        }
                  }
            }
      }    
}


#Creating new scopes in SSA
CreateNewScope
#Creating new rules in SSA
CreateNewRule

Sample CreateScopes_Rules.xml:

<?xml version="1.0"?>
<Scopes>
  <SSAName Name="Search Service Application" />
  <ScopeRules>
    <ScopeRule ScopeName="People" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="urn:content-class:SPSPeople" URL="" />
    <ScopeRule ScopeName="All Sites" Type="AllContent" Behaviour="Include" ManagedProperty="" MatchingString="" RuleType="" PropertyValue="" URL="" />
    <ScopeRule ScopeName="All Sites" Type="PropertyQuery" Behaviour="Exclude" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="urn:content-class:SPSPeople" URL="" />
    <ScopeRule ScopeName="Test" Type="Url" Behaviour="Require" ManagedProperty="" MatchingString="http://servername:7575/Lists/SupervisoryBoard" RuleType="Folder" PropertyValue="" URL="" />
    <ScopeRule ScopeName="CTSearch" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="CTSearch" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="ContentType" MatchingString="" RuleType="" PropertyValue="BoardMeeting" URL="" />
    <ScopeRule ScopeName="SBScope" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_DocumentLibrary" URL="" />
    <ScopeRule ScopeName="Copy of People" Type="PropertyQuery" Behaviour="Include" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="urn:content-class:SPSPeople" URL="" />
    <ScopeRule ScopeName="All Sites Search" Type="AllContent" Behaviour="Include" ManagedProperty="" MatchingString="" RuleType="" PropertyValue="" URL="" />
    <ScopeRule ScopeName="All Sites Search" Type="Url" Behaviour="Exclude" ManagedProperty="" MatchingString="http://servername:7576" RuleType="Folder" PropertyValue="" URL="" />
    <ScopeRule ScopeName="All Sites Search" Type="PropertyQuery" Behaviour="Exclude" ManagedProperty="contentclass" MatchingString="" RuleType="" PropertyValue="STS_ListItem_PictureLibrary" URL="" />
    <ScopeRule ScopeName="IT Search" Type="Url" Behaviour="Include" ManagedProperty="" MatchingString="http://servername:7576" RuleType="Folder" PropertyValue="" URL="" />
    <ScopeRule ScopeName="IT Search" Type="PropertyQuery" Behaviour="Exclude" ManagedProperty="Filename" MatchingString="" RuleType="" PropertyValue="mod-view.aspx" URL="" />
  </ScopeRules>
  <Scope Name="People" Description="Search for people." />
  <Scope Name="All Sites" Description="Search for everything available for searching." />
  <Scope Name="Global Query Exclusion" Description="Everything that should be omitted from all searches by default." />
  <Scope Name="Rank Demoted Sites" Description="Sites whose ranks will be demoted in click-distance calculation." />
  <Scope Name="Test" Description="" />
  <Scope Name="CTSearch" Description="" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="SBScope" Description="Search Scope for Supervisory Board Search" />
  <Scope Name="Copy of People" Description="Search for people." />
  <Scope Name="All Sites Search" Description="Searches all content" />
  <Scope Name="IT Search" Description="Only searches IT pages in the Intranet" />

</Scopes>


ExportSearchScopes.bat

echo off
powershell.exe -command .\ExportSearchScopes.ps1

pause

ImportSearchScopes.bat

echo off
powershell.exe -command .\ImportSearchScopes.ps1
pause


1 comment:

  1. Great article

    I have a couple of questions :
    1. In the import script I get an error saying Url is empty - should we manually fill in the Url
    New-SPEnterpriseSearchQueryScopeRule -RuleType $newType -MatchingString $newMatchingString -UrlScopeRuleType $newRuleType -Filterbehavior $newRuleBehaviour -url $newRuleURL -scope $scope -SearchApplication $ssa

    2. Matching string in CreateScopes_Rules.xml - should the url be changed to the new environment Url?

    ReplyDelete