/// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            try
            {
                // Ensure that the current user has admin access to the current Workflow Manager DB
                if (!CurrentUserIsWmxAdministrator())
                {
                    throw new WmauException(WmauErrorCodes.C_USER_NOT_ADMIN_ERROR);
                }

                // Stash away the executing user's information, if appropriate
                string             username      = ESRI.ArcGIS.JTXUI.ConfigurationCache.GetCurrentSystemUser(ESRI.ArcGIS.JTXUI.ConfigurationCache.UseUserDomain);
                IJTXConfiguration3 configMgr     = this.WmxDatabase.ConfigurationManager as IJTXConfiguration3;
                IJTXUser3          executingUser = configMgr.GetUser(username) as IJTXUser3;

                // Import the AD information
                string domain         = System.Environment.UserDomainName;
                string domainUsername = string.Empty;
                string domainPassword = string.Empty;
                int    numUsers       = 0;
                int    numGroups      = 0;
                ActiveDirectoryHelper.SyncronizeJTXDatabaseWithActiveDirectory(this.WmxDatabase, domain, domainUsername, domainPassword, m_userGroup, m_groupGroup, out numGroups, out numUsers);

                // If the tool was set to preserve the current user's account and the user
                // was removed from the DB, then re-add their account
                if (configMgr.GetUser(username) == null)
                {
                    if (m_preserveCurrentUser)
                    {
                        IJTXConfigurationEdit2 configEdit = this.WmxDatabase.ConfigurationManager as IJTXConfigurationEdit2;
                        IJTXUserConfig         newUser    = configEdit.CreateUser() as IJTXUserConfig;
                        newUser.FirstName_2 = executingUser.FirstName;
                        newUser.FullName_2  = executingUser.FullName;
                        newUser.LastName_2  = executingUser.LastName;
                        newUser.UserName_2  = executingUser.UserName;
                        (newUser as IJTXUser3).IsAdministrator = executingUser.IsAdministrator;
                        newUser.Store();

                        msgs.AddMessage("User '" + username + "' not found in Active Directory group '" + m_userGroup + "'; re-added placeholder to Workflow Manager database");
                    }
                    else
                    {
                        msgs.AddWarning("User '" + username + "' removed from Workflow Manager database");
                    }
                }

                // Update the output parameters
                WmauParameterMap  paramMap = new WmauParameterMap(paramValues);
                IGPParameterEdit3 outParam = paramMap.GetParamEdit(C_PARAM_OUT_NUM_USERS);
                IGPLong           value    = new GPLongClass();
                value.Value    = numUsers;
                outParam.Value = value as IGPValue;

                outParam       = paramMap.GetParamEdit(C_PARAM_OUT_NUM_GROUPS);
                value          = new GPLongClass();
                value.Value    = numGroups;
                outParam.Value = value as IGPValue;

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_UNSPECIFIED_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            finally
            {
                // Release any COM objects here!
            }
        }
        static void Main(string[] args)
        {
            // Convert the arguments to objects
            object[] argv = new object[args.Length];
            for (int i = 0; i < args.Length; i++)
            {
                argv[i] = args[i];
            }

            // Check if they want to display the usage message
            string tmpOut;

            if (StepUtilities.GetArgument(ref argv, "h", true, out tmpOut) || StepUtilities.GetArgument(ref argv, "?", true, out tmpOut))
            {
                PrintUsageMessage();
                return;
            }

            if (CheckoutLicense())
            {
                try
                {
                    IJTXDatabaseManager2 dbMgr = new JTXDatabaseManagerClass() as IJTXDatabaseManager2;
                    IJTXDatabase         ipDB  = null;
                    if (StepUtilities.GetArgument(ref argv, argNames[0], true, out tmpOut))
                    {
                        // Database was specified
                        try
                        {
                            ipDB = dbMgr.GetDatabase(tmpOut);
                        }
                        catch (COMException)
                        {
                            Console.WriteLine("{0} is not a valid Workflow Manager database alias", tmpOut);
                        }
                        Console.WriteLine("Using database {0}", tmpOut);
                    }
                    else
                    {
                        // Use the default database
                        Console.WriteLine("Using default database");
                        ipDB = dbMgr.GetActiveDatabase(false);
                    }

                    // Get the domain. If one is not specified, use the current domain
                    string strDomain;
                    if (StepUtilities.GetArgument(ref argv, argNames[1], true, out tmpOut))
                    {
                        strDomain = tmpOut;
                    }
                    else
                    {
                        strDomain = Environment.UserDomainName;
                    }

                    // Get the username
                    string strUsername = "";
                    if (StepUtilities.GetArgument(ref argv, argNames[2], true, out tmpOut))
                    {
                        strUsername = tmpOut;
                    }

                    // Get the password
                    string strPassword = "";
                    if (strUsername != "" && StepUtilities.GetArgument(ref argv, argNames[3], true, out tmpOut))
                    {
                        strPassword = tmpOut;
                    }

                    // Get the userGroup.  If one is not specified, check the registry for the value the UI stored
                    string strUserGroup;
                    if (StepUtilities.GetArgument(ref argv, argNames[4], true, out tmpOut))
                    {
                        strUserGroup = tmpOut;
                    }
                    else
                    {
                        strUserGroup = GetGroupFromReg("UserADGroup");
                    }

                    // Get the groupGroup.  If one is not specified, check the registry for the value the UI stored
                    string strGroupGroup;
                    if (StepUtilities.GetArgument(ref argv, argNames[5], true, out tmpOut))
                    {
                        strGroupGroup = tmpOut;
                    }
                    else
                    {
                        strGroupGroup = GetGroupFromReg("GroupADGroup");
                    }

                    if (String.IsNullOrEmpty(strUserGroup) || String.IsNullOrEmpty(strGroupGroup))
                    {
                        Console.WriteLine("Error: Empty userGroup or groupGroup");
                        return;
                    }

                    ConfigurationCache.InitializeCache(ipDB);

                    // Synchronize
                    int groupCount, userCount;
                    ActiveDirectoryHelper.SyncronizeJTXDatabaseWithActiveDirectory(ipDB, strDomain, strUsername, strPassword,
                                                                                   strUserGroup, strGroupGroup, out groupCount, out userCount);

                    Console.WriteLine("Successfully imported {0} users in {1} groups", userCount, groupCount);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed with error: " + e.Message + e.StackTrace);
                }
                finally
                {
                    CheckinLicense();
                }
            }
            else
            {
                Console.WriteLine("Could not checkout license... exiting");
            }
        }