How to Use PowerShell to Start FIM Run Profiles
Summary
The FIM run profile script is a powershell script permitting you to automatically start run profiles in the specific order you want them to run and for a specific (or unlimited) number of times.
Introduction
Thanks to this powershell script, you can easily automatize the execution of run profiles for FIM management agents.
You just have to adapt the PARAMETERS section to your needs.
Execution screenshot
Source Code
01.# @author: Fabien Duchene
02.# @mail: f.duchene **at** car-online.fr
03.
04.############
05.# PARAMETERS
06.############
07.
08.$params_ComputerName = "." # "." is the current computer
09.$params_delayBetweenExecs = 30 #delay between each execution, in seconds
10.$params_numOfExecs = 0 #Number of executions 0 for infinite
11.$params_runProfilesOrder =
12.
13.@(
14. @{
15. type="Forefront Identity Management (FIM)";
16. profilesToRun=@("Full Import";"Full Synchronization");
17. };
18.
19. @{
20. type="Active Directory";
21. profilesToRun=@("Full Import";"Full Synchronization";"Export");
22. };
23.);
24.
25.
26.
27.############
28.# FUNCTIONS
29.############
30.
31.$line = "-----------------------------"
32.function Write-Output-Banner([string]$msg) {
33. Write-Output $line,("- "+$msg),$line
34.}
35.
36.
37.############
38.# DATAS
39.############
40.
41.$MAs = @(get-wmiobject -class "MIIS_ManagementAgent" -namespace "root\MicrosoftIdentityIntegrationServer" -computername $params_ComputerName)
42.$numOfExecDone = 0
43.
44.############
45.# PROGRAM
46.############
47.
48.do {
49. Write-Output-Banner("Execution #:"+(++$numOfExecDone))
50. foreach($MATypeNRun in $params_runProfilesOrder) {
51.
52. $found = $false;
53. foreach($MA in $MAS) {
54. if(!$found) {
55. if($MA.Type.Equals($MATypeNRun.type)) {
56. $found=$true;
57. Write-Output-Banner("MA: "+$MA.Type)
58. foreach($profileName in $MATypeNRun.profilesToRun) {
59. Write-Output (" "+$profileName)," -> starting"
60. $datetimeBefore = Get-Date;
61. $result = $MA.Execute($profileName);
62. $datetimeAfter = Get-Date;
63. $duration = $datetimeAfter - $datetimeBefore;
64.
65. if("success".Equals($result.ReturnValue)){
66. $msg = "done. Duration: "+$duration.Hours+":"+$duration.Minutes+":"+$duration.Seconds
67. } else { $msg = "Error: "+$result }
68. Write-Output (" -> "+$msg)
69. }
70. }
71. }
72. }
73. if(!$found) { Write-Output ("Not found MA type :"+$MATypeNRun.type); }
74. }
75.
76. $continue = ($params_numOfExecs -EQ 0) -OR ($numOfExecDone -lt $params_numOfExecs)
77. if($continue) {
78. Write-Output-Banner("Sleeping "+$params_delayBetweenExecs+" seconds")
79. Start-Sleep -s $params_delayBetweenExecs
80. }
81.
82.} while($continue)
Improvements
Many people proposed some improvements.
At the time I am writing these lines, you can find them on the following thread:
Using powershell to run FIM MA synchronisation profiles
References
You can also find more informations on my blog: http://fabienduchene.blogspot.com/2009/11/fim-execute-management-agents-run.html