Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for running MIM Synchronization Service in a controlled way.
<#
.SYNOPSIS
Script to run Synchronization of MIM.
Requires the LithnetMiisAutomation PowerShell module from https://github.com/lithnet/miis-powershell
.NoExport
Parameter that will disable Exports to External Systems. Only FIMService will receive Exported data.
#>
PARAM([bool]$NoExport=$false)
Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message ("Starting Delta Synchronization.") -EntryType Information
Import-Module LithnetMiisAutomation
#region Functions
function RunAsJob
{
param([string]$MAName, [string[]]$Profiles)
Start-Job -Name $MAName -ArgumentList $MAName,$Profiles -ScriptBlock {
param($MAName,$Profiles)
foreach($Profile in $Profiles)
{
if($Profile.StartsWith("Script")){
Invoke-Expression ($Profile.Split(":")[1])
}
elseif($Profile.StartsWith("Sleep")){
Invoke-Expression (Start-Sleep -Seconds $Profile.Split(":")[1])
}
else{
try{Start-ManagementAgent -RunProfileName $Profile -MA $MAName -NoProgress}
catch
{$Message = ("MA: "+$MAName + " LastStepStatus: "+(Get-LastRunDetails -MA $MAName).LastStepStatus)
Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message $Message -EntryType Error}
}
}
}
}
function RunInSequence
{
param([string]$MAName, [string[]]$Profiles)
foreach($Profile in $Profiles)
{
if($Profile.StartsWith("Script")){
Invoke-Expression ($Profile.Split(":")[1])
}
elseif($Profile.StartsWith("Sleep")){
Invoke-Expression (Start-Sleep -Seconds $Profile.Split(":")[1])
}
else{
try{Start-ManagementAgent -RunProfileName $Profile -MA $MAName -NoProgress}
catch
{$Message = ("MA: "+$MAName + " LastStepStatus: "+(Get-LastRunDetails -MA $MAName).LastStepStatus)
Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message $Message -EntryType Error}
}
}
}
#endregion Functions
#region Import (Runs in Parallel per MA)
RunAsJob -MAName HR -Profiles @("DI Person","DI Org")
RunAsJob -MAName AD -Profiles @("DI")
RunAsJob -MAName FIMService -Profiles @("DI")
RunAsJob -MAName ADviaPS -Profiles @("DI")
#Wait for Jobs to finish before we move on
Get-Job | Wait-Job | Receive-Job -Keep
Get-Job | Remove-Job
#endregion Import
#region Sync (Runs in Sequence)
RunInSequence -MAName HR -Profiles @("DS Person","DS Org")
RunInSequence -MAName AD -Profiles @("DS")
RunInSequence -MAName ADviaPS -Profiles @("DS")
RunInSequence -MAName FIMService -Profiles @("E","Script:WaitForWF.ps1","DI","DS")
#endregion Sync
#region Verify before Export
#This section can be used to run checks on Pending Exports and if something looks wrong we can set $NoExport = $true to prevent errors from spreading.
if((Get-PendingExportDeletes -MA AD).Count -gt 1){$NoExport=$true}
#endregion Verify befor Export
#region Export (Runs in Parallel per MA)
if($NoExport)
{Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message ("No Exports Running!") -EntryType Information}
else
{#Exports should be running
RunAsJob -MAName AD -Profiles @("E","Script:SaveExportStatistics.ps1")
}
#Wait for Jobs to finish before we move on
Get-Job | Wait-Job | Receive-Job -Keep
Get-Job | Remove-Job
#endregion Export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment