Schedule a Full User Profile Import
It may be desirable to perform full profile imports on a regular schedule, possibly once a month or once a quarter. This would ensure picking up changes in the import connection rules (included containers and filters for example), and changes to the profile property schema (new properties and changes in mapping to existing properties).
The Central Administration interface only provides a means to schedule incremental imports.
A full import can be started through Central Administration on-demand by using the Start Profile Synchronization link, but this link does not provide for scheduling.
It is possible to schedule full imports using the Windows Server Task Scheduler and a Windows PowerShell script.
- Create the Windows PowerShell script ExecuteFullImport.ps1 and associated configuration file ExecuteFullImport.xml. Place in the standard scripts folder of the server. (Source code below. Special credit goes to Brian Lalancette and the AutoSPInstaller project for foundational code included in this script, reference https://autospinstaller.codeplex.com. AutoSPInstaller is a fantastic script to build multi-server SharePoint 2010 farms.)
- Create a schedule task. Start Menu > Administrative Tools > Task Scheduler;
or Run > taskschd.msc. - Right click the Task Scheduler Library node in the left pane, and select Create Task.
- On the General Tab, enter the Name and Description.
- On the Triggers Tab, click the New button, and then enter the desired schedule.
- On the Actions Tab, select Start a program, enter PowerShell as the program/script, enter the script name ExecuteFullImport.ps1, and Start in put the directory containing the script and associated xml configuration file.
- Optionally, modify the Conditions and Settings tabs’ configuration.
- The script will run at the scheduled time. The PowerShell window remains open by default. Press any key to close the window.
ExecuteFullImport.ps1
############################################################
### Powershell commands to execute a User Profile full import
############################################################
param
(
[string]$InputFile = "ExecuteFullImport.xml"
)
###############################################################
### ExecutionPloicy Note: errors/warnings expected
###############################################################
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -confirm:$false
write-host "Displaying execution policy" -foregroundcolor yellow
Get-ExecutionPolicy -List
$0 = $myInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
# Get the configuration file contents
$configPath = Join-Path -Path $dp0 -ChildPath $InputFile
[xml]$xmlinput = (Get-Content $configPath)
#region functions
###############################################################
# Note: Following functions borrowed from AutoSPInstallerFunctions.ps1
Function StartTracing
{
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$script:LogFile = "$env:USERPROFILE\Desktop\ExecuteFullImport-$LogTime.rtf"
Start-Transcript -Path $LogFile -Force
}
Function WriteLine
{
Write-Host -ForegroundColor White "--------------------------------------------------------------"
}
Function Pause
{
#From https://www.microsoft.com/technet/scriptcenter/resources/pstips/jan08/pstip0118.mspx
Write-Host "Press any key to exit..."
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
###############################################################
#endregion functions
StartTracing
If (!$env:StartDate) {$env:StartDate = Get-Date}
Write-Host -ForegroundColor White "-----------------------------------"
Write-Host -ForegroundColor White "| ExecuteFullImport script |"
Write-Host -ForegroundColor White "| Started on: $env:StartDate |"
Write-Host -ForegroundColor White "-----------------------------------"
Try
{
# The SharePoint PowerShell snap-in is required
If ((Get-PSSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
{
WriteLine
Write-Host -ForegroundColor White " - Loading SharePoint Powershell Snapin"
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
WriteLine
}
$stopTimerJob = $xmlinput.Configuration.Farm.FullImport.StopCleanUpTimerJob
if ($stopTimerJob)
{
$timerJob = Get-SPTimerJob "mysitecleanup"
Disable-SPTimerJob $timerJob
WriteLine
Write-Host -ForegroundColor White " - IMPORTANT: My Site Clean Up time job has been disabled."
Write-Host -ForegroundColor White " - Remember to enable the timer job after the import completes."
WriteLine
}
# Get the Profile Service Application
$upa = (Get-SPServiceApplication | ? {$_.GetType().ToString() -eq "Microsoft.Office.Server.Administration.UserProfileApplication"})
# Start the full profile import job
# NOTE: This method call appears to "eat" exceptions, so it "fails silently". Check the application event log for error details.
$upa.StartImport($true)
Write-Host -ForegroundColor White " - Full Import request submitted, monitor progress with the Central Administration User Profile Service Application page"
}
Catch
{
WriteLine
Write-Host -ForegroundColor Yellow " - Script aborted!"
If ($_.FullyQualifiedErrorId -ne $null -and $_.FullyQualifiedErrorId.StartsWith(" - "))
{
# Error messages starting with " - " are thrown directly from this script
Write-Host -ForegroundColor Red $_.FullyQualifiedErrorId
}
# Lately, loading the snapin throws an error: "System.TypeInitializationException: The type initializer for 'Microsoft.SharePoint.Utilities.SPUtility' threw an exception. ---> System.IO.FileNotFoundException:"...
ElseIf ($_.Exception.Message -like "*Microsoft.SharePoint.Utilities.SPUtility*")
{
Write-Host -ForegroundColor Yellow " - A known (annoying) issue occurred loading the SharePoint Powershell snapin."
Write-Host -ForegroundColor Yellow " - We need to re-launch the script to clear this condition."
$ScriptCommandLine = $($MyInvocation.Line)
Write-Host -ForegroundColor White " - Re-Launching:"
Write-Host -ForegroundColor White " - $ScriptCommandLine"
Start-Process -WorkingDirectory $PSHOME -FilePath "powershell.exe" -ArgumentList "$ScriptCommandLine" -Verb RunAs
Start-Sleep 10
Exit
}
Else
{
#Other error messages are exceptions. Can't find a way to make this Red
$_ | Format-List -Force
}
$env:EndDate = Get-Date
Write-Host -ForegroundColor White "-----------------------------------"
Write-Host -ForegroundColor White "| ExecuteFullImport script |"
Write-Host -ForegroundColor White "| Started on: $env:StartDate |"
Write-Host -ForegroundColor White "| Aborted: $env:EndDate |"
Write-Host -ForegroundColor White "-----------------------------------"
}
Finally
{
Stop-Transcript
If ($ScriptCommandLine) {Exit}
Else {Pause}
Invoke-Item $LogFile
}
ExecuteFullImport.xml
<?xml version="1.0" ?>
<Configuration>
<Farm>
<FullImport
StopCleanUpTimerJob="true"
/>
</Farm>
</Configuration>