PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/SanitizeExport/SanitizeExport/Program.cs

#
C# | 215 lines | 199 code | 16 blank | 0 comment | 17 complexity | dbf374bda3eb087eb77ccd74041799cb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Security;
  8. using System.IO;
  9. using System.Reflection;
  10. using Microsoft.SystemCenter.Orchestrator.Integration.Administration;
  11. using Microsoft.SystemCenter.Orchestrator.Integration.Administration.OIS_Export;
  12. namespace SanitizeExport
  13. {
  14. class Program
  15. {
  16. #region Gobals
  17. private static XmlDocument finalDocument = new XmlDocument();
  18. private static XmlDocument inputXML = new XmlDocument();
  19. private static XmlNode finalConfigNode;
  20. private static XmlNode finalCountersNode;
  21. private static XmlNode finalVariablesNode;
  22. private static XmlNode finalSchedulesNode;
  23. private static XmlNode finalComputerGroupsNode;
  24. private static string configurationNodePath = @"//GlobalConfigurations";
  25. private static string countersNodePath = @"//GlobalSettings/Counters/Folder";
  26. private static string variablesNodePath = @"//GlobalSettings/Variables/Folder";
  27. private static string schedulesNodePath = @"//GlobalSettings/Schedules/Folder";
  28. private static string computerGroupsNodePath = @"//GlobalSettings/ComputerGroups/Folder";
  29. private static string policiesFolderPath = @"//Policies/Folder";
  30. private static string CommonData = "LogCommonData";
  31. private static string SpecificData = "LogSpecificData";
  32. private static bool verbose = false;
  33. #endregion
  34. static void Main(string[] args)
  35. {
  36. FileInfo inputFile = null;
  37. FileInfo outputFile = null;
  38. bool sanitizeGlobals = true;
  39. bool applyLinkBestPractices = true;
  40. bool updateMaxParallel = true;
  41. String ObjectSpecificLogging = String.Empty;
  42. String GenericObjectLogging = String.Empty;
  43. if (parseInputs(args, ref inputFile, ref outputFile, ref sanitizeGlobals, ref ObjectSpecificLogging, ref GenericObjectLogging, ref applyLinkBestPractices, ref updateMaxParallel))
  44. {
  45. ExportFile export = new ExportFile(inputFile);
  46. if (sanitizeGlobals)
  47. {
  48. export.cleanGlobalConfigurations();
  49. export.cleanGlobalComputerGroupsNode();
  50. export.cleanGlobalCountersNode();
  51. export.cleanGlobalSchedulesNode();
  52. export.cleanGlobalVariablesNode();
  53. }
  54. if (applyLinkBestPractices) { export.modifyExportLinkApplyBestPractices(); }
  55. if (updateMaxParallel) { export.modifyExportSetMaxParallelRequestSettingNameBased(); }
  56. export.modifyGenericLogging(GenericObjectLogging);
  57. export.modifyObjectSpecificLogging(ObjectSpecificLogging);
  58. export.OISExport.Save(outputFile.FullName);
  59. }
  60. }
  61. #region Input Parameter Parsing and Validation
  62. private static bool parseInputs(string[] args, ref FileInfo inputFile, ref FileInfo outputFile, ref bool sanitizeGlobals, ref String ObjectSpecificLogging, ref String GenericObjectLogging, ref bool applyLinkBestPractices, ref bool updateMaxParallel)
  63. {
  64. bool force = false;
  65. if (args.Count() == 0 || args.Contains("-help") || args.Contains("/?"))
  66. {
  67. printHelp();
  68. return false;
  69. }
  70. else
  71. {
  72. for (int i = 0; i < args.Count(); i++)
  73. {
  74. parseInput(args, ref inputFile, ref outputFile, ref force, ref sanitizeGlobals, ref ObjectSpecificLogging, ref GenericObjectLogging, ref i, ref applyLinkBestPractices, ref updateMaxParallel);
  75. }
  76. return verifyMandatoryParameters(inputFile, outputFile, force);
  77. }
  78. }
  79. private static void printHelp()
  80. {
  81. Console.WriteLine("SanitizeExport.exe -ExportFilePath <String> -SanitizedExportFilePath <String> [-ObjectSpecificLogging (On|Off)] [-GenericObjectLogging (On|Off)] [-DoNotSanitizeGlobals] [-Force]\n\n");
  82. Console.WriteLine("-ExportFilePath <String> : Path to the ois_export file to sanitize");
  83. Console.WriteLine("-SanitizedExportFilePath <String> : Path to save the sanitized export file to");
  84. Console.WriteLine("-ObjectSpecificLogging (On|Off) : Turns On or Off object specific logging for all runbooks in export file");
  85. Console.WriteLine("-GenericObjectLogging (On|Off) : Turns On or Off generic object logging for all runbooks in export file");
  86. Console.WriteLine("-ApplyLinkBestPractices (True|False) : Applys link best practice naming and coloring");
  87. Console.WriteLine("-UpdateMaxParallel (True|False) : Updates the max concurrent settings for Runbooks based on folder names");
  88. Console.WriteLine("-DoNotSanitizeGlobals : If set no manipulations of Globals (configurations, variables, counters etc) will be done");
  89. Console.WriteLine("-Force : If set any file at SanitizedExportFilePath will be overwritten");
  90. Console.WriteLine("-Verbose : Prints detailed information on what the tool is doing");
  91. Console.WriteLine("-Help : Prints this Help Page");
  92. }
  93. private static void parseInput(string[] args, ref FileInfo inputFile, ref FileInfo outputFile, ref bool force, ref bool sanitizeGlobals, ref String ObjectSpecificLogging, ref String GenericObjectLogging, ref int i, ref bool applyLinkBestPractices, ref bool updateMaxParallel)
  94. {
  95. switch (args[i].ToLower())
  96. {
  97. case "-applylinkbestpractices":
  98. case "-a":
  99. i++;
  100. applyLinkBestPractices = Convert.ToBoolean(args[i]);
  101. break;
  102. case "-updatemaxparallel":
  103. case "-u":
  104. i++;
  105. updateMaxParallel = Convert.ToBoolean(args[i]);
  106. break;
  107. case "-exportfilepath":
  108. i++;
  109. inputFile = new FileInfo(args[i]);
  110. break;
  111. case "-e":
  112. i++;
  113. inputFile = new FileInfo(args[i]);
  114. break;
  115. case "-sanitizedexportfilepath":
  116. i++;
  117. outputFile = new FileInfo(args[i]);
  118. break;
  119. case "-s":
  120. i++;
  121. outputFile = new FileInfo(args[i]);
  122. break;
  123. case "-objectspecificlogging":
  124. i++;
  125. ObjectSpecificLogging = args[i];
  126. break;
  127. case "-o":
  128. i++;
  129. ObjectSpecificLogging = args[i];
  130. break;
  131. case "-genericobjectlogging":
  132. i++;
  133. GenericObjectLogging = args[i];
  134. break;
  135. case "-g":
  136. i++;
  137. GenericObjectLogging = args[i];
  138. break;
  139. case "-donotsanitizeglobals":
  140. sanitizeGlobals = false;
  141. break;
  142. case "-d":
  143. sanitizeGlobals = false;
  144. break;
  145. case "-force":
  146. force = true;
  147. break;
  148. case "-f":
  149. force = true;
  150. break;
  151. case "-verbose":
  152. verbose = true;
  153. break;
  154. case "-v":
  155. verbose = true;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. private static bool verifyMandatoryParameters(FileInfo inputFile, FileInfo outputFile, bool force)
  162. {
  163. if (inputFile == null)
  164. {
  165. Console.WriteLine("Please specifiy an export file to sanitize using -ExportFilePath \"\\\\path\\to\\exportfile.ois_export\"");
  166. return false;
  167. }
  168. else
  169. {
  170. if(!inputFile.Exists)
  171. {
  172. return false;
  173. }
  174. }
  175. if (outputFile == null)
  176. {
  177. Console.WriteLine("Please specifiy a path to store the output file using -SanitizedExportFilePath \"\\\\path\\to\\exportfile.ois_export\"");
  178. return false;
  179. }
  180. else
  181. {
  182. if (outputFile.Exists)
  183. {
  184. if (force)
  185. {
  186. outputFile.Delete();
  187. }
  188. else
  189. {
  190. Console.WriteLine("Output file path already exsits, please remove before running or specify -Force\n" + outputFile.FullName);
  191. return false;
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. #endregion
  198. }
  199. }