PowerShell: SharePoint - Add Crawled Property Mapping

Updated on 2/26/2015
$Assem = ( 
"Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
"Microsoft.Office.Server, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
"Microsoft.Office.Server.Search, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
) 
        
$Source = @"
using System;
using System.Collections.Generic;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint.Administration;

namespace SearchWorkaround
{
    public class Utility
    {
        public static void AddTitleCrawledPropertyMapping(string propertyName)
        {
            foreach (SPService service in SPFarm.Local.Services)
            {
                if (service is SearchService)
                {
                    SearchService searchService = (SearchService)service;

                    foreach (SearchServiceApplication ssa in searchService.SearchApplications)
                    {
                        Schema schema = new Schema(ssa);
                        
                        ManagedProperty property = schema.AllManagedProperties[propertyName];
                        property.RespectPriority = true;

			//Title
                        //64ae120f-487d-445a-8d5a-5258f99cb970
                        CrawledProperty crawledProperty = schema.GetCrawledProperty(new Guid("64ae120f-487d-445a-8d5a-5258f99cb970"), "Title");                        
                        
                        List crawledproperties = property.GetMappedCrawledProperties(0);
                        crawledproperties.Add(crawledProperty);

                        MappingCollection mappings = new MappingCollection();
                        foreach(CrawledProperty c in crawledproperties)
                        {
                                Mapping mapping = new Mapping(c.Propset, c.Name, c.VariantType, property.PID);
                                mappings.Add(mapping);
                        }
                        
                        property.SetMappings(mappings);
                        property.Update();
                    }
                }
            }            
        }
    }
}

"@

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp -IgnoreWarnings #-Debug

[SearchWorkaround.Utility]::AddTitleCrawledPropertyMapping("SPSiteUrl")
[SearchWorkaround.Utility]::AddTitleCrawledPropertyMapping("ListId")