Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Created March 9, 2017 03:59
Show Gist options
  • Save darrenjrobinson/6b5464493e4b75cd11d06c02419c8e13 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/6b5464493e4b75cd11d06c02419c8e13 to your computer and use it in GitHub Desktop.
Automated Simultaneous MIM Management Agent Imports then Async Synchronisation Run Profiles
# Get the Invoke-Parallel script from https://github.com/RamblingCookieMonster/Invoke-Parallel
# And replace Line 3 with it
+ function Invoke-Parallel {}
# Import the MIIS Automation PS Module
Import-Module LithnetMIISAutomation
# Get all my AD Management Agents
$ADmanagementAgents = Get-ManagementAgent | where-object {$_.category -eq "AD" } | select Name
# Create a collection of the MAs to track what we have to sync
$MAsToSync = New-Object System.Collections.ArrayList
write-host "The follow MAs were found"
foreach ($agent in $ADmanagementAgents){
$MAsToSync.Add($agent.Name)
write-host " " $agent.Name.ToString()
}
$completedimport = @()
# Run simultaneous Delta Imports then perform Delta Syncs
$ADmanagementAgents | Invoke-Parallel -ImportVariables -NoCloseOnTimeout -ScriptBlock {
Start-ManagementAgent -MA $_.Name -RunProfileName DI -Verbose
# Loop through each MA until we find one that has completed its Import
do {
foreach ($MA in $ADmanagementAgents){
$status = Get-ManagementAgent -Name $MA.name
if ($status.Status -eq "in-progress" -and $status.ExecutingRunProfileName -eq "DI"){
write-host "Import in progress on" $MA.name
}
if (!$status.Status){
# A candidate for a Sync run
$completedimport += $MA.Name
write-host $MA.name "is idle"
}
}
} until ($completedimport.count -gt 0)
# we now have at least one MA thats finished its import lets start it syncing
Start-ManagementAgent -MA $completedimport[0] -RunProfileName DS
# Wait for it to complete
Wait-ManagementAgent -MA $completedimport[0]
# Remove it from our list to process
$MAsToSync.Remove($completedimport[0].Name)
Write-host $completedimport[0].Name "has completed its sync cycle"
Write-host $MAsToSync.Count "remaining to peform their sync cycle"
# iterate through the rest until done
do {
foreach ($MAtoSync in $MAsToSync){
$MAstatus = Get-ManagementAgent -Name $MAtoSync
if (!$MAstatus.Status){
# Perform Delta Sync
Start-ManagementAgent -MA $MAtoSync -RunProfileName DS
# Wait for it to complete
Wait-ManagementAgent -MA $MAtoSync
# Remove it from our to be processed arraylist
$MAsToSync.Remove($MAtoSync)
Write-host $MAtoSync "has completed its sync cycle"
Write-host $MAsToSync.Count "remaining to peform their sync cycle"
}
}
} until ($MAsToSync.Count -eq 0)
# We're done
Write-host "FINISHED"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment