Beispiel #1
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);
                }
            }
        }