public ActionResult StartDirSync(FormCollection _POST) { try { model.StartDirSync = new CustomStartDirSync() { Organization = _POST["organization"], Policy = _POST["policy"], Force = _POST["force"] == "on" ? true : false }; CommonCAS.Log(string.Format("has run Office365/StartDirSync() with policy '{0}' for '{1}'", model.StartDirSync.Policy, model.StartDirSync.Organization)); // execute powershell script and dispose powershell object using (MyPowerShell ps = new MyPowerShell()) { ps.StartDirSync(model.StartDirSync.Organization, model.StartDirSync.Policy, model.StartDirSync.Force); var result = ps.Invoke(); if (result.Count() == 0) { model.OKMessage.Add(string.Format("Dirsync {0} has been started for {1}.", model.StartDirSync.Policy, model.StartDirSync.Organization)); } else { foreach (PSObject message in result) { CommonCAS.Log(string.Format("Dirsync {0} info: {1}", model.StartDirSync.Policy, message.ToString())); throw new Exception(string.Format(message.ToString())); } } } CommonCAS.Stats("Office365/StartDirSync"); return(View("StartDirSync", model)); } catch (Exception exc) { CommonCAS.Log("Exception: " + exc.Message); model.ActionFailed = true; model.Message = exc.Message; return(View(model)); } }