PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/CommandLine/ADModifyMain.cs

#
C# | 3072 lines | 2468 code | 316 blank | 288 comment | 181 complexity | 2118bbae88426453d678a4c781974638 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.DirectoryServices;
  3. using ADModify.Library;
  4. using System.Xml;
  5. using System.IO;
  6. using Microsoft.Win32;
  7. namespace ADModify
  8. {
  9. /// <summary>
  10. /// Main is the program entry point. In main we check for arguments and change global variables depending on what
  11. /// was entered. If we have in fact found valid attribute switches, then the GetDN method is called. This Method
  12. /// does the initial LDAP query to find the DN for all users matching the LDAP filter specified.
  13. ///
  14. /// Once the have the users we test all globals for a non null value for string and a true value for boolean. If
  15. /// these test true, then we call the specified method from the ModifyAttributes class and pass all necesarry parameters.
  16. /// Modification and reporting is done within the methods of the ModifyAttributes and LogFile classes.
  17. /// </summary>
  18. class ADModifyMain
  19. {
  20. //public variables
  21. //set all string values to null and all bool values to false
  22. //later on we test these conditions and execute the modify only if string values
  23. //are !null and bool values are true
  24. public static int usercount = 0;
  25. public static int querycount = 0;
  26. //ldap settings
  27. public static string servername = null;
  28. public static int pageSize = 1000;
  29. //Terminal Server Attributes
  30. public static string TSProfilePath = null;
  31. public static string TSFolderPath = null;
  32. public static string TSHomeDrive = null;
  33. public static bool TSEnable = false;
  34. public static bool TSDisable = false;
  35. public static bool RemoteEnable = false;
  36. public static bool RemoteDisable = false;
  37. public static bool RequirePermission = false;
  38. public static string program = null;
  39. public static string startIn = null;
  40. public static bool InteractWithSession = false;
  41. public static bool SetDisconnectTime = false;
  42. public static int DisconnectTime;
  43. public static bool SetMaxConnectionTime = false;
  44. public static int MaxConnectionTime;
  45. public static bool SetMaxIdleTime = false;
  46. public static int MaxIdleTime;
  47. public static bool SetEndSession = false;
  48. public static bool EndSession = false;
  49. public static bool SetOriginatingOnly = false;
  50. public static bool OriginatingOnly = false;
  51. public static bool SetConnectClientDrives = false;
  52. public static bool ConnectClientDrives = false;
  53. public static bool SetConnectClientPrinters = false;
  54. public static bool ConnectClientPrinters = false;
  55. public static bool SetDefaultToMainPrinter = false;
  56. public static bool DefaultToMainPrinter = false;
  57. //Exchange related attributes
  58. public static bool killmail = false;
  59. public static bool includeinrecipientpolicy = false;
  60. public static bool excludefromrecipientpolicy = false;
  61. public static bool includeinmailboxmanager = false;
  62. public static bool excludefrommailboxmanager = false;
  63. public static bool enablehttp = false;
  64. public static bool disablehttp = false;
  65. public static bool enablepop3 = false;
  66. public static bool disablepop3 = false;
  67. public static bool enableimap4 = false;
  68. public static bool disableimap4 = false;
  69. public static bool enablemapi = false;
  70. public static bool disablemapi = false;
  71. public static bool enablecachedmapi = false;
  72. public static bool EnableOMA = false;
  73. public static bool EnableUIS = false;
  74. public static bool EnableUTD = false;
  75. public static string smtpaddress = null;
  76. public static bool setasprimary = false;
  77. public static bool updatemail = false;
  78. public static bool removeaddress = false;
  79. public static bool keepprimary = false;
  80. public static string addresstoremove = null;
  81. public static bool showinaddresslists = false;
  82. public static bool hidefromaddresslists = false;
  83. public static bool SetMailboxRights = false;
  84. public static bool ClearMailboxRights = false;
  85. public static bool FixSendAs = false;
  86. public static bool GrantSelfAEA = false;
  87. public static bool RemoveAEAFromSelf = false;
  88. public static bool RemoveSendAs = false;
  89. public static bool GrantSelfFullAndRead = false;
  90. public static bool RemoveSelfFullAndRead = false;
  91. public static bool DumpMailboxRights = false;
  92. //User Account Settings
  93. public static bool disableaccount = false;
  94. public static bool enableaccount = false;
  95. public static bool PasswordNotRequired = false;
  96. public static bool SetPasswordNotRequired = false;
  97. public static bool SetPasswordNeverExpires = false;
  98. public static bool PasswordNeverExpires = false;
  99. public static bool MustChangePasswd = false;
  100. public static bool SetMustChangePassword = false;
  101. public static bool CannotChangePasswd = false;
  102. public static bool SetCannotChangePassword = false;
  103. public static bool UseReversibleEncryption = false;
  104. public static bool SetUseReversibleEncryption = false;
  105. public static bool SetRequireSmartCard = false;
  106. public static bool RequireSmartCard = false;
  107. public static bool SetCannotBeDelegated = false;
  108. public static bool CannotBeDelegated = false;
  109. public static bool SetUseDESEncryption = false;
  110. public static bool UseDESEncryption = false;
  111. public static bool SetDoNotRequireKerberosPreAuth = false;
  112. public static bool DoNotRequireKerberosPreAuth = false;
  113. public static bool AddMembersToGroup = false;
  114. public static bool SetAccountExpires = false;
  115. public static string ExpireTime = null;
  116. public static bool RemoveMembersFromGroup = false;
  117. public static string DNOfGroup = null;
  118. public static bool ModifyRDN = false;
  119. public static string NewRDN = null;
  120. //Permissions Settings
  121. public static bool Setallowinheritable = false;
  122. public static bool AllowInheritable = false;
  123. public static bool DumpDacl = false;
  124. public static bool DumpDaclInheritance = false;
  125. //custom attribute set
  126. public static string customAttributeName = null;
  127. public static string customAttributeValue = null;
  128. public static bool multi = false;
  129. public static bool remove = false;
  130. //undo mode
  131. public static bool UndoMode = false;
  132. public static string logFileName = null;
  133. //mailbox rights
  134. public static bool GrantSendAsRights = false;
  135. public static bool ImportMailboxRights = false;
  136. public static bool ModifyMailboxRights = false;
  137. public static bool RemoveMailboxRights = false;
  138. public static string Trustee = null;
  139. public static string Permission = null;
  140. public static bool Deny = false;
  141. public static bool MailboxRightsOverwrite = false;
  142. //timer
  143. public static float timeElapsed = 0;
  144. //progress bar
  145. public static int UserCountDividedWhole;
  146. public static float UserCountDividedDecimalCount;
  147. public static float UserCountDividedDecimalCountConst;
  148. public static int dots;
  149. /// <summary>
  150. ///
  151. /// ADModifyMain::GetDN
  152. ///
  153. /// Accepts:
  154. /// logWriter - Handle to Writer object for logging
  155. /// LDAPFilter - the LDAP filter used for object search
  156. /// SearchBase - the DN to begin the query
  157. /// subtree - boolean specifies either onelevel or subtree
  158. ///
  159. /// Returns:
  160. /// none
  161. ///
  162. /// GetDN accepts the Search Base, Scope, and LDAP Filter from args.
  163. /// GetDN runs the query and returns the list of users that match.
  164. /// At that point we test all public variables. If a value other than the default
  165. /// is set, we call ADModify!ModifyAttributes::* to modify the object and pass all
  166. /// appropriate arguments.
  167. /// </summary>
  168. static void GetDN(XmlWriter logWriter, string LDAPFilter, bool subtree, string SearchBase)
  169. {
  170. //Before doing anything, verify the server and base DN entered are valid
  171. if(servername!=null)
  172. {
  173. try
  174. {
  175. //do this to test if we can even connect to the server
  176. DirectoryEntry ent = new DirectoryEntry("LDAP://" + servername + "/" + SearchBase);
  177. string myTempVar = ent.Parent.ToString(); //if the servername is invalid, this line will raise an exception
  178. ent.Dispose();
  179. }
  180. catch(System.Exception caught)
  181. {
  182. if(caught.Message=="The server is not operational")
  183. {
  184. Console.WriteLine("\nAn error has occurred connecting to server " + servername + ".");
  185. Console.WriteLine("The error was: " + caught.Message + "\n");
  186. }
  187. else if(caught.Message=="A referral was returned from the server" || caught.Message=="There is no such object on the server")
  188. {
  189. Console.WriteLine("\nAn error has occured binding to Base DN " + SearchBase + ".");
  190. Console.WriteLine("The error was: " + caught.Message + ".");
  191. Console.WriteLine("Please enter a valid Base DN.\n");
  192. }
  193. else
  194. {
  195. Console.WriteLine("\nAn error has occurred binding to Active Directory.");
  196. Console.WriteLine("The error was: " + caught.Message + "\n");
  197. }
  198. return;
  199. }
  200. }
  201. //Initialize the log file
  202. LogFile log = new LogFile();
  203. //Check for mailbox rights dump mode and create logfile if necesarry
  204. XmlTextWriter DumpWriter = null;
  205. if(DumpMailboxRights)
  206. {
  207. if(File.Exists("mbxrights.xml"))
  208. {
  209. Console.WriteLine("\nThe file mbxrights.xml already exists. You must remove or rename this file before you can proceed.\n");
  210. return;
  211. }
  212. DumpWriter = new XmlTextWriter("mbxrights.xml", null);
  213. DumpWriter.WriteStartElement("MailboxRights");
  214. }
  215. //get OS version to determine supportability of TS modifications
  216. //string OSVersionFull = System.Environment.OSVersion.ToString();
  217. //string OSVersion = OSVersionFull.Substring(OSVersionFull.Length - 10, 10);
  218. //int OSVersionNoDots = Convert.ToInt32(OSVersion.Replace(".",""),10);
  219. //Check for Win2k3 so we know whether to enable terminal services options
  220. //get OS build to determine supportability of TS modifications
  221. //TODO: When longhorn releases we need to add their build number to this check
  222. System.OperatingSystem osInfo = System.Environment.OSVersion;
  223. //First things first... check for TS attributes. If there are any, check OS version before proceeding.
  224. if(TSProfilePath!=null | program!=null | startIn!=null | TSFolderPath!=null | TSHomeDrive!=null | TSEnable | TSDisable | RemoteEnable | RemoteDisable | SetDisconnectTime | SetMaxConnectionTime | SetMaxIdleTime | SetEndSession | SetOriginatingOnly | SetConnectClientDrives | SetConnectClientPrinters | SetDefaultToMainPrinter)
  225. {
  226. if (osInfo.Version.Build != 3790)
  227. {
  228. Console.WriteLine("Operating System must be at build 5.2.3790.0 or later to support this operation.\nCurrent OS build: " + osInfo.VersionString + "\n");
  229. return;
  230. }
  231. }
  232. //Also check for CDOEXM attributes. If there are any, make sure Exchange is installed.
  233. if(FixSendAs || GrantSelfAEA || RemoveAEAFromSelf || GrantSelfFullAndRead || RemoveSelfFullAndRead || DumpMailboxRights || ImportMailboxRights || ModifyMailboxRights || RemoveMailboxRights)
  234. {
  235. RegistryKey myRegKey = Registry.LocalMachine;
  236. RegistryKey mySubKey = myRegKey.OpenSubKey("Software\\Microsoft\\Exchange\\Setup");
  237. string ExchangeVersion = mySubKey.GetValue("NewestBuild").ToString();
  238. int ExchangeVersionInt = Convert.ToInt32(ExchangeVersion,10);
  239. if(ExchangeVersionInt<4417)
  240. {
  241. Console.WriteLine("Exchange Server must be installed and must be at build 4417 or later to support this modification.\nCurrent Exchange Build: " + ExchangeVersion + "\n");
  242. return;
  243. }
  244. }
  245. //bind to AD using the SearchBase specified by the user
  246. DirectoryEntry de = new DirectoryEntry("LDAP://" + SearchBase);
  247. //set searcher object with our LDAP filter
  248. DirectorySearcher src = new DirectorySearcher(LDAPFilter);
  249. src.SearchRoot = de;
  250. //set our search scope
  251. if(subtree)
  252. src.SearchScope = SearchScope.Subtree;
  253. else
  254. src.SearchScope = SearchScope.OneLevel;
  255. //Other settings for search
  256. src.PageSize = pageSize;
  257. src.PropertiesToLoad.Add("distinguishedName");
  258. SearchResultCollection results = null;
  259. //execute the query
  260. try
  261. {
  262. results = src.FindAll();
  263. }
  264. catch(System.Exception caught)
  265. {
  266. Console.WriteLine("An error occured while executing the query. The error was: {0}", caught.Message + "\n");
  267. return;
  268. }
  269. Console.WriteLine("\nIssuing Query....\n");
  270. if(results.Count == 1)
  271. {
  272. Console.WriteLine("1 item found matching the specified filter.");
  273. }
  274. if(results.Count >= 2)
  275. {
  276. Console.WriteLine(results.Count + " items found matching the specified filter.");
  277. }
  278. if(results.Count == 0)
  279. {
  280. Console.WriteLine("No items found matching the specified filter.\n");
  281. return;
  282. }
  283. Console.WriteLine("");
  284. //for progress bar
  285. float UserCountDivided = 50/(float)results.Count; //this number remains constant
  286. float UserCountDividedTemp = UserCountDivided; //this number we increment
  287. dots = 0; //number of dots shown so far on progress bar
  288. UserCountDividedWhole = 0; //number on left side of decimal to increment
  289. UserCountDividedDecimalCount = 0; //number on right side of decimal to increment
  290. UserCountDividedDecimalCountConst = 0; //number on right side of decimal, stays contstant
  291. string UserCountDividedDecimalCountString = null;
  292. string[] UserCountDividedString = UserCountDivided.ToString().Split('.'); //split at the decimal
  293. //calculate the whole and decimal increment counters
  294. UserCountDividedWhole = Convert.ToInt16(UserCountDividedString[0],10); //grab the whole number on the left
  295. if(UserCountDividedString.GetUpperBound(0) == 1)
  296. {
  297. UserCountDividedDecimalCountString = UserCountDividedString[1].Replace(".", "");
  298. UserCountDividedDecimalCountString = "." + UserCountDividedDecimalCountString;
  299. UserCountDividedDecimalCount = float.Parse(UserCountDividedDecimalCountString);
  300. }
  301. else
  302. {
  303. UserCountDividedDecimalCount = 0;
  304. }
  305. UserCountDividedDecimalCountConst = UserCountDividedDecimalCount;
  306. Console.WriteLine("");
  307. Console.WriteLine("0% 50% 100%");
  308. Console.WriteLine("|----|----|----|----|----|----|----|----|----|----|");
  309. Console.Write(".");
  310. dots++;
  311. //increment the user count
  312. usercount++;
  313. ModifyAttributes admod = new ModifyAttributes(); //from ADModify.Library.dll
  314. string UserDN;
  315. //iterate the query results
  316. for(int modusers=0; modusers<=results.Count-1; modusers++)
  317. {
  318. if(dots<=50) //just to be safe, make sure we're under out dots quota so we don't
  319. { //exceed the 100% mark
  320. for(int b=0; b<= UserCountDividedWhole-1; b++)
  321. {
  322. if(dots<=50)
  323. {
  324. Console.Write(".");
  325. dots++;
  326. }
  327. }
  328. if(UserCountDividedDecimalCount>=1)
  329. {
  330. if(dots<=50)
  331. {
  332. UserCountDividedDecimalCount--;
  333. Console.Write(".");
  334. dots++;
  335. }
  336. }
  337. UserCountDividedDecimalCount = UserCountDividedDecimalCount + UserCountDividedDecimalCountConst;
  338. }
  339. if(modusers==results.Count-1 & dots < 51) //sometimes the decimal math doesn't leave us with a
  340. { //51st dot so we have to add it ourselves.
  341. Console.Write(".");
  342. }
  343. if(modusers==results.Count-1) //finish off the progress bar
  344. {
  345. Console.WriteLine("");
  346. Console.WriteLine("");
  347. }
  348. UserDN = results[modusers].Path;
  349. string UserDNWithLDAP = UserDN;
  350. string UserDNWithoutLDAP = UserDNWithLDAP.Remove(0, 7); //trim the LDAP:// off the name in case we need to append servername
  351. //check for servername. If its null we just pass UserDN as UserDNWithLDAP.
  352. //If it has a value we append it and add the LDAP:// back
  353. if(servername==null)
  354. {
  355. UserDN = UserDNWithLDAP;
  356. }
  357. else
  358. {
  359. UserDN = "LDAP://" + servername + "/" + UserDNWithoutLDAP;
  360. }
  361. //test each public variable for a value, if a value other than null exists for string values or
  362. //a true value exists for boolean values then we loop the users through the applicable methods
  363. //Terminal Server Attributes
  364. if(TSProfilePath!=null)
  365. {
  366. admod.TSChangeTermServProfile(logWriter, UserDN, TSProfilePath);
  367. }
  368. if(TSFolderPath!=null & TSHomeDrive==null)
  369. {
  370. admod.TSChangeTermServFolder(logWriter, UserDN, TSFolderPath);
  371. }
  372. if(TSHomeDrive!=null)
  373. {
  374. admod.TSChangeTermServDrive(logWriter, UserDN, TSHomeDrive, TSFolderPath);
  375. }
  376. if(TSEnable)
  377. {
  378. admod.TSEnableLogon(logWriter, UserDN);
  379. }
  380. if(TSDisable)
  381. {
  382. admod.TSDisableLogon(logWriter, UserDN);
  383. }
  384. if(RemoteEnable)
  385. {
  386. admod.TSAllowRemoteControl(logWriter, UserDN, RequirePermission, InteractWithSession);
  387. }
  388. if(RemoteDisable)
  389. {
  390. admod.TSDisableRemoteControl(logWriter, UserDN);
  391. }
  392. if(SetDisconnectTime)
  393. {
  394. admod.TSMaxDisconnectedSessionTime(logWriter, UserDN, DisconnectTime);
  395. }
  396. if(program!=null)
  397. {
  398. admod.TSRunProgramAtStartup(logWriter, UserDN, program);
  399. }
  400. if(startIn!=null)
  401. {
  402. admod.TSRunProgramAtStartupStartIn(logWriter, UserDN, startIn);
  403. }
  404. if(SetMaxConnectionTime)
  405. {
  406. admod.TSMaxConnectionTime(logWriter, UserDN, MaxConnectionTime);
  407. }
  408. if(SetMaxIdleTime)
  409. {
  410. admod.TSMaxIdleTime(logWriter, UserDN, MaxIdleTime);
  411. }
  412. if(SetEndSession)
  413. {
  414. admod.TSEndSessionAction(logWriter, UserDN, EndSession);
  415. }
  416. if(SetOriginatingOnly)
  417. {
  418. admod.TSAllowReconnectAction(logWriter, UserDN, OriginatingOnly);
  419. }
  420. if(SetConnectClientDrives)
  421. {
  422. admod.TSConnectClientDrivesAtLogon(logWriter, UserDN, ConnectClientDrives);
  423. }
  424. if(SetConnectClientPrinters)
  425. {
  426. admod.TSConnectClientPrintersAtLogon(logWriter, UserDN, ConnectClientPrinters);
  427. }
  428. if(SetDefaultToMainPrinter)
  429. {
  430. admod.TSDefaultToMainPrinter(logWriter, UserDN, DefaultToMainPrinter);
  431. }
  432. //Exchange Related Attributes
  433. if(showinaddresslists)
  434. {
  435. admod.ShowInAddressLists(logWriter, UserDN);
  436. }
  437. if(hidefromaddresslists)
  438. {
  439. admod.HideFromAddressLists(logWriter, UserDN);
  440. }
  441. if(killmail)
  442. {
  443. admod.KillMail(logWriter, UserDN);
  444. }
  445. if(includeinrecipientpolicy)
  446. {
  447. admod.IncludeInRecipientPolicy(logWriter, UserDN);
  448. }
  449. if(excludefromrecipientpolicy)
  450. {
  451. admod.ExcludeFromRecipientPolicy(logWriter, UserDN);
  452. }
  453. if(includeinmailboxmanager)
  454. {
  455. admod.IncludeInMailboxManager(logWriter, UserDN);
  456. }
  457. if(excludefrommailboxmanager)
  458. {
  459. admod.ExcludeFromMailboxManager(logWriter, UserDN);
  460. }
  461. if(enablehttp)
  462. {
  463. admod.EnableHTTP(logWriter, UserDN);
  464. }
  465. if(disablehttp)
  466. {
  467. admod.DisableHTTP(logWriter, UserDN);
  468. }
  469. if(enablepop3)
  470. {
  471. admod.EnablePOP3(logWriter, UserDN);
  472. }
  473. if(disablepop3)
  474. {
  475. admod.DisablePOP3(logWriter, UserDN);
  476. }
  477. if(enableimap4)
  478. {
  479. admod.EnableIMAP4(logWriter, UserDN);
  480. }
  481. if(disableimap4)
  482. {
  483. admod.DisableIMAP4(logWriter, UserDN);
  484. }
  485. if(enablemapi)
  486. {
  487. admod.ChangeMAPISettings(logWriter, UserDN, "MAPI§1§0§§§§§§");
  488. }
  489. if(disablemapi)
  490. {
  491. admod.ChangeMAPISettings(logWriter, UserDN, "MAPI§0§0§§§§§§");
  492. }
  493. if(enablecachedmapi)
  494. {
  495. admod.ChangeMAPISettings(logWriter, UserDN, "MAPI§1§1§§§§§§");
  496. }
  497. if(smtpaddress!=null)
  498. {
  499. admod.SetSMTPAddress(logWriter, UserDN, smtpaddress, setasprimary);
  500. }
  501. if(updatemail==true)
  502. {
  503. admod.SetCustomAttribute(logWriter, UserDN, "mail", smtpaddress, false, false);
  504. }
  505. if(removeaddress)
  506. {
  507. if(keepprimary)
  508. {
  509. admod.RemoveEmailAddress(logWriter, UserDN, addresstoremove, true);
  510. }
  511. else
  512. {
  513. admod.RemoveEmailAddress(logWriter, UserDN, addresstoremove, false);
  514. }
  515. }
  516. if(SetMailboxRights)
  517. {
  518. admod.SetmsExchMailboxSecurityDescriptor(logWriter, UserDN);
  519. }
  520. if(GrantSendAsRights)
  521. {
  522. admod.AddSendAs(logWriter, UserDN, Trustee, Deny);
  523. }
  524. if(FixSendAs)
  525. {
  526. admod.GiveSendAsToUsersWithFullMBXAccess(logWriter, UserDN);
  527. }
  528. if(RemoveSendAs)
  529. {
  530. admod.RemoveSendAsFromUsersWithFullMBXAccess(logWriter, UserDN);
  531. }
  532. if(GrantSelfAEA)
  533. {
  534. admod.SetSelfAsAssociatedExternalAccount(logWriter, UserDN);
  535. }
  536. if(RemoveAEAFromSelf)
  537. {
  538. admod.RemoveAssociatedExternalAccountFromSelf(logWriter, UserDN);
  539. }
  540. if(GrantSelfFullAndRead)
  541. {
  542. admod.GrantSelfFullMailboxAccessAndRead(logWriter, UserDN);
  543. }
  544. if(RemoveSelfFullAndRead)
  545. {
  546. admod.RemoveFullMailboxAccessAndReadFromSelf(logWriter, UserDN);
  547. }
  548. if(DumpMailboxRights)
  549. {
  550. admod.DumpMailboxRights(logWriter, DumpWriter, UserDN);
  551. }
  552. if(ModifyMailboxRights)
  553. {
  554. admod.AddACEToMailboxRights(logWriter, UserDN, Trustee, Permission, Deny);
  555. }
  556. if(RemoveMailboxRights)
  557. {
  558. admod.RemoveACEFromMailboxRights(logWriter, UserDN, Trustee, Permission, Deny);
  559. }
  560. //User Account Settings
  561. if(disableaccount)
  562. {
  563. admod.DisableAccount(logWriter, UserDN);
  564. }
  565. if(enableaccount)
  566. {
  567. admod.EnableAccount(logWriter, UserDN);
  568. }
  569. if (SetPasswordNotRequired & PasswordNotRequired)
  570. {
  571. admod.SetPasswordNotRequired(logWriter, UserDN);
  572. }
  573. if (SetPasswordNotRequired & !PasswordNotRequired)
  574. {
  575. admod.DisablePasswordNotRequired(logWriter, UserDN);
  576. }
  577. if(SetPasswordNeverExpires)
  578. {
  579. admod.SetPasswordNeverExpires(logWriter, UserDN, PasswordNeverExpires);
  580. }
  581. if(SetMustChangePassword)
  582. {
  583. admod.MustChangePassword(logWriter, UserDN, MustChangePasswd);
  584. }
  585. if(SetCannotChangePassword)
  586. {
  587. admod.CannotChangePassword(logWriter, UserDN, CannotChangePasswd);
  588. }
  589. if(SetUseReversibleEncryption)
  590. {
  591. admod.StorePasswordWithReversibleEncryption(logWriter, UserDN, UseReversibleEncryption);
  592. }
  593. if(SetRequireSmartCard)
  594. {
  595. admod.SetRequireSmartCard(logWriter, UserDN, RequireSmartCard);
  596. }
  597. if(SetCannotBeDelegated)
  598. {
  599. admod.SetCannotBeDelegated(logWriter, UserDN, CannotBeDelegated);
  600. }
  601. if(SetUseDESEncryption)
  602. {
  603. admod.SetUseDESEncryption(logWriter, UserDN, UseDESEncryption);
  604. }
  605. if(SetDoNotRequireKerberosPreAuth)
  606. {
  607. admod.SetDoNotRequireKerberosPreAuth(logWriter, UserDN, DoNotRequireKerberosPreAuth);
  608. }
  609. if(AddMembersToGroup)
  610. {
  611. admod.AddToGroup(logWriter, UserDN, DNOfGroup);
  612. }
  613. if(RemoveMembersFromGroup)
  614. {
  615. admod.RemoveFromGroup(logWriter, UserDN, DNOfGroup);
  616. }
  617. if(ModifyRDN)
  618. {
  619. admod.SetRelativeDistinguishedName(logWriter, UserDN, NewRDN);
  620. }
  621. //Permissions Settings
  622. if(Setallowinheritable)
  623. {
  624. admod.AllowInheritablePermissions(logWriter, UserDN, AllowInheritable);
  625. }
  626. //custom attribute set
  627. if(customAttributeName!=null)
  628. {
  629. admod.SetCustomAttribute(logWriter, UserDN, customAttributeName, customAttributeValue, multi, remove);
  630. }
  631. }
  632. //finish up
  633. if(DumpMailboxRights)
  634. {
  635. DumpWriter.WriteEndElement();
  636. DumpWriter.Flush();
  637. DumpWriter.Close();
  638. }
  639. log.showResults();
  640. src.Dispose();
  641. return;
  642. }
  643. /// <summary>
  644. /// ADModifyMain::GiveCommandArgs
  645. ///
  646. /// Accepts:
  647. /// none
  648. /// Returns:
  649. /// none
  650. ///
  651. /// Displays command line usage back to the console.
  652. /// </summary>
  653. static void GiveCommandArgs()
  654. {
  655. Console.WriteLine("");
  656. Console.WriteLine("Active Directory Bulk Modify Tool Command Line Version 2.1");
  657. Console.WriteLine("");
  658. Console.WriteLine("Questions or Comments? ");
  659. Console.WriteLine("Email: admodify@microsoft.com");
  660. Console.WriteLine("");
  661. Console.WriteLine("ADModCmd runs an LDAP query against the specified AD container. The ");
  662. Console.WriteLine("modifications are then performed against the results of the query.");
  663. Console.WriteLine("");
  664. Console.WriteLine("Usage:");
  665. Console.WriteLine("");
  666. Console.WriteLine("admodcmd [-dn BaseDN] [-p pagesize] [-s] [-server servername]");
  667. Console.WriteLine(" [-f LDAPFilter] [modification]");
  668. Console.WriteLine("");
  669. Console.WriteLine("");
  670. Console.WriteLine("Required:");
  671. Console.WriteLine("");
  672. Console.WriteLine("-dn BaseDN Base DN to begin the LDAP query.");
  673. Console.WriteLine("");
  674. Console.WriteLine("modification Modification to perform. ");
  675. Console.WriteLine(" See below for details.");
  676. Console.WriteLine("");
  677. Console.WriteLine("Optional:");
  678. Console.WriteLine("");
  679. Console.WriteLine("-p pagesize LDAP Page size to use for query.");
  680. Console.WriteLine("");
  681. Console.WriteLine("-s Denotes a subtree search ");
  682. Console.WriteLine(" (If -s is not specified, search ");
  683. Console.WriteLine(" defaults to onelevel).");
  684. Console.WriteLine("");
  685. Console.WriteLine("-server servername Denotes the server to make the changes ");
  686. Console.WriteLine(" to (If -server is not specified, ");
  687. Console.WriteLine(" changes are made locally if on a DC. ");
  688. Console.WriteLine(" If on a member, DNS is used to ");
  689. Console.WriteLine(" find a DC).");
  690. Console.WriteLine("");
  691. Console.WriteLine("-f LDAPFilter A Valid LDAP filter to use when ");
  692. Console.WriteLine(" enumerating objects (If -f is not ");
  693. Console.WriteLine(" specified, (objectClass=user) is");
  694. Console.WriteLine(" the default filter).");
  695. Console.WriteLine("");
  696. Console.WriteLine("");
  697. Console.WriteLine("Modification Switches:");
  698. Console.WriteLine("");
  699. Console.WriteLine("");
  700. Console.WriteLine("NOTE: Using the word \"null\" (without quotes) as an attribute value will ");
  701. Console.WriteLine(" clear the attribute.");
  702. Console.WriteLine("");
  703. Console.WriteLine("");
  704. Console.WriteLine("============================");
  705. Console.WriteLine("|Terminal Server Attributes|");
  706. Console.WriteLine("============================");
  707. Console.WriteLine("");
  708. Console.WriteLine("Windows 2003 or later is required to modify Terminal Server attributes.");
  709. Console.WriteLine("");
  710. Console.WriteLine("-tsprofilepath ProfilePath Sets the users Terminal Server ");
  711. Console.WriteLine(" Profile path to the specified value.");
  712. Console.WriteLine("");
  713. Console.WriteLine("-tshomefolderpath FolderPath Sets the users Terminal Server home ");
  714. Console.WriteLine(" folder path to the specified value.");
  715. Console.WriteLine("");
  716. Console.WriteLine("-tsnetworkfolderpath driveLetter FolderPath ");
  717. Console.WriteLine(" Sets the users Terminal Server home");
  718. Console.WriteLine(" folder path to a network share.");
  719. Console.WriteLine(" driveLetter should be the drive that");
  720. Console.WriteLine(" FolderPath will be mapped to.");
  721. Console.WriteLine("");
  722. Console.WriteLine("-tsenable Enables the user for Terminal Server.");
  723. Console.WriteLine("");
  724. Console.WriteLine("-tsdisable Disables the user from using Terminal");
  725. Console.WriteLine(" Server.");
  726. Console.WriteLine("");
  727. Console.WriteLine("-enableremote -required -interact Enables Remote Control for the user.");
  728. Console.WriteLine(" The -required and -interact switches");
  729. Console.WriteLine(" are optional. If required is used,");
  730. Console.WriteLine(" \"Require Users Permission\" will be");
  731. Console.WriteLine(" checked. If -interact is specified, ");
  732. Console.WriteLine(" then \"Interact With The Session\" will");
  733. Console.WriteLine(" be checked.");
  734. Console.WriteLine("");
  735. Console.WriteLine("-disableremote Disables Remote Control for the user.");
  736. Console.WriteLine("");
  737. Console.WriteLine("-tsstartingprogram program Sets the program to start when the user");
  738. Console.WriteLine(" logs on to Terminal Server. ");
  739. Console.WriteLine("");
  740. Console.WriteLine("-tsstartin location Sets the location for the users startup");
  741. Console.WriteLine(" program to start in.");
  742. Console.WriteLine("");
  743. Console.WriteLine("-maxdisconnectedsession minutes");
  744. Console.WriteLine(" Sets the maximum disconnected session");
  745. Console.WriteLine(" time for the user in minutes. Setting");
  746. Console.WriteLine(" to 0 minutes indicates an unlimited");
  747. Console.WriteLine(" time.");
  748. Console.WriteLine("");
  749. Console.WriteLine("-maxconnectiontime minutes Sets the maximum connection time for ");
  750. Console.WriteLine(" the user in minutes. Setting to 0");
  751. Console.WriteLine(" indicates an unlimited time.");
  752. Console.WriteLine("");
  753. Console.WriteLine("-maxidletime minutes Sets the maximum idle time for the");
  754. Console.WriteLine(" user in minutes. Setting to 0 ");
  755. Console.WriteLine(" indicates an unlimited time. ");
  756. Console.WriteLine("");
  757. Console.WriteLine("-sessionlimitaction disconnect|end Specifies the action to take when ");
  758. Console.WriteLine(" a sessions limit has been reached.");
  759. Console.WriteLine(" Specify either disconnect (disconnect");
  760. Console.WriteLine(" the session) or end (end the session).");
  761. Console.WriteLine("");
  762. Console.WriteLine("-allowreconnect any|originating Specifies whether or not to allow a");
  763. Console.WriteLine(" reconnect from anywhere (any) or just");
  764. Console.WriteLine(" the originating client (originating).");
  765. Console.WriteLine("");
  766. Console.WriteLine("-tsconnectclientdrives yes|no Specifies whether or not to connect");
  767. Console.WriteLine(" client drives upon logon to a Terminal");
  768. Console.WriteLine(" Server session.");
  769. Console.WriteLine("");
  770. Console.WriteLine("-tsconnectclientprinters yes|no Specifies whether or not to connect");
  771. Console.WriteLine(" client printers upon logon to a ");
  772. Console.WriteLine(" Terminal Server session.");
  773. Console.WriteLine("");
  774. Console.WriteLine("-tsdefaulttomainprinter yes|no Specifies whether or not to default");
  775. Console.WriteLine(" to the main client printer.");
  776. Console.WriteLine("");
  777. Console.WriteLine("");
  778. Console.WriteLine("=============================");
  779. Console.WriteLine("|Exchange Related Attributes|");
  780. Console.WriteLine("=============================");
  781. Console.WriteLine("");
  782. Console.WriteLine("-killmail Removes Exchange Attributes.");
  783. Console.WriteLine("");
  784. Console.WriteLine("-includeinrecipientpolicy Checks \"Automatically Update E-mail");
  785. Console.WriteLine(" Addresses Based on Recipient Policy\"");
  786. Console.WriteLine(" Check box.");
  787. Console.WriteLine("");
  788. Console.WriteLine("-excludefromrecipientpolicy Unchecks \"Automatically Update E-mail");
  789. Console.WriteLine(" Addresses Based on Recipient Policy\"");
  790. Console.WriteLine(" Check box.");
  791. Console.WriteLine("");
  792. Console.WriteLine("-hidefromaddresslists Hides the user from all Address Lists.");
  793. Console.WriteLine("");
  794. Console.WriteLine("-showinaddresslists Shows the user in address lists.");
  795. Console.WriteLine("");
  796. Console.WriteLine("-includeinmailboxmanager Includes the user in mailbox manager");
  797. Console.WriteLine(" policies.");
  798. Console.WriteLine("");
  799. Console.WriteLine("-excludefrommailboxmanager Excludes the user from mailbox manager");
  800. Console.WriteLine(" policies.");
  801. Console.WriteLine("");
  802. Console.WriteLine("-enablehttp Enables HTTP for the user.");
  803. Console.WriteLine("");
  804. Console.WriteLine("-disablehttp Disables HTTP for the user.");
  805. Console.WriteLine("");
  806. Console.WriteLine("-enablepop3 Enables POP3 for the user.");
  807. Console.WriteLine("");
  808. Console.WriteLine("-disablepop3 Disables POP3 for the user.");
  809. Console.WriteLine("");
  810. Console.WriteLine("-enableimap4 Enables IMAP4 for the user.");
  811. Console.WriteLine("");
  812. Console.WriteLine("-disableimap4 Disables IMAP4 for the user.");
  813. Console.WriteLine("");
  814. Console.WriteLine("The three MAPI settings below require that the users mailbox");
  815. Console.WriteLine("is homed on an Exchange 2003 SP2 or later server.");
  816. Console.WriteLine("");
  817. Console.WriteLine("-enablemapi Enables MAPI access for the user.");
  818. Console.WriteLine("");
  819. Console.WriteLine("-disablemapi Disables MAPI access for the user.");
  820. Console.WriteLine("");
  821. Console.WriteLine("-enablecachedmapi Enables MAPI access for users in");
  822. Console.WriteLine(" cached mode only.");
  823. Console.WriteLine("");
  824. Console.WriteLine("-addsmtp address -setasprimary -updatemail");
  825. Console.WriteLine(" Adds an SMTP address to the user.");
  826. Console.WriteLine(" Address will need to be in the form");
  827. Console.WriteLine(" of variable@domain.com (Information");
  828. Console.WriteLine(" on variable usage can be found below");
  829. Console.WriteLine(" under \"Variables\"). Specify the ");
  830. Console.WriteLine(" optional setasprimary switch to set ");
  831. Console.WriteLine(" the address as primary (default ");
  832. Console.WriteLine(" is secondary). Use the -updatemail");
  833. Console.WriteLine(" switch to also set the E-mail address");
  834. Console.WriteLine(" on the ADU&C General tab.");
  835. Console.WriteLine("");
  836. Console.WriteLine("-removeaddress address Removes any address that matches the");
  837. Console.WriteLine(" specified filter. Wild cards are");
  838. Console.WriteLine(" allowed. Examples would be:\n");
  839. Console.WriteLine(" smtp:*@domain.com");
  840. Console.WriteLine(" smtp:*@doma??.com\n");
  841. Console.WriteLine(" Normal * and ? wildcard matching");
  842. Console.WriteLine(" rules apply. Please note that this");
  843. Console.WriteLine(" can be used to remove any match found");
  844. Console.WriteLine(" in the users Proxy Addresses list,");
  845. Console.WriteLine(" not just SMTP addresses. Matches are");
  846. Console.WriteLine(" case-insensitive.");
  847. Console.WriteLine("");
  848. Console.WriteLine("");
  849. Console.WriteLine("----Mailbox Rights----");
  850. Console.WriteLine("");
  851. Console.WriteLine("-setmailboxrights Sets msExchMailboxSecurityDescriptor.");
  852. Console.WriteLine(" KB 324353");
  853. Console.WriteLine("");
  854. Console.WriteLine("-fixsendas Grants Send-As rights to users with");
  855. Console.WriteLine(" Full Mailbox Access and Read.");
  856. Console.WriteLine(" KB 327274");
  857. Console.WriteLine("");
  858. Console.WriteLine("-removefixsendas Removes Send-As rights from users");
  859. Console.WriteLine(" with Full Mailbox Access and Read.");
  860. Console.WriteLine(" Undo for KB 327274");
  861. Console.WriteLine("");
  862. Console.WriteLine("-grantselfaea Grants Associated External Account");
  863. Console.WriteLine(" privileges to SELF. KB 278966");
  864. Console.WriteLine("");
  865. Console.WriteLine("-removeaeafromself Removes Associated External Account");
  866. Console.WriteLine(" from SELF. Undo for KB 278966");
  867. Console.WriteLine("");
  868. Console.WriteLine("-grantselffullandread Grants Full Mailbox Access and Read");
  869. Console.WriteLine(" to SELF. KB 304935");
  870. Console.WriteLine("");
  871. Console.WriteLine("-removefullandread Removes Full Mailbox Access and Read");
  872. Console.WriteLine(" from SELF. Undo for KB 304935");
  873. Console.WriteLine("");
  874. Console.WriteLine("-dumpmailboxrights Dumps all permissions in Mailbox Rights");
  875. Console.WriteLine(" to a file called mbxrights.xml.");
  876. Console.WriteLine("");
  877. Console.WriteLine("-importmailboxrights overwrite|append");
  878. Console.WriteLine(" Imports mailbox rights from the");
  879. Console.WriteLine(" mbxrights.xml created by the");
  880. Console.WriteLine(" -dumpmailboxrights switch. When using");
  881. Console.WriteLine(" this switch, the -dn switch is not");
  882. Console.WriteLine(" required. Overwrite will remove all");
  883. Console.WriteLine(" non-inherited ACE's before importing,");
  884. Console.WriteLine(" append will not. This switch only");
  885. Console.WriteLine(" imports non-inherited mailbox");
  886. Console.WriteLine(" rights.");
  887. Console.WriteLine("");
  888. Console.WriteLine("-addtomailboxrights DOMAIN\\USER ACCESS_MASK -deny (Optional)");
  889. Console.WriteLine(" Adds an account to mailbox rights with");
  890. Console.WriteLine(" the specified permissions masks. The ");
  891. Console.WriteLine(" optional -deny switch specfies a deny");
  892. Console.WriteLine(" entry, default is an allow entry.");
  893. Console.WriteLine(" Valid Access Masks:");
  894. Console.WriteLine("");
  895. Console.WriteLine(" ACE_MB_FULL_ACCESS");
  896. Console.WriteLine(" ACE_MB_DELETE_MB_STORAGE");
  897. Console.WriteLine(" ACE_MB_READ_PERMISSIONS");
  898. Console.WriteLine(" ACE_MB_CHANGE_PERMISSION");
  899. Console.WriteLine(" ACE_MB_TAKE_OWNERSHIP");
  900. Console.WriteLine("");
  901. Console.WriteLine("-removefrommailboxrights DOMAIN\\USER ACCESS_MASK -deny (optional)");
  902. Console.WriteLine(" Removes the specified mask from");
  903. Console.WriteLine(" DOMAIN\\USER in mailbox rights. The");
  904. Console.WriteLine(" -deny switch specifies a deny entry,");
  905. Console.WriteLine(" default is an allow entry.");
  906. Console.WriteLine(" Valid Access Masks:");
  907. Console.WriteLine("");
  908. Console.WriteLine(" ACE_MB_FULL_ACCESS");
  909. Console.WriteLine(" ACE_MB_DELETE_MB_STORAGE");
  910. Console.WriteLine(" ACE_MB_READ_PERMISSIONS");
  911. Console.WriteLine(" ACE_MB_CHANGE_PERMISSION");
  912. Console.WriteLine(" ACE_MB_TAKE_OWNERSHIP");
  913. Console.WriteLine(" ALL");
  914. Console.WriteLine("");
  915. Console.WriteLine(" The ALL Mask will remove all permissions");
  916. Console.WriteLine(" for the specified user.");
  917. Console.WriteLine("");
  918. Console.WriteLine("");
  919. Console.WriteLine("=======================");
  920. Console.WriteLine("|User Account Settings|");
  921. Console.WriteLine("=======================");
  922. Console.WriteLine("");
  923. Console.WriteLine("-enableaccount Enables the user account.");
  924. Console.WriteLine("");
  925. Console.WriteLine("-disableaccount Disables the user account.");
  926. Console.WriteLine("");
  927. Console.WriteLine("-passwordnotrequired yes|no Specifies whether to set the password");
  928. Console.WriteLine(" not required option.");
  929. Console.WriteLine("");
  930. Console.WriteLine("-passwordneverexpires yes|no Specifies whether to check or uncheck");
  931. Console.WriteLine(" the \"Password Never Expires\" box.");
  932. Console.WriteLine("");
  933. Console.WriteLine("-mustchangepassword yes|no Specifies whether to check or uncheck");
  934. Console.WriteLine(" the \"User Must Change Password at Next");
  935. Console.WriteLine(" Logon\" box.");
  936. Console.WriteLine("");
  937. Console.WriteLine("-cannotchangepassword yes|no Specifies whether to check or uncheck");
  938. Console.WriteLine(" the \"User Cannot Change Password\" box.");
  939. Console.WriteLine("");
  940. Console.WriteLine("-usereversibleencryption yes|no Specifies whether to check or uncheck");
  941. Console.WriteLine(" the \"Store Password Using Reversible ");
  942. Console.WriteLine(" Encryption\" box.");
  943. Console.WriteLine("");
  944. Console.WriteLine("-smartcardrequired yes|no Specifies whether to check or uncheck");
  945. Console.WriteLine(" the \"Smart Card is Required for ");
  946. Console.WriteLine(" Interactive Logon\" box.");
  947. Console.WriteLine("");
  948. Console.WriteLine("-cannotbedelegated yes|no Specifies whether to check or uncheck");
  949. Console.WriteLine(" the \"Account is Sensitive and Cannot");
  950. Console.WriteLine(" be Delegated\" box.");
  951. Console.WriteLine("");
  952. Console.WriteLine("-usedesencryption yes|no Specifies whether to check or uncheck");
  953. Console.WriteLine(" the \"Use DES Encryption Types for This");
  954. Console.WriteLine(" Account\" box.");
  955. Console.WriteLine("");
  956. Console.WriteLine("-donotrequirekerberospreauth yes|no Specifies whether to check or uncheck");
  957. Console.WriteLine(" the \"Do Not Require Kerberos ");
  958. Console.WriteLine(" Preauthentication\" box.");
  959. Console.WriteLine("");
  960. Console.WriteLine("-addtogroup DNOfGroup Adds the user to the specified group.");
  961. Console.WriteLine("");
  962. Console.WriteLine("-removefromgroup DNOfGroup Removes the user from the specified");
  963. Console.WriteLine(" group.");
  964. Console.WriteLine("");
  965. Console.WriteLine("-allowinheritable yes|no Specifies whether to check or uncheck");
  966. Console.WriteLine(" the \"Allow inheritable permissions to");
  967. Console.WriteLine(" propagate to this object\" box.");
  968. Console.WriteLine("");
  969. Console.WriteLine("-modrdn NewRDN Changes the users Relative Distinguished");
  970. Console.WriteLine(" Name (CN). Variable usage is required.");
  971. Console.WriteLine(" To change an RDN to LastName, FirstName");
  972. Console.WriteLine(" use the following syntax:");
  973. Console.WriteLine(" -modrdn \"%'sn'%, %'givenName'%\"");
  974. Console.WriteLine("");
  975. Console.WriteLine("===================");
  976. Console.WriteLine("|Custom Attributes|");
  977. Console.WriteLine("===================");
  978. Console.WriteLine("");
  979. Console.WriteLine("The -custom switch allows you to name the attribute you wish to modify. This");
  980. Console.WriteLine("operation is only supported against attributes of the following type:");
  981. Console.WriteLine("");
  982. Console.WriteLine("Boolean");
  983. Console.WriteLine("Case Insensitive String");
  984. Console.WriteLine("Distinguished Name");
  985. Console.WriteLine("DN Binary");
  986. Console.WriteLine("IA5-String");
  987. Console.WriteLine("Integer");
  988. Console.WriteLine("Numerical String");
  989. Console.WriteLine("Unicode String");
  990. Console.WriteLine("");
  991. Console.WriteLine("Syntax: ");
  992. Console.WriteLine("[-custom attributeName attributeValue -multi|-remove]");
  993. Console.WriteLine("");
  994. Console.WriteLine("attributeName The name of the attribute you wish");
  995. Console.WriteLine(" to modify.");
  996. Console.WriteLine("");
  997. Console.WriteLine("attributeValue The value to give the attribute.");
  998. Console.WriteLine("");
  999. Console.WriteLine("-multi (optional) Specifies a multi-valued append. If");
  1000. Console.WriteLine(" the attribute being modified is multi-");
  1001. Console.WriteLine(" valued this switch needs to be used. ");
  1002. Console.WriteLine(" Otherwise, the value will be ");
  1003. Console.WriteLine(" overwritten, not appended.");
  1004. Console.WriteLine("");
  1005. Console.WriteLine("-remove (optional) Specifies a multi-valued remove. If");
  1006. Console.WriteLine(" the attribute being modified is multi-");
  1007. Console.WriteLine(" valued and -remove is used, then only");
  1008. Console.WriteLine(" the specified value will get removed.");
  1009. Console.WriteLine("");
  1010. Console.WriteLine("Example for setting description attribute:");
  1011. Console.WriteLine("");
  1012. Console.WriteLine("-custom description \"IT Department\"");
  1013. Console.WriteLine("");
  1014. Console.WriteLine("");
  1015. Console.WriteLine("==========");
  1016. Console.WriteLine("|Variables|");
  1017. Console.WriteLine("==========");
  1018. Console.WriteLine("");
  1019. Console.WriteLine("Variable usage is allowed when building attributes. Variables can be based ");
  1020. Console.WriteLine("off of almost any current Active Directory attribute, as long as it has a ");
  1021. Console.WriteLine("value. Variables are seperated from literal values using the % sign.");
  1022. Console.WriteLine("Variables must also be enclosed in a single tick (').");
  1023. Console.WriteLine("");
  1024. Console.WriteLine("This example shows how to set homeDirectory to the path c:\\test\\username, ");
  1025. Console.WriteLine("where username is the users sAMAccountName:");
  1026. Console.WriteLine("");
  1027. Console.WriteLine("-custom homeDirectory c:\\test\\%'sAMAccountName'%");
  1028. Console.WriteLine("");
  1029. Console.WriteLine("It is also possible to pull only a specified number of characters from the ");
  1030. Console.WriteLine("attribute as well. Just specify the number of characters you want to use");
  1031. Console.WriteLine("after the %, and before the \"'\". The following example shows how to add ");
  1032. Console.WriteLine("an SMTP address of FirstInitial.LastName@domain.com:");
  1033. Console.WriteLine("");
  1034. Console.WriteLine("-addsmtp %1'givenName'%.%'sn'%@domain.com");
  1035. Console.WriteLine("");
  1036. Console.WriteLine("If a % or ' is needed as a literal value, simply use the forward slash (/)");
  1037. Console.WriteLine("as an escape character:");
  1038. Console.WriteLine("");
  1039. Console.WriteLine("Example:");
  1040. Console.WriteLine("This is a percent sign: /%");
  1041. Console.WriteLine("");
  1042. Console.WriteLine("The above line will end up being: This is a percent sign: %");
  1043. Console.WriteLine("");
  1044. Console.WriteLine("There may be times where you need a literal forward slash before the");
  1045. Console.WriteLine("percentage or single tick characters, usually when the forward slash");
  1046. Console.WriteLine("needs to be followed by a variable. The syntax for this is //% or //'");
  1047. Console.WriteLine("An example would be adding an MS Mail address:");
  1048. Console.WriteLine("");
  1049. Console.WriteLine("ms:PO/SERVER//%'mailNickName'");
  1050. Console.WriteLine("");
  1051. Console.WriteLine("===========");
  1052. Console.WriteLine("|Undo Mode|");
  1053. Console.WriteLine("===========");
  1054. Console.WriteLine("");
  1055. Console.WriteLine("Changes made with ADModify can be undone, as long as the xml log file that");
  1056. Console.WriteLine("logged the changes still exists. These log files are typically located in");
  1057. Console.WriteLine("the same folder as the admodify executable.");
  1058. Console.WriteLine("");
  1059. Console.WriteLine("Syntax:");
  1060. Console.WriteLine("[-undo logfilename -server servername]");
  1061. Console.WriteLine("");
  1062. Console.WriteLine("-undo logfilename Specifes the log file that contains the");
  1063. Console.WriteLine(" changes to be undone.");
  1064. Console.WriteLine("");
  1065. Console.WriteLine("-server servername (optional) Specifies the DC to write the changes");
  1066. Console.WriteLine(" to. If left blank changes are written");
  1067. Console.WriteLine(" locally if the local machine is a DC.");
  1068. Console.WriteLine(" If not, DNS is used to find one.");
  1069. Console.WriteLine("");
  1070. Console.WriteLine("For information on users that were skipped during an undo process, refer");
  1071. Console.WriteLine("to the undo.log file.");
  1072. Console.WriteLine("");
  1073. Console.WriteLine("For more information on sample usage, please refer to the ADModify help.");
  1074. Console.WriteLine("");
  1075. Console.WriteLine("");
  1076. }
  1077. /// <summary>
  1078. /// ADModifyMain::Main
  1079. ///
  1080. /// Accepts:
  1081. /// args - command line arguments passed by the user
  1082. /// Returns:
  1083. /// nothing
  1084. ///
  1085. /// Here we accept all command line arguments and set the global variables for these
  1086. /// arguments accordingly. Control is then given to the GetDN function.
  1087. /// </summary>
  1088. [STAThread]
  1089. static void Main(string[] args)
  1090. {
  1091. LogFile log = new LogFile();
  1092. DateTime endTime;
  1093. TimeSpan finalCount;
  1094. string RawDateTime = System.DateTime.Now.ToString();
  1095. RawDateTime = RawDateTime.Replace(" ","").Replace("/","").Replace(":","");
  1096. string logFileName = RawDateTime + ".xml";
  1097. XmlTextWriter logWriter = new XmlTextWriter(logFileName, null);
  1098. logWriter.WriteStartDocument(false);
  1099. logWriter.WriteDocType("LogFile", null, null, null);
  1100. logWriter.WriteStartElement("XmlRoot", logFileName);
  1101. DateTime startTime = System.DateTime.Now;
  1102. bool haveDN = false; //flip this bit when we have a base DN passed
  1103. int numberargs = args.Length;
  1104. string LDAPFilter = "(objectClass=user)"; //default to this in case a filter is not specified
  1105. bool subtree = false; //default to a onelevel search
  1106. bool bitflipped = false; //if any valid attribute switch is given we flip this bit
  1107. string SearchBase = "blank";
  1108. //loop through args and set the necesarry information
  1109. for(int i=0; i < numberargs; i++)
  1110. {
  1111. switch(args[i].ToLower())
  1112. {
  1113. case "/?":
  1114. case "-?":
  1115. bitflipped = true;
  1116. GiveCommandArgs();
  1117. return;
  1118. case "-server":
  1119. try
  1120. {
  1121. servername = args[i+1];
  1122. i++;
  1123. }
  1124. catch(System.IndexOutOfRangeException)
  1125. {
  1126. Console.WriteLine("Please enter a server name after the -servername switch.");
  1127. return;
  1128. }
  1129. break;
  1130. case "-f":
  1131. try
  1132. {
  1133. LDAPFilter = args[i+1];
  1134. i++;
  1135. }
  1136. catch(System.IndexOutOfRangeException)
  1137. {
  1138. Console.WriteLine("Please enter an LDAP filter after the -filter switch.");
  1139. return;
  1140. }
  1141. break;
  1142. case "-p":
  1143. try
  1144. {
  1145. pageSize = Convert.ToInt32(args[i+1],10);
  1146. if(pageSize>1000 || pageSize==0)
  1147. {
  1148. Console.WriteLine("Please enter …

Large files files are truncated, but you can click here to view the full file