Saturday 29 November 2014

Exporting UPA profile connection mappings from SharePoint

The below script allows you to export all the profile properties used in the User Profile Service along with any mapping used for a particular Profile Sync connection.

Replace the bits in yellow with your own variables:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Get the site context
$siteUrl = "<<Site Url>>"
$site = Get-SPSite $siteUrl
$context = Get-SPServiceContext $site


#Retrieve a list of all property mappings
function Get-Mappings ($context)
{
    $upam = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context)
    $cm = $upam.ConnectionManager["<<Name of your UPA sync connection>>"]
    $maps = $cm.PropertyMapping.GetEnumerator()
   
    $maps | ForEach-Object{
      $returnValue = @{
        "Column" = $_.ProfileProperty.Name
        "Mapping" = $_.DataSourcePropertyName
      }
     
      New-Object PSObject -Property $returnValue | Select ("Column", "Mapping")
    }
}

#Create a new UserProfileConfigManager object
$upam = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context)

#Get the core properties
$coreProps = $upam.ProfilePropertyManager.GetCoreProperties()


#Match the core properties to their coresponding mappings
function Get-CorePropsMapping ($coreProps, $maps)
{
    foreach ($coreProp in $coreProps)
    {
        $map = $maps | Where-Object {$_.Column -eq $coreProp.Name}
     
        $value = @{
            "Column" = $coreProp.DisplayName
            "Mapping" = $map.Mapping
        }
     
       New-Object PSObject -Property  $value | Select ("Column", "Mapping")
    }
}

#Call the get mappings function
$maps = Get-Mappings -context $context

#Call the CorePropsMapping function and output to a gridview
Get-CorePropsMapping $coreProps $maps | Out-GridView

This can be used if you should need to recreate the mapping or UPA service (...say if the User Profile Sync service is stuck starting.. for instance)