Esempio n. 1
0
        protected override void ExecuteMain()
        {
            try
            {
                this.Status = StepStatusEnum.Executing;
                WMICmdHelper wmi = new WMICmdHelper(
                    Properties.Settings.Default.RestartServicesTargetMachine,
                    "",
                    Properties.Settings.Default.RestartServicesUserName,
                    Properties.Settings.Default.RestartServicesPassword,
                    Properties.Settings.Default.RestartServicesDomain);

                wmi.RestartServices(
                    Properties.Settings.Default.ServicesToRestart.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)/*,
                                                                                                                                        * Properties.Settings.Default.RestartServiceTimeout*/
                    );

                this.Status       = StepStatusEnum.Pass;
                this.ResultDetail = new StepResultDetail("Services {0} restarted on machine {1}.".FormatWith(
                                                             Properties.Settings.Default.ServicesToRestart,
                                                             Properties.Settings.Default.RestartServicesTargetMachine));
            }
            catch (Exception ex)
            {
                this.Status       = StepStatusEnum.Failed;
                this.ResultDetail = new StepResultDetail("Error has occurred, please check log.", ExceptionHelper.CentralProcessSingle2(ex));
            }
            finally
            {
                if (this.ResultDetail != null)
                {
                    Log.Info(this.ResultDetail.Message);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Execute this step.
        /// </summary>
        protected override void ExecuteMain()
        {
            this.Status = StepStatusEnum.Executing;
            string logPath = Properties.Settings.Default.ResultLogPath.Trim();
            string cmd     = UpsertArgument(Properties.Settings.Default.RemoteCommand, "ResultLogPath", logPath);

            Impersonator impersonator = new Impersonator(Properties.Settings.Default.RemoteUserName, Properties.Settings.Default.RemoteDomain, Properties.Settings.Default.RemotePassword);
            WMICmdHelper wmiHelper    = new WMICmdHelper(
                Properties.Settings.Default.RemoteMachine,
                cmd,
                Properties.Settings.Default.RemoteUserName,
                Properties.Settings.Default.RemotePassword,
                Properties.Settings.Default.RemoteDomain
                );

            wmiHelper.RunCommandReturnOutput();

            if (wmiHelper.ExitCodeCaptured)
            {
                if (wmiHelper.ExitCode == 0)
                {
                    this.Status       = StepStatusEnum.Pass;
                    this.ResultDetail = new StepResultDetail("Successfully executed the following command on {0} as user {2}.\r\n{1}".FormatWith(
                                                                 Properties.Settings.Default.RemoteMachine,
                                                                 Properties.Settings.Default.RemoteCommand,
                                                                 Properties.Settings.Default.RemoteUserName
                                                                 ));
                }
                else
                {
                    this.Status       = StepStatusEnum.Failed;
                    this.ResultDetail = new StepResultDetail("Failed to execute the following command on {0} as user {2}. The exit code is {3}. Please check log for more details.\r\n{1}".FormatWith(
                                                                 Properties.Settings.Default.RemoteMachine,
                                                                 Properties.Settings.Default.RemoteCommand,
                                                                 Properties.Settings.Default.RemoteUserName,
                                                                 wmiHelper.ExitCode
                                                                 ));
                }
            }
            else
            {
                // Check the log path
                Log.Info("Failed to get the exit code of the following command on {0}.\r\n{1}\r\nChecking log from network path...".FormatWith(
                             Properties.Settings.Default.RemoteMachine,
                             Properties.Settings.Default.RemoteCommand
                             ));
                try
                {
                    DateTime start   = DateTime.Now;
                    DateTime end     = DateTime.Now;
                    TimeSpan timeout = TimeSpan.MaxValue;
                    TimeSpan.TryParse(Properties.Settings.Default.RemoteTimeout.ToString(), out timeout);

                    Step step = null;

                    while (!File.Exists(logPath) || FileHelper.IsFileLocked(logPath) || (step = Step.GetFromFile(logPath)) == null)
                    {
                        if (end.Subtract(start).CompareTo(timeout) < 0)
                        {
                            System.Threading.Thread.Sleep(TimeSpan.FromMinutes(1));
                            end = DateTime.Now;
                        }
                        else
                        {
                            throw new TimeoutException("Timed out after {0} waiting for the result file {1}.".FormatWith(
                                                           timeout.ToString(), logPath
                                                           ));
                        }
                    }

                    if (step != null)
                    {
                        this.Status       = step.Status;
                        this.ResultDetail = new StepResultDetail("", new List <Exception>());
                        if (step.ResultDetail != null)
                        {
                            this.ResultDetail.Message += step.ResultDetail.Message;
                            if (step.ResultDetail.Exceptions != null && step.ResultDetail.Exceptions.Count > 0)
                            {
                                this.ResultDetail.Exceptions.AddRange(step.ResultDetail.Exceptions);
                            }
                        }
                    }
                }
                catch (TimeoutException tex)
                {
                    this.Status       = StepStatusEnum.Timeout;
                    this.ResultDetail = new StepResultDetail("Timed out when waiting for the result of '{0}' executed on {1} from network path.".FormatWith(
                                                                 Properties.Settings.Default.RemoteCommand,
                                                                 Properties.Settings.Default.RemoteMachine), tex);
                }
                catch (Exception ex)
                {
                    this.Status       = StepStatusEnum.Warning;
                    this.ResultDetail = new StepResultDetail("Failed to get the exit code of the following command on {0}.\r\n{1}".FormatWith(
                                                                 Properties.Settings.Default.RemoteMachine,
                                                                 Properties.Settings.Default.RemoteCommand
                                                                 ), ex);
                }
            }

            impersonator.Undo();
        }
Esempio n. 3
0
        protected override void ExecuteMain()
        {
            try
            {
                this.Status = StepStatusEnum.Executing;
                List <string> failedMachineList = new List <string>();

                foreach (string machine in Properties.Settings.Default.RestartMachineList)
                {
                    #region Access is denied (5) ?
                    ////System.Diagnostics.Process process = System.Diagnostics.Process.Start("shutdown", "-r -f -m \\\\{0} -t 0".Format2(machine));
                    //Impersonator impersonator = new Impersonator(
                    //    Properties.Settings.Default.RemoteUserName,
                    //    Properties.Settings.Default.RemoteDomain,
                    //    Properties.Settings.Default.RemotePassword
                    //);
                    //CmdHelper cmd = new CmdHelper();
                    ////cmd.StartCmd("cmd /C echo {2} | runas /noprofile /user:bedrd\\{1} \"shutdown /r /f /m \\\\{0} /t 0\"".Format2(machine, Properties.Settings.Default.RemoteUserName, Properties.Settings.Default.RemotePassword));
                    ////cmd.StartCmd("shutdown /r /f /m \\\\{0} /t 0".Format2(machine));
                    ////cmd.StartCmd(@"whoami > C:\RulePerf\whoami.txt");
                    //if (cmd.LastExitCode != 0)
                    //{
                    //    failedMachineList.Add(machine);
                    //}
                    #endregion Access is denied (5) ?

                    #region Error 21 ?
                    //WMICmdHelper wmi = new WMICmdHelper(machine, "shutdown /r /f /t 0", Properties.Settings.Default.RemoteUserName, Properties.Settings.Default.RemotePassword, Properties.Settings.Default.RemoteDomain);
                    //wmi.RunCommandReturnOutput();
                    //if (wmi.ExitCodeCaptured && wmi.ExitCode != 0)
                    //{
                    //    failedMachineList.Add(machine);
                    //}
                    #endregion Error 21?

                    try
                    {
                        WMICmdHelper wmi = new WMICmdHelper(machine, "", Properties.Settings.Default.RemoteUserName, Properties.Settings.Default.RemotePassword, Properties.Settings.Default.RemoteDomain);
                        wmi.Reboot();
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.CentralProcess(ex);
                        failedMachineList.Add(machine);
                    }
                }

                // Wait for them shuting down
                System.Threading.Thread.Sleep(new TimeSpan(0, 2, 0));

                DateTime startTime = DateTime.Now;
                TimeSpan timeout   = TimeSpan.MaxValue;
                TimeSpan.TryParse(Properties.Settings.Default.RestartMachineTimeoutTimeSpan, out timeout);
                TimeSpan interval = TimeSpan.FromSeconds(30);
                TimeSpan.TryParse(Properties.Settings.Default.RestartMachinePollInterval, out interval);

                while (true)
                {
                    foreach (string machine in Properties.Settings.Default.RestartMachineList)
                    {
                        // rdp.exe listens on port 3389. If testing 3389 is connectable, then the machine should be ready for being remote logged on to
                        bool success = RemoteHelper.TestPort(System.Net.Dns.GetHostAddresses(machine)[0].ToString(), 3389);

                        if (!success)
                        {
                            goto PollWait;
                        }
                    }

                    System.Threading.Thread.Sleep(interval);
                    break;

PollWait:
                    DateTime now = DateTime.Now;
                    TimeSpan diff = now.Subtract(startTime);
                    if (diff.CompareTo(timeout) >= 0)
                    {
                        throw new TimeoutException("Not all machines are started after '{0}' seconds".FormatWith(diff.TotalSeconds));
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(interval);
                    }
                }

                if (failedMachineList.Count >= Properties.Settings.Default.RestartMachineList.Count)
                {
                    this.Status       = StepStatusEnum.Failed;
                    this.ResultDetail = new StepResultDetail("All the shuting down commands are failed, please check log for detailed information.");
                }
                else if (failedMachineList.Count > 0)
                {
                    this.Status       = StepStatusEnum.Warning;
                    this.ResultDetail = new StepResultDetail("Not all the machines are restarted successfully. The following machines are not shutdown successfully: {0}".FormatWith(string.Join(", ", failedMachineList.ToArray())));
                }
                else
                {
                    this.Status       = StepStatusEnum.Pass;
                    this.ResultDetail = new StepResultDetail("All the machines are restarted successfully.");
                }
            }
            catch (Exception ex)
            {
                this.Status       = StepStatusEnum.Failed;
                this.ResultDetail = new StepResultDetail("Error has occurred, please check log.", ExceptionHelper.CentralProcessSingle2(ex));
            }
            finally
            {
                if (this.ResultDetail != null)
                {
                    Log.Info(this.ResultDetail.Message);
                }
            }
        }