コード例 #1
0
        public static bool JoinDomain(string OU)
        {
            var credentials = new ApiCall.APICall().PolicyApi.GetDomainJoinCredentials();

            if (credentials == null)
            {
                Logger.Debug("Could Not Obtain Credentials To Join The Domain.");
                return(false);
            }

            if (string.IsNullOrEmpty(OU))
            {
                OU = null; //set ou to null if it comes through as empty
            }
            Logger.Info("Joining Domain " + credentials.Domain);
            Logger.Debug("Username: "******"OU: " + OU);
            try
            {
                var resultValue = NetJoinDomain(null, credentials.Domain, OU, credentials.Username, credentials.Password, (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_ACCT_CREATE));
                if (resultValue == 0 || resultValue == 2691) //2691 = already joined, return success as to not hold up the policy
                {
                    Logger.Info("Successfully Joined Domain");
                    return(true);
                }
                else if (resultValue == 2224)
                {
                    Logger.Info("Computer Already Exists In A Different OU.  Cannot Join To Specified OU");
                    resultValue = NetJoinDomain(null, credentials.Domain, null, credentials.Username, credentials.Password, (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_ACCT_CREATE));
                    if (resultValue == 0)
                    {
                        Logger.Info("Successfully Joined Domain");
                        return(true);
                    }
                    else
                    {
                        Logger.Error("Domain Join Failed.");
                        Logger.Info("Domain Join Result: " + resultValue);
                        return(false);
                    }
                }
                else
                {
                    Logger.Error("Domain Join Failed.");
                    Logger.Info("Domain Join Result: " + resultValue);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Domain join failed.");
                Logger.Error(ex.Message);
                return(false);
            }
        }
コード例 #2
0
        public int RunTask()
        {
            Logger.Debug("Starting Impersonation Task");
            var credentials = new ApiCall.APICall().PolicyApi.GetImpersonationAccount(ImpersonationGuid);

            if (credentials == null)
            {
                Logger.Debug("Could Not Obtain Credentials For Impersonation Account " + ImpersonationGuid);
                return(-1);
            }
            TaskDefinition td = TaskService.Instance.NewTask();

            td.RegistrationInfo.Description = "Toec Impersonation Task";
            td.Principal.RunLevel           = TaskRunLevel.Highest;
            td.Actions.Add(Command, Arguments, WorkingDirectory);
            td.Settings.DisallowStartIfOnBatteries = false;
            td.Settings.StopIfGoingOnBatteries     = false;
            if (ExecutionTimeout != 0)
            {
                td.Settings.ExecutionTimeLimit = TimeSpan.FromMinutes(ExecutionTimeout);
            }

            try
            {
                var ts = TaskService.Instance.RootFolder.RegisterTaskDefinition(
                    "Toec Impersonation Task " + ModuleGuid, td, TaskCreation.CreateOrUpdate, credentials.Username, credentials.Password, TaskLogonType.Password);

                //Not sure if this actually helps in any way.
                //Anyone care to share a better way?
                for (int i = 0; i < 20; i++)
                {
                    credentials.Username = "******";
                    credentials.Password = "******";
                    credentials.Password = "******";
                    credentials.Username = "******";
                }

                ts.Run();

                //Give the task some time to start
                //If it hasn't started in 5 minutes skip for now

                /* int counter = 1;
                 * while (ts.State == TaskState.Queued || ts.State == TaskState.Ready)
                 * {
                 *   if (counter == 61)
                 *   {
                 *       ts.Stop();
                 *       ts.TaskService.RootFolder.DeleteTask("Toec Impersonation Task " + ModuleGuid );
                 *       return -1;
                 *   }
                 *
                 *   System.Threading.Tasks.Task.Delay(5 * 1000).Wait();
                 *   counter++;
                 * }
                 */
                //Wait for task to finish up to Execution Timeout - handled by windows task scheduler
                while (ts.State == TaskState.Running)
                {
                    System.Threading.Tasks.Task.Delay(5 * 1000).Wait();
                }

                ts.Stop();
                var exitCode = ts.LastTaskResult;
                ts.TaskService.RootFolder.DeleteTask("Toec Impersonation Task " + ModuleGuid);

                return(exitCode);
            }
            catch (Exception ex)
            {
                Logger.Error("Could Not Run Impersonation Task");
                Logger.Error(ex.Message);
                return(-1);
            }
        }