Sample C# code to invoke execution of a run profile on Forefront Identity Server Synchronization Service

 During integration testing of FIM Synchronization Service it is often useful to programmatically invoke run profiles of 
 FIM Sync Service, and later query the Metaverse and/or Connector spaces for the results of the run to validate proper 
 rule execution. The sample below shows how to programmatically invoke run profiles.
 Note that profiles run synchronously. 
  
 This posting is provided "AS IS" with no warranties, and confers no rights. 
 Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm
  
 namespace FIMTestCasesDriver.EnvironmentHelpers.FIM
{
    using System.Management;

    public static class RunProfileInvoker
    {
        private const string FimSyncServiceWMINameSpace = "root\\MicrosoftIdentityIntegrationServer";
        private const string FimSyncServiceMAObjectSpace = "MIIS_ManagementAgent.Name";

        private const string FullImportAndFullSyncProfileName = "full import and full sync";

        private const string ExportAndDeltaImportStageOnlyProfileName = "export and delta import stage only";

        private const string ActiveDirectoryMAName = "AD";
        private const string ADLDSMAName = "ADLDS";
        private const string HRMAName = "HR";
        private const string RegistrationMAName = "Registration";

        public static void RunFullCycle()
        {
            RunProfile(
                ActiveDirectoryMAName,
                FullImportAndFullSyncProfileName);

            RunProfile(
                RegistrationMAName,
                FullImportAndFullSyncProfileName);

            RunProfile(
                HRMAName,
                FullImportAndFullSyncProfileName);

            RunProfile(
                ActiveDirectoryMAName,
                ExportAndDeltaImportStageOnlyProfileName);

            RunProfile(
                ADLDSMAName,
                ExportAndDeltaImportStageOnlyProfileName);
        }

        private static void RunProfile(
            string maName,
            string runProfileName)
        {
            var managementObject = new ManagementObject(
                FimSyncServiceWMINameSpace,
                string.Format("{0}=" + "'{1}'", FimSyncServiceMAObjectSpace, maName),
                null);
            var inParameters = managementObject.GetMethodParameters("Execute");
            inParameters["RunProfileName"] = runProfileName;
            managementObject.InvokeMethod("Execute", inParameters, null);
        }
    }
}