Monday, September 20, 2010

Using PowerShell Jobs to Run Management Agents Concurrently

There are three basic operations performed by Management Agents in the synchronization engine:

  1. Import (get stuff from the connected system)
  2. Sync (run the Sync rules)
  3. Export (put stuff into the connected system)

The Sync operation should only ever be run by one Management Agent at a time, but the Import and Export operations can (and usually should) be run concurrently.

PowerShell makes easy work of this thanks to the Start-Job cmdlet.  The Start-Job cmdlet starts another PowerShell session and executes a script block in that session.  If you watch Task Manager you will see ‘PowerShell.exe’ processes start up every time you run Start-Job.

The script sample below uses Start-Job to run each Management Agent in its own job.  Here are the highlights:

Get the list of management agents using WMI. 

In my script I want to run all of the AD Management Agents, so I filter the list of Management Agents based on the ‘Type’ property.  This is done using the Where cmdlet, but I could have also done it with a WMI filter.

Loop through the Management Agents

Loop through the Management Agents to execute a Run Profile.  In my example I want to run a Run Profile named ‘Full Import (Stage Only).  That Run Profile has to exist on the Management Agent or the call to the ‘Execute’ method will fail.

Here is the script:

$admas = Get-WmiObject -Class MIIS_ManagementAgent -Namespace root/MicrosoftIdentityIntegrationServer | where {$_.Type -eq 'Active Directory'}

foreach ($ma in $admas)
{
    Write-host "Starting Full Import on" $ma.Name"..." 


    Start-Job -ArgumentList $ma.Name -ScriptBlock {param($maName)$ma = Get-WmiObject -Class MIIS_ManagementAgent -Namespace root/MicrosoftIdentityIntegrationServer -Filter "name='$maName'";$ma.Execute('Full Import (Stage Only)');}    
}

No comments: