0

We are going to reset the Search Index in our SharePoint 2010 environment, I guess this will remove all the Managed properties(Metadata properties) from the search service application. So, would like to backup the Managed property settings and restore after contents are re-crawled.

Please let me know if there is a powershell script to achieve this?

1 Answer 1

1
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}


function ResetIndex {
    param ($searchapp)

    #Reset index with the following options in brackets:
    #Disable Alerts $true/$false
    #Ignore unreachable server $true/$false
    try
    {
        Write-Host "Attempting to reset the index...please wait"
        $searchapp.Reset($false, $false)
        Write-Host "Index successfully reset" -ForegroundColor Blue
    }
    catch
    {
        Write-Host "There was a problem resetting the index:" $_ -ForegroundColor Red
    }

}


function BackupMetadataProperties {
    param ($searchapp, $MetadataPropertyLogs)

    "PID;Name;Description;Type;HasMultiValue;IncludeAll;EnabledForScoping;IsSortable;PutInPropertyBlob;MappingCount;CrawledProperty;Category;VariantType" | out-file $MetadataPropertyLogs;

    Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp |% {
    $prop= $_;
    $propData = $prop.Pid.ToString() + ";" + $prop.Name.ToString() + ";" + $prop.Description.ToString() + ";" + $prop.ManagedType.ToString() + ";" + $prop.HasMultipleValues.ToString() + ";" + $prop.RespectPriority + ";" + $prop.EnabledForScoping.ToString() + ";" + $prop.MaxCharactersInPropertyStoreIndex.ToString() + ";" + $prop.PutInPropertyBlob.ToString() + ";" + $prop.GetMappings().Count.ToString(); 

        $prop.GetMappedCrawledProperties($prop.GetMappings().Count) |% {
        $propMapping = $propData  + ";" + $_.Name.ToString() + ";" + $_.CategoryName.ToString() + ";" + $_.VariantType.ToString();
        $propMapping | out-file $MetadataPropertyLogs -append;
        }
    }
}


function StartFullCrawl {
    param ( $SearchApp, $ContentSource)

    $CrawlContentSource = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchApp  -Identity $ContentSource;
        if ($CrawlContentSource.CrawlStatus -ne "Idle")
        {
            Write-Host "Stopping currently running crawl for content source $($CrawlContentSource.Name)...";
            $CrawlContentSource.StopCrawl();

            do { Start-Sleep -Seconds 60 }
            while ($CrawlContentSource.CrawlStatus -ne "Idle");
        }

        Write-Host "Starting full crawl for content source $($CrawlContentSource.Name)...";
        $CrawlContentSource.StartFullCrawl();
}

function checkCrawlStatus {
    param ($searchapp)
    $CrawlContentSource = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchApp  -Identity $ContentSource;
    if ($CrawlContentSource.CrawlStatus -ne "Idle")
        {   
        do { Start-Sleep -Seconds 300 }
            while ($CrawlContentSource.CrawlStatus -ne "Idle");
    }
}


function CreateMetadataProperty {
    param ( $searchapp, $MetadataPropertyName, $Description, $Type, $HasMultiValue, $IncludeAll, $EnabledForScoping, $IsSortable, $PutInPropertyBlob, $CrawledProperty, $Category, $VariantType)

    $category = Get-SPEnterpriseSearchMetadataCategory  -SearchApplication $searchapp –Identity $Category ;
    $crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType $VariantType -Name $CrawledProperty;

    If (!($crawledproperty -eq $null))
    {
        $managedproperty = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp  -Identity $MetadataPropertyName -ErrorAction SilentlyContinue;
        if( ($managedproperty -eq $null))
        {
            $managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $MetadataPropertyName -Type $Type -Description $Description -EnabledForScoping $EnabledForScoping -RespectPriority $IncludeAll -Confirm:$false;
            If (!($IsSortable -eq 0))
            {  
                $managedproperty.MaxCharactersInPropertyStoreIndex = $IsSortable;
                $managedproperty.Update();
            }

            If ($HasMultiValue -eq $True)
            {  
                $managedproperty.HasMultipleValues = $true;
                $managedproperty.Update();
            }

            If ($PutInPropertyBlob -eq $True)
            {  
                $managedproperty.PutInPropertyBlob = $true;
                $managedproperty.Update();
            }
        }
        New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty
    }
    else
    {
        write-host -f Red "Crawl property is missing, you might need to recrawl your content source to get it."
        "Crawl property $CrawledProperty is missing, you might need to recrawl your content source to get it." | out-file $ErrorLog -append;    
    }

    if (!$?)
    {
        "An error has occurred" | out-file $ErrorLog -append;   
    }
}


function Main {

BackupMetadataProperties $searchapp $MetadataPropertyLogs;
ResetIndex $searchapp;

$contentSource = @("Local SharePoint sites","People Content Search","Site Registry","Social Data");

$contentSource |% {
    StartFullCrawl $searchapp $_.ToString();
}

$contentSource |% {
    checkCrawlStatus $searchapp $_.ToString();
}

$dictionary = @{}
$dictionary.Add("Text",1); 
$dictionary.Add("Integer",2);
$dictionary.Add("Decimal",3);
$dictionary.Add("DateTime",4);
$dictionary.Add("YesNo",5);

$MetadataProperties = GC $MetadataPropertyLogs;
$MetadataProperties |% {
    [Array]$MetadataProperty = $_.ToString() -split ";"
    $MetadataPropertyName = $MetadataProperty[1];
    $Description =  $MetadataProperty[2];
    $Type = $dictionary[$MetadataProperty[3]];
    $HasMultiValue = [System.Convert]::ToBoolean($MetadataProperty[4]); 
    $IncludeAll = [System.Convert]::ToBoolean($MetadataProperty[5]);
    $EnabledForScoping = [System.Convert]::ToBoolean($MetadataProperty[6]);
    $IsSortable = $MetadataProperty[7];
    $PutInPropertyBlob = [System.Convert]::ToBoolean($MetadataProperty[8]);
    $CrawledProperty = $MetadataProperty[10];
    $Category = $MetadataProperty[11];
    $VariantType = $MetadataProperty[12];

    CreateMetadataProperty $searchapp  $MetadataPropertyName $Description $Type $HasMultiValue $IncludeAll $EnabledForScoping $IsSortable $PutInPropertyBlob $CrawledProperty $Category $VariantType; 
}
}

$searchapp = Get-SPEnterpriseSearchServiceApplication;
$MetadataPropertyLogs = "D:\Scripts\searchReIndex\propData_Sample.txt";
$ErrorLog = "D:\Scripts\searchReIndex\Error.txt";

Main;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.