コード例 #1
0
        public ActionResult ScheduleReboot(FormCollection _POST)
        {
            try
            {
                // Schedule Reboot and create View.
                CustomScheduleReboot ScheduleReboot = new CustomScheduleReboot()
                {
                    TaskID   = _POST["ScheduleReboot.TaskID"].ToString(),
                    VMID     = _POST["vmid"].ToUpper(),
                    DateTime = _POST["datetime"].ToString(),
                    Email    = _POST["ScheduleReboot.Email"]
                };

                if (ScheduleReboot.TaskID.Length == 0)
                {
                    throw new ArgumentException("Please enter a task id");
                }

                if (ScheduleReboot.TaskID.Length < 6 || ScheduleReboot.TaskID.Length > 8)
                {
                    throw new ArgumentException("The taskid must be 6 characters long.");
                }

                CommonCAS.Log(string.Format("has run Service/ScheduleReboot() to Reboot {0} at date {1}, with TaskID {2}.", ScheduleReboot.VMID, ScheduleReboot.DateTime, ScheduleReboot.TaskID));

                // execute powershell script and dispose powershell object
                using (MyPowerShell ps = new MyPowerShell())
                {
                    ps.ScheduleReboot(ScheduleReboot.VMID, ScheduleReboot.DateTime, ScheduleReboot.Email, ScheduleReboot.TaskID);
                    var result = ps.Invoke();

                    if (result.Count() == 0)
                    {
                        model.OKMessage.Add(string.Format("The VM has been scheduled for reboot on {0}", ScheduleReboot.DateTime));
                    }
                    else
                    {
                        foreach (PSObject message in result)
                        {
                            model.OKMessage.Add(message.ToString());
                            CommonCAS.Log(string.Format("Has run Service/ScheduleReboot() with info: {1}", ScheduleReboot.TaskID, message.ToString()));
                        }
                    }
                }

                CommonCAS.Stats("Service/ScheduleReboot");

                return(View("ScheduleReboot", model));
            }
            catch (Exception exc)
            {
                CommonCAS.Log("Exception: " + exc.Message);
                model.ActionFailed = true;
                model.Message      = exc.Message;
                return(View(model));
            }
        }