Script for automating a large assortment of AME related actions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
9.7 KiB

9 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.DirectoryServices;
  4. using System.DirectoryServices.AccountManagement;
  5. using Ameliorated.ConsoleUtils;
  6. using System.Linq;
  7. using System.Security;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace amecs.Actions
  12. {
  13. public static class UserPass
  14. {
  15. public static bool ShowMenu()
  16. {
  17. var mainMenu = new Ameliorated.ConsoleUtils.Menu()
  18. {
  19. Choices =
  20. {
  21. new Menu.MenuItem("Change Username", new Func<bool>(ChangeUsername)),
  22. new Menu.MenuItem("Change Password", new Func<bool>(ChangePassword)),
  23. new Menu.MenuItem("Change Display Name", new Func<bool>(ChangeDisplayName)),
  24. new Menu.MenuItem("Change Administrator Password", new Func<bool>(ChangeAdminPassword)),
  25. Menu.MenuItem.Blank,
  26. new Menu.MenuItem("Return to Menu", new Func<bool>(() => true)),
  27. new Menu.MenuItem("Exit", new Func<bool>(Globals.Exit)),
  28. },
  29. SelectionForeground = ConsoleColor.Green
  30. };
  31. mainMenu.Write();
  32. var result = (Func<bool>)mainMenu.Load();
  33. return result.Invoke();
  34. }
  35. public static bool ChangeUsername()
  36. {
  37. try
  38. {
  39. while (true)
  40. {
  41. var username = new InputPrompt() { Text = "Enter new username, or press escape to quit: " }.Start();
  42. if (username == null)
  43. return true;
  44. if (String.IsNullOrEmpty(username) || !Regex.Match(username, @"^\w[\w\.\- ]{0,19}$").Success)
  45. {
  46. ConsoleTUI.OpenFrame.WriteLine("Username is invalid.");
  47. Console.WriteLine();
  48. continue;
  49. }
  50. if (Globals.Username.Equals(username))
  51. {
  52. ConsoleTUI.OpenFrame.WriteLine("Username matches the current username.");
  53. Console.WriteLine();
  54. continue;
  55. }
  56. try
  57. {
  58. ConsoleTUI.OpenFrame.WriteCentered("\r\nSetting new username");
  59. using (new ConsoleUtils.LoadingIndicator(true))
  60. {
  61. DirectoryEntry entry = (DirectoryEntry)Globals.User.GetUnderlyingObject();
  62. entry.Rename(username);
  63. entry.CommitChanges();
  64. PrincipalContext context = new PrincipalContext(ContextType.Machine);
  65. PrincipalSearcher userPrincipalSearcher = new PrincipalSearcher(new UserPrincipal(context));
  66. Globals.User = userPrincipalSearcher.FindAll().FirstOrDefault(x => (x is UserPrincipal) && x.Sid.Value == Globals.UserSID) as UserPrincipal;
  67. break;
  68. }
  69. } catch (System.Runtime.InteropServices.COMException e)
  70. {
  71. if (e.ErrorCode != -2147022694)
  72. throw e;
  73. ConsoleTUI.OpenFrame.WriteLine("Username is invalid.");
  74. Console.WriteLine();
  75. }
  76. }
  77. } catch (Exception e)
  78. {
  79. Console.WriteLine();
  80. ConsoleTUI.OpenFrame.Close("Error: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  81. return false;
  82. }
  83. Console.WriteLine();
  84. if ((int?)ConsoleTUI.OpenFrame.Close("Username changed successfully", ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt()
  85. {
  86. TextForeground = ConsoleColor.Yellow,
  87. Text = "Logoff to apply changes? (Y/N): "
  88. }) == 0) amecs.RestartWindows(true);
  89. return true;
  90. }
  91. public static bool ChangePassword()
  92. {
  93. try
  94. {
  95. while (true)
  96. {
  97. var password = new InputPrompt() { MaskInput = true, Text = "Enter new password, or press escape to quit: " }.Start();
  98. if (password == null)
  99. return true;
  100. ConsoleTUI.OpenFrame.WriteCentered("\r\nSetting new password");
  101. try
  102. {
  103. using (new ConsoleUtils.LoadingIndicator(true))
  104. {
  105. if (String.IsNullOrEmpty(password))
  106. {
  107. Globals.User.SetPassword("");
  108. }
  109. else
  110. {
  111. Globals.User.SetPassword(password);
  112. }
  113. Thread.Sleep(800);
  114. break;
  115. }
  116. } catch (PasswordException e)
  117. {
  118. ConsoleTUI.OpenFrame.WriteLine("Could not set password: " + e.Message);
  119. Console.WriteLine();
  120. }
  121. }
  122. } catch (Exception e)
  123. {
  124. Console.WriteLine();
  125. ConsoleTUI.OpenFrame.Close("Error: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  126. return false;
  127. }
  128. Console.WriteLine();
  129. ConsoleTUI.OpenFrame.Close("Password changed successfully", ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  130. return true;
  131. }
  132. public static bool ChangeAdminPassword()
  133. {
  134. try
  135. {
  136. PrincipalContext context = new PrincipalContext(ContextType.Machine);
  137. PrincipalSearcher userPrincipalSearcher = new PrincipalSearcher(new UserPrincipal(context));
  138. var administrator = userPrincipalSearcher.FindAll().FirstOrDefault(x => (x is UserPrincipal) && x.Name == "Administrator") as UserPrincipal;
  139. while (true)
  140. {
  141. var password = new InputPrompt() { MaskInput = true, Text = "Enter new Administrator password,\r\nor press escape to quit: " }.Start();
  142. if (password == null)
  143. return true;
  144. ConsoleTUI.OpenFrame.WriteCentered("\r\nSetting new password");
  145. try
  146. {
  147. using (new ConsoleUtils.LoadingIndicator(true))
  148. {
  149. if (String.IsNullOrEmpty(password))
  150. {
  151. administrator.SetPassword("");
  152. }
  153. else
  154. {
  155. administrator.SetPassword(password);
  156. }
  157. Thread.Sleep(1000);
  158. break;
  159. }
  160. } catch (PasswordException e)
  161. {
  162. ConsoleTUI.OpenFrame.WriteLine("Could not set password: " + e.Message);
  163. Console.WriteLine();
  164. }
  165. }
  166. } catch (Exception e)
  167. {
  168. Console.WriteLine();
  169. ConsoleTUI.OpenFrame.Close("Error: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  170. return false;
  171. }
  172. Console.WriteLine();
  173. ConsoleTUI.OpenFrame.Close("Administrator password changed successfully", ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  174. return true;
  175. }
  176. public static bool ChangeDisplayName()
  177. {
  178. try
  179. {
  180. var name = new InputPrompt() { Text = "Enter new display name, or press escape to quit: " }.Start();
  181. if (name == null)
  182. return true;
  183. ConsoleTUI.OpenFrame.WriteCentered("\r\nSetting new display name");
  184. using (new ConsoleUtils.LoadingIndicator(true))
  185. {
  186. Globals.User.DisplayName = name;
  187. Globals.User.Save();
  188. Thread.Sleep(800);
  189. }
  190. } catch (Exception e)
  191. {
  192. Console.WriteLine();
  193. ConsoleTUI.OpenFrame.Close("Error: " + e.Message.TrimEnd('\n').TrimEnd('\r'), ConsoleColor.Red, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  194. return false;
  195. }
  196. Console.WriteLine();
  197. ConsoleTUI.OpenFrame.Close("Display name changed successfully", ConsoleColor.Green, Console.BackgroundColor, new ChoicePrompt() {AnyKey = true, Text = "Press any key to return to the Menu: "});
  198. return true;
  199. }
  200. }
  201. }