public User GetLoggedOnUser() { try { User CurrentUser = (User)System.Web.HttpContext.Current.Session["CurrentUser"]; if (CurrentUser == null) { String Username = System.Web.HttpContext.Current.User.Identity.Name; DataLayer.EPSEntities db = new DataLayer.EPSEntities(); User user = db.Users.Where(u => u.Username == Username).FirstOrDefault(); System.Web.HttpContext.Current.Session["CurrentUser"] = user; return(user); } else { return(CurrentUser); } } catch (Exception exUser) { string dummy = exUser.Message; return(null); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); try { Utilities util = new Utilities(); String emailServer = util.GetParam("SMTPServer", "smtp (email) server name"); String from = util.GetParam("EmailFrom", "email address to send from"); String to = util.GetParam("HelpDeskEmail", "email address to send to for helpdesk tickets"); String body = util.GetParam("ClonedLaptopBody", "message to create a helpdesk ticket for cloned laptops"); String subject = util.GetParam("ClonedLaptopSubject", "subject line to create a helpdesk ticket for cloned laptops"); util.SendEmail(EmpID, from, to, null, null, subject, body); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("The email was sent to '{0}' regarding {1} {2} for their cloned laptop.", to, emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); try { Utilities util = new Utilities(); String emailServer = util.GetParam("SMTPServer", "smtp (email) server name"); String from = util.GetParam("EmailFrom", "email address to send from"); String to = util.GetParam("WifiAdminEmailAddress", "email address for the wifi admin"); String body = util.GetParam("WifiAdminNotifyBody", "message to wifi admin to notify them of a disabled employee"); String subject = util.GetParam("WifiAdminNotifySubject", "subject line for the email to send the wifi admin to notify them of a disabled employee"); util.SendEmail(EmpID, from, to, null, null, subject, body); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("The email was sent to '{0}' regarding {1} {2}.", to, emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory Domain name"); String Script = ""; try { Script = "Set-ADUser " + emp.Username + " -Replace @{msExchHideFromAddressLists=\"TRUE\"}"; String result = util.RunPSScript(Script); if (!String.IsNullOrEmpty(result)) { throw new Exception(result); } return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1} was hidden from the Exchange address lists.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}... Script Run: {1}", ex.Message, Script), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); try { Utilities util = new Utilities(); String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); String htmlOptions = li.HtmlOptions; Employee manager = db.Employees.Where(e => e.EmpID == emp.ReportsTo).FirstOrDefault(); if (manager == null) { throw new Exception(String.Format("There is no manager assigned to {0} {1}.", emp.FirstName, emp.LastName)); } if (String.IsNullOrEmpty(manager.Email)) { throw new Exception(String.Format("The manager assigned to {0} {1} does not have an email set.", emp.FirstName, emp.LastName)); } String emailServer = util.GetParam("SMTPServer", "smtp (email) server name"); String from = util.GetParam("EmailFrom", "email address to send from"); String to = manager.Email; String body = util.GetParam("FinalDisableNotifyBody", "message to disable distro group to notify them of a disabled employee"); String subject = util.GetParam("FinalDisableNotifySubject", "subject line for the email to send the disable distro group to notify them of a disabled employee"); List <RunPayloadItem> thisPL = RunPayload.RunPayloadItems.Where(p => p.ItemID == li.ItemID).ToList(); string numDays = thisPL.Where(p => p.ElementID == "NumDays").FirstOrDefault().ElementValue; string ticketNumber = thisPL.Where(p => p.ElementID == "TicketNumber").FirstOrDefault().ElementValue; body = body.Replace("[NumDays]", numDays); body = body.Replace("[TicketNumber]", ticketNumber); util.SendEmail(EmpID, from, to, null, null, subject, body); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("The email was sent to '{0}' regarding {1} {2} for their final notification.", manager.Email, emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity (context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } int firstIndex = user.Description.IndexOf("**"); if (firstIndex > -1) { int lastIndex = user.Description.IndexOf("**", firstIndex + 3) + 2; if (lastIndex > -1) { int count = lastIndex - firstIndex; String newDesc = user.Description.Remove(firstIndex, count); user.Description = newDesc; user.Save(); } } return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("Employee: {0} {1} had the description cleared in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); String htmlOptions = li.HtmlOptions; Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity (context, emp.Username); List <RunPayloadItem> thisPL = RunPayload.RunPayloadItems.Where(p => p.ItemID == li.ItemID).ToList(); string techName = thisPL.Where(p => p.ElementID == "TechInit").FirstOrDefault().ElementValue; string ticketNumber = thisPL.Where(p => p.ElementID == "TicketNumber").FirstOrDefault().ElementValue; if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } String CurrentDesc = user.Description; user.Description = String.Format("{3} ** Disabled on {0} by {1}. Ticket Number: {2} **", DateTime.Now.ToString("MM/dd/yyyy"), techName, ticketNumber, CurrentDesc); user.Save(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("Employee: {0} {1} had the description updated in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); if (domain == null) { throw new Exception("The Active Directory domain is not configured in the parameters table"); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity (context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } if (user.Enabled == false) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} was already disabled in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } user.Enabled = false; user.Save(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("Employee: {0} {1} was disabled in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String defaultPassword = String.Format("AA{0}^", Guid.NewGuid().ToString()); PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } if (user.AccountExpirationDate == null) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} was not expired in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } user.AccountExpirationDate = null; user.Save(); user.SetPassword(defaultPassword); user.ExpirePasswordNow(); user.Save(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1} was removed from expiration in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); try { Utilities util = new Utilities(); Employee manager = db.Employees.Where(e => e.EmpID == emp.ReportsTo).FirstOrDefault(); if (manager == null) { throw new Exception(String.Format("There is no manager assigned to {0} {1}.", emp.FirstName, emp.LastName)); } if (String.IsNullOrEmpty(manager.Email)) { throw new Exception(String.Format("The manager assigned to {0} {1} does not have an email set.", emp.FirstName, emp.LastName)); } String emailServer = util.GetParam("SMTPServer", "smtp (email) server name"); String from = util.GetParam("EmailFrom", "email address to send from"); String to = manager.Email; String body = util.GetParam("DisabledNotifyBody", "message to managers to notify them of a disabled employee"); String subject = util.GetParam("DisabledNotifySubject", "subject line for the email to send managers to notify them of a disabled employee"); foreach (PropertyInfo prop in typeof(Employee).GetProperties()) { body = body.Replace(String.Format("[{0}]", prop.Name), prop.GetValue(emp).ToString()); subject = subject.Replace(String.Format("[{0}]", prop.Name), prop.GetValue(emp).ToString()); } util.SendEmail(emp.EmpID, from, to, null, null, subject, body); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("The email was sent to {0} {1}.", manager.FirstName, manager.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); String htmlOptions = li.HtmlOptions; String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); List <RunPayloadItem> thisPL = RunPayload.RunPayloadItems.Where(p => p.ItemID == li.ItemID).ToList(); string compAcct = thisPL.Where(p => p.ElementID == "CompAcct").FirstOrDefault().ElementValue; PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); ComputerPrincipal comp = ComputerPrincipal.FindByIdentity (context, compAcct); if (comp == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", compAcct), TimeDone = DateTime.Now }); } comp.Delete(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("Computer name: {0} was removed from Active Directory.", compAcct), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public void WriteErrorToLog(String URL, Exception ex, String AdditionalMessage) { try { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); User CurrentUser = (User)System.Web.HttpContext.Current.Session["CurrentUser"]; string ErrorText = string.Format("{2}Error Message: {0}, Inner Exception Message: {1}, Trace Data: {3}", ex.Message, ex.InnerException == null ? "None." : ex.InnerException.Message, AdditionalMessage == "" ? "" : AdditionalMessage + " - ", ex.StackTrace); ErrorLog error = new ErrorLog { ErrorMessage = URL + " - " + ErrorText, Username = CurrentUser == null ? "Not Logged In" : CurrentUser.Username, ErrorDate = DateTime.Now }; db.ErrorLogs.Add(error); db.SaveChanges(); } catch (Exception ex1) { string dummy = ex1.Message; } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); try { Utilities util = new Utilities(); String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); String htmlOptions = li.HtmlOptions; String disableGroup = util.GetParam("DisableDistroGroupEmail", "Disable Distro Group email address"); String emailServer = util.GetParam("SMTPServer", "smtp (email) server name"); String from = util.GetParam("EmailFrom", "email address to send from"); String to = disableGroup; String body = util.GetParam("DisableDistroGroupBody", "message to disable distro group to notify them of a disabled employee"); String subject = util.GetParam("DisableDistroGroupSubject", "subject line for the email to send the disable distro group to notify them of a disabled employee"); List <RunPayloadItem> thisPL = RunPayload.RunPayloadItems.Where(p => p.ItemID == li.ItemID).ToList(); string ticketNumber = thisPL.Where(p => p.ElementID == "TicketNumber").FirstOrDefault().ElementValue; body = body.Replace("[TicketNumber]", ticketNumber); util.SendEmail(EmpID, from, to, null, null, subject, body); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("The email was sent to '{0}' regarding {1} {2}.", disableGroup, emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory Domain name"); String Script = ""; try { Script = "$CallEMS = \". '$env:ExchangeInstallPath\\bin\\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell\"; Invoke-Expression $CallEMS;"; Script = Script + "Get-Mailbox | Remove-MailboxPermission -User " + emp.Username + " -AccessRights Fullaccess -InheritanceType all -Confirm:$false;"; Script = Script + "Get-PublicFolder -Recurse | Remove-PublicFolderClientPermission -User " + emp.Username + " -Confirm:$false;"; String result = util.RunPSScript(Script); if (!result.Contains("There is no existing permission entry found for user")) { if (!String.IsNullOrEmpty(result)) { throw new Exception(result); } } return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1}'s access and public folder permissions were removed.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}... Script Run: {1}", ex.Message, Script), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String disabledFolderPath = util.GetParam("DisabledFolderPath", "disabled users folder path"); if (disabledFolderPath.EndsWith("\\")) { disabledFolderPath = disabledFolderPath.Remove(disabledFolderPath.Length - 1, 1); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); DirectoryEntry DE = (DirectoryEntry)user.GetUnderlyingObject(); String userFolder = String.Format("{0}\\{1}\\{2}\\UserFiles", disabledFolderPath, DateTime.Now.Year.ToString(), user.SamAccountName); if (!System.IO.Directory.Exists(disabledFolderPath)) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("The disabled users folder '{0}' could not be found.", disabledFolderPath), TimeDone = DateTime.Now }); } if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } if (!System.IO.Directory.Exists(userFolder)) { System.IO.Directory.CreateDirectory(userFolder); } var HD = DE.InvokeGet("TerminalServicesHomeDirectory"); if (HD == null) { HD = user.HomeDirectory; } String homeFolder = HD == null ? "" : HD.ToString(); if (String.IsNullOrEmpty(homeFolder)) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} {1}'s home folder is not set.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } foreach (string dirPath in Directory.GetDirectories(homeFolder, "*", SearchOption.AllDirectories)) { string newPath = dirPath.Replace(homeFolder, userFolder); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } } foreach (string oldFile in Directory.GetFiles(homeFolder, "*.*", SearchOption.AllDirectories)) { File.SetAttributes(oldFile, System.IO.FileAttributes.Normal); } foreach (string oldFile in Directory.GetFiles(homeFolder, "*.*", SearchOption.AllDirectories)) { String newFile = oldFile.Replace(homeFolder, userFolder); if (!File.Exists(newFile)) { File.SetAttributes(oldFile, System.IO.FileAttributes.Normal); File.Move(oldFile, newFile); } } System.IO.Directory.Delete(homeFolder, true); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1}'s home folder documents were moved from '{3}' to '{2}'.", emp.FirstName, emp.LastName, userFolder, homeFolder), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String disabledFolderPath = util.GetParam("DisabledFolderPath", "disabled users folder path"); if (disabledFolderPath.EndsWith("\\")) { disabledFolderPath = disabledFolderPath.Remove(disabledFolderPath.Length - 1, 1); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); DirectoryEntry DE = (DirectoryEntry)user.GetUnderlyingObject(); if (!System.IO.Directory.Exists(disabledFolderPath)) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("The disabled users folder '{0}' could not be found.", disabledFolderPath), TimeDone = DateTime.Now }); } if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } String userFolder = ""; foreach (string dirPath in Directory.GetDirectories(disabledFolderPath, "*", SearchOption.AllDirectories)) { if (userFolder != "") { break; } int yearFolder = 0; DirectoryInfo di = new DirectoryInfo(dirPath); string currentFolder = di.Name; if (currentFolder.Length == 4 && int.TryParse(currentFolder, out yearFolder) == true) { foreach (string uf in Directory.GetDirectories(di.FullName, "*", SearchOption.TopDirectoryOnly)) { if (uf.EndsWith("\\" + emp.Username)) { userFolder = uf; break; } } } } if (userFolder == "") { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} {1}'s folder was not found in the disabled folder path.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } var profilePath = DE.InvokeGet("TerminalServicesProfilePath"); String homeFolder = profilePath == null ? "" : profilePath.ToString(); if (String.IsNullOrEmpty(homeFolder)) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} {1}'s home folder is not set.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } foreach (string dirPath in Directory.GetDirectories(userFolder, "*", SearchOption.AllDirectories)) { string newPath = dirPath.Replace(userFolder, homeFolder); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } } foreach (string oldFile in Directory.GetFiles(userFolder, "*.*", SearchOption.AllDirectories)) { File.SetAttributes(oldFile, System.IO.FileAttributes.Normal); } foreach (string oldFile in Directory.GetFiles(userFolder, "*.*", SearchOption.AllDirectories)) { String newFile = oldFile.Replace(userFolder, homeFolder); if (!File.Exists(newFile)) { File.SetAttributes(oldFile, System.IO.FileAttributes.Normal); File.Move(oldFile, newFile); } } System.IO.Directory.Delete(userFolder, true); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1}'s profile folder was moved from '{3}' to '{2}'.", emp.FirstName, emp.LastName, homeFolder, userFolder), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String ou = util.GetParam("DisabledUsersOU", "Active Directory Disabled Users OU path"); String defCN = ""; RunWorkflow wf = db.RunWorkflows.Where(r => r.EmpID == EmpID && r.RunPayloads.Count() > 0).OrderByDescending(r => r.StartTime).FirstOrDefault(); if (wf != null) { RunPayloadItem i = (from rpl in wf.RunPayloads select rpl.RunPayloadItems.Where(pli => pli.ElementID == "UserOUPath").FirstOrDefault()).FirstOrDefault(); if (i != null) { defCN = i.ElementValue; } } if (defCN == "") { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", "The user's original OU could not be found. Please move them manually from the disabled OU."), TimeDone = DateTime.Now }); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } Boolean isInOUAlready = false; DirectoryEntry userOU = new DirectoryEntry("LDAP://" + user.DistinguishedName, adminName, password); DirectoryEntry defaultCN = new DirectoryEntry(defCN, adminName, password); String OUCheckUser = userOU.Path.ToString(); if (OUCheckUser.EndsWith(defCN.Replace("LDAP://", ""))) { isInOUAlready = true; } if (isInOUAlready == true) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} is already in the default Users container.", emp.Username), TimeDone = DateTime.Now }); } userOU.MoveTo(defaultCN); userOU.Close(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1} was moved to the default Users container in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
static void Main(string[] args) { try { string currUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name; int AuditUserID = 0; string pathToFile = args.Count() == 0 ? "" : args[0]; DataLayer.EPSEntities db = new DataLayer.EPSEntities(); EmployeeFunctions funct = new EmployeeFunctions(); Utilities util = new Utilities(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); int slashIndex = currUser.IndexOf("\\") + 1; currUser = currUser.Substring(slashIndex, (currUser.Length - slashIndex)); User admin = db.Users.Where(e => e.Username == currUser).FirstOrDefault(); if (admin == null) { Console.WriteLine("ERROR: You are not a user in the EPS system."); Console.ReadKey(); Environment.Exit(0); } AuditUserID = admin.UserID; if (String.IsNullOrEmpty(pathToFile)) { Console.WriteLine("ERROR: No file was entered."); Console.ReadKey(); Environment.Exit(0); } if (!System.IO.File.Exists(pathToFile)) { Console.WriteLine("ERROR: The file " + pathToFile + " does not exist."); Console.ReadKey(); Environment.Exit(0); } String[] users = System.IO.File.ReadAllLines(pathToFile); if (users.Count() == 0) { Console.WriteLine("ERROR: No users were found in the file " + pathToFile + "."); Console.ReadKey(); Environment.Exit(0); } foreach (String u in users) { DataLayer.Employee emp = db.Employees.Where(e => e.Username == u).FirstOrDefault(); if (emp == null) { PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, u); if (user == null) { Console.WriteLine("ERROR: " + u + " could not be found in Active Directory."); Console.ReadKey(); Environment.Exit(0); } DirectoryEntry DE = (DirectoryEntry)user.GetUnderlyingObject(); String isManager = funct.IsAManager(DE.Path, adminName, password, domain).ToString(); String vbManagerID = null; String vbEmpNum = ""; if (DE.Properties["employeeNumber"].Count > 0) { vbEmpNum = DE.Properties["employeeNumber"][0].ToString(); } if (DE.Properties["manager"].Count > 0) { String vbManagerPath = DE.Properties["manager"][0].ToString(); Guid vbManagerGUID = funct.GetUserByPath(vbManagerPath).Guid; funct.AddMissingManagers(vbManagerPath, AuditUserID); Employee manager = db.Employees.Where(e => e.ADGUID == vbManagerGUID).FirstOrDefault(); vbManagerID = manager.EmpID.ToString(); } funct.AddEmployee(user.Guid.ToString(), user.EmailAddress == null ? "" : user.EmailAddress, user.GivenName == null ? "" : user.GivenName, user.Surname == null ? "" : user.Surname, u, isManager, vbManagerID, vbEmpNum, AuditUserID); Console.WriteLine(u + " added."); } else { Console.WriteLine(u + " already exists in EPS."); } } Console.WriteLine(""); Console.WriteLine("Users imported successfully."); Console.ReadKey(); Environment.Exit(0); } catch (Exception ex) { Console.WriteLine("ERROR: " + ex.Message); Console.ReadKey(); Environment.Exit(0); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String disabledFolderPath = util.GetParam("DisabledFolderPath", "disabled users folder path"); if (disabledFolderPath.EndsWith("\\")) { disabledFolderPath = disabledFolderPath.Remove(disabledFolderPath.Length - 1, 1); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); DirectoryEntry DE = (DirectoryEntry)user.GetUnderlyingObject(); String userFolder = String.Format("{0}\\{1}\\{2}\\MailBox_Archive_Export", disabledFolderPath, DateTime.Now.Year.ToString(), user.SamAccountName); if (String.IsNullOrEmpty(user.EmailAddress)) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} {1} does not have an email address in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } if (!System.IO.Directory.Exists(disabledFolderPath)) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("The disabled users folder '{0}' could not be found.", disabledFolderPath), TimeDone = DateTime.Now }); } if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } var archiveName = DE.InvokeGet("msExchArchiveName"); if (archiveName == null || String.IsNullOrEmpty(archiveName.ToString())) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} does not have an archive folder set.", emp.Username), TimeDone = DateTime.Now }); } if (!System.IO.Directory.Exists(userFolder)) { System.IO.Directory.CreateDirectory(userFolder); } String Script = ""; try { Script = "$CallEMS = \". '$env:ExchangeInstallPath\\bin\\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell\"; Invoke-Expression $CallEMS; "; Script = Script + String.Format("New-MailboxExportRequest -Mailbox {0} -FilePath \"{1}\\ArchiveExport.pst\" -IsArchive -Priority High", emp.Username, userFolder); String result = util.RunPSScript(Script); if (!String.IsNullOrEmpty(result)) { throw new Exception(result); } return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1}'s archive export was started going to {2}. Exchange Server will continue to process until it is complete.", emp.FirstName, emp.LastName, userFolder), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}... Script Run: {1}", ex.Message, Script), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String disabledFolderPath = util.GetParam("DisabledFolderPath", "disabled users folder path"); if (disabledFolderPath.EndsWith("\\")) { disabledFolderPath = disabledFolderPath.Remove(disabledFolderPath.Length - 1, 1); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); String userFolder = String.Format("{0}\\{1}\\{2}", disabledFolderPath, DateTime.Now.Year.ToString(), user.SamAccountName); String groupNameFilePath = String.Format("{0}\\ADGroups.txt", userFolder); StringBuilder sbGroupNamesFile = new StringBuilder(); if (!System.IO.Directory.Exists(disabledFolderPath)) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("The disabled users folder '{0}' could not be found.", disabledFolderPath), TimeDone = DateTime.Now }); } if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } if (!System.IO.Directory.Exists(userFolder)) { System.IO.Directory.CreateDirectory(userFolder); } PrincipalSearchResult <Principal> groups = user.GetGroups(); foreach (var g in groups) { sbGroupNamesFile.AppendFormat("{0}" + Environment.NewLine, g.Name); } if (System.IO.File.Exists(groupNameFilePath)) { System.IO.File.Delete(groupNameFilePath); } System.IO.File.WriteAllText(groupNameFilePath, sbGroupNamesFile.ToString()); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1}'s Active Directory groups have been documented in '{2}'.", emp.FirstName, emp.LastName, groupNameFilePath), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String ou = util.GetParam("DisabledUsersOU", "Active Directory Disabled Users OU path"); String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } Boolean isInOUAlready = false; DirectoryEntry userOU = new DirectoryEntry("LDAP://" + user.DistinguishedName, adminName, password); DirectoryEntry disabledOU = new DirectoryEntry("LDAP://" + ou, adminName, password); String OUCheckUser = userOU.Path.ToString(); if (OUCheckUser.EndsWith(disabledOU.Path.Replace("LDAP://", ""))) { isInOUAlready = true; } if (isInOUAlready == true) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} is already in the Disabled Users OU.", emp.Username), TimeDone = DateTime.Now }); } String plPath = userOU.Path.Replace(String.Format("CN={0} {1},", emp.FirstName, emp.LastName), ""); userOU.MoveTo(disabledOU); userOU.Close(); String htmlAnswers = String.Format("{1}:UserOUPath={0}", plPath, li.ItemID); RunWorkflow run = db.RunWorkflows.Where(r => r.EmpID == EmpID).OrderByDescending(r => r.StartTime).FirstOrDefault(); util.SetPayload(run.RunID, li.ItemID, htmlAnswers); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1} was moved to the Disabled Users OU in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }