Appendix E - Automation Application

Applies To: Windows Server 2008, Windows Server 2008 R2

The following is code for the automation application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Host;
using System.Reflection;
using System.Collections.ObjectModel;



namespace Automator
{
    class Program
    {
        static void Main(string[] args)
        {

            Program myP = new Program();
            DataSet myda = myP.ADMTUsers();
            myP.AccountMAFullImport();
            myP.ResourceMAFullImport();
            myP.SQLFullImport();
            myP.AccountMAFullSynch();
            myP.ResourceMAFullSynch();
            myP.SQLFullSynch();
            myP.ResourceMAExport();
            myP.SQLExport();
            myP.ResourceMADeltaImport();
            myP.SQLFullImport();
            
            myP.MigrateSids(myda);
            myP.CreateLinkedMailboxes(myda);

            myP.ResourceMADeltaImport();
            myP.ResourceMADeltaSynch();
            myP.AccountMAExport();
            myP.AccountMADeltaImport();
            myP.SQLFullSynch();
            myP.SQLExport();


        }

        public void MigrateSids(DataSet da)
        {



            System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
            proc1.EnableRaisingEvents = false;
            string q = "\"";
            string strCmdLine;
            for (int i = 0; i <= da.Tables["admtusers"].Rows.Count - 1; i++)
            {

                string fname = da.Tables["admtusers"].Rows[i].ItemArray.GetValue(0).ToString();
                string lname = da.Tables["admtusers"].Rows[i].ItemArray.GetValue(1).ToString();
                fname = fname.Trim();
                lname = lname.Trim();

                string sourcename = "CN=" + fname + " " + lname;
                string targetname = fname + " " + lname;

               
              
                strCmdLine = (@"user /N " + q + sourcename + q + " " + q + targetname + q + " " + "/O:" + q + @"C:\Windows\ADMT\options.txt" + q);
                proc1.StartInfo.FileName = @"C:\Windows\ADMT\admt.exe";
                proc1.StartInfo.RedirectStandardOutput = true;
                proc1.StartInfo.UseShellExecute = false;
                proc1.StartInfo.Arguments = strCmdLine;
                Console.WriteLine("Migrating: " + targetname);
               proc1.Start();
               
                string result = proc1.StandardOutput.ReadToEnd();
                
            }
            if (proc1.HasExited)
                proc1.Dispose();
           

        }

        public DataSet ADMTUsers()
        {
            SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=UserSidTracking;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = ("select FirstName,LastName from Users where SidHistoryPresent = 'N'");
            cmd.Connection = conn;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet da = new DataSet();
            adapter.Fill(da, "admtusers");
            conn.Close();
            return da;
        }

        public void CreateLinkedMailboxes(DataSet da)
        {
            RunspaceConfiguration rc = RunspaceConfiguration.Create();

            PSSnapInException warning;
            PSSnapInInfo info = rc.AddPSSnapIn("Microsoft.Exchange.Management.Powershell.Admin", out warning);

            if (warning != null)
            {
                Console.WriteLine(warning.Message);
            }

            Runspace runspace = RunspaceFactory.CreateRunspace(rc);
            runspace.Open();

            for (int i = 0; i <= da.Tables["admtusers"].Rows.Count - 1; i++)
            {
                string fname = da.Tables["admtusers"].Rows[i].ItemArray.GetValue(0).ToString();
                string lname = da.Tables["admtusers"].Rows[i].ItemArray.GetValue(1).ToString();

                fname = fname.Trim();
                lname = lname.Trim();
                string mailnick = fname.Substring(0, 1).ToLower() + lname.ToLower();

                string fullname = fname + " " + lname;

                Pipeline pipeline = runspace.CreatePipeline();
                string script = (@"Enable-Mailbox -Identity 'resource.fabrikam.net/ResourceForestUsers/" + fullname + "' -Alias '" + mailnick + @"' -Database 'RES-DC\First Storage Group\Mailbox Database' -LinkedMasterAccount 'corp\" + mailnick + @"' -LinkedDomainController 'ACC-DC.corp.fabrikam.com' -LinkedCredential $null");
                Console.WriteLine();
                Console.Write("Creating Linked Mailbox for " + fullname + "... ");
                pipeline.Commands.AddScript(script);
                Collection<PSObject> results = pipeline.Invoke();
                Console.Write("Done!");
                pipeline.Dispose();


            }

            runspace.Close();
        }

        public void AccountMAFullImport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{CFF78843-4450-4902-B18A-C10C24A67513}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("ACCOUNT.Execute( \"FI\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "FI" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }


        }

        public void AccountMADeltaImport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{CFF78843-4450-4902-B18A-C10C24A67513}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("ACCOUNT.Execute( \"DI\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "DI" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }


        }

        public void AccountMAFullSynch()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{CFF78843-4450-4902-B18A-C10C24A67513}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("ACCOUNT.Execute( \"FS\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "FS" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }


        }

        public void AccountMADeltaSynch()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{CFF78843-4450-4902-B18A-C10C24A67513}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("ACCOUNT.Execute( \"DS\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "DS" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }


        }

        public void AccountMAExport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{CFF78843-4450-4902-B18A-C10C24A67513}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("ACCOUNT.Execute( \"E\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "E" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }


        }

        public void SQLFullImport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{4089d66f-39b2-449e-8f7e-a8630e931fcf}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("SQL.Execute( \"FI\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "FI" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void SQLFullSynch()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{4089d66f-39b2-449e-8f7e-a8630e931fcf}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("SQL.Execute( \"FS\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "FS" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }

        }

        public void SQLDeltaSynch()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{4089d66f-39b2-449e-8f7e-a8630e931fcf}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("SQL.Execute( \"DS\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "DS" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void SQLExport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{4089d66f-39b2-449e-8f7e-a8630e931fcf}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("SQL.Execute( \"E\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "E" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void ResourceMAFullImport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{AF8EA810-b232-4850-bf8b-6c2e2b32578c}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("RESOURCE.Execute( \"FI\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "FI" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void ResourceMAFullSynch()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{AF8EA810-b232-4850-bf8b-6c2e2b32578c}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("RESOURCE.Execute( \"FS\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "FS" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void ResourceMADeltaImport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{AF8EA810-b232-4850-bf8b-6c2e2b32578c}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("RESOURCE.Execute( \"DI\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "DI" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void ResourceMADeltaSynch()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{AF8EA810-b232-4850-bf8b-6c2e2b32578c}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("RESOURCE.Execute( \"DS\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "DS" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        public void ResourceMAExport()
        {
            try
            {

                ConnectionOptions opt = new ConnectionOptions();
                opt.Authentication = AuthenticationLevel.PacketPrivacy;

                ManagementScope myScope = new ManagementScope("root\\MicrosoftIdentityIntegrationServer", opt);
                SelectQuery myQuery = new SelectQuery("MIIS_ManagementAgent", "GUID='{AF8EA810-b232-4850-bf8b-6c2e2b32578c}'");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(myScope, myQuery);
                foreach (ManagementObject ma in searcher.Get())
                {
                    Console.WriteLine("RESOURCE.Execute( \"E\" )...");
                    ma.InvokeMethod("Execute", new object[1] { "E" });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }

        }

    }
}