static void Main(string[] args) { // Set Title. Console.Title = "Script Junkie"; // Add Services. ServiceManager.Services.Add(new LogService()); ServiceManager.Services.Add(new ArgumentService()); // Debug Arguments // Arguments passed through args variable over power below arguments. #if (DEBUG) // Example of programatically added arguments. //ServiceManager.Services.ArgumentService.AddArgument(ArgumentService.XmlTemplatePath, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ScriptJunkie\\Template.xml"); //ServiceManager.Services.ArgumentService.AddArgument(ArgumentService.XmlPath, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ScriptJunkie\\Template.xml"); #endif // Start Script Junkie. Version version = Assembly.GetEntryAssembly().GetName().Version; ServiceManager.Services.LogService.WriteHeader("Starting Script Junkie v({0})", version.ToString()); ServiceManager.Services.LogService.WriteLine("If executable of script needs to be ran as administrator, run ScriptJunkie as admin.", ConsoleColor.Yellow); // Check if debug mode. if (ServiceManager.Services.ArgumentService.IsDebug) { ServiceManager.Services.LogService.WriteSubHeader("Paused for debug mode. Attach to debugger then press any key to continue."); Console.ReadKey(true); } // Initialization. Setup setup = new Setup(); int exitCode = -1; // Display Arguments ServiceManager.Services.LogService.WriteSubHeader("Arguments"); foreach (Argument arg in ServiceManager.Services.ArgumentService.Arguments) { if (string.IsNullOrEmpty(arg.Value)) { ServiceManager.Services.LogService.WriteLine("\"{0}\" = \"True\"", arg.Key, arg.Value); } else { ServiceManager.Services.LogService.WriteLine("\"{0}\" = \"{1}\"", arg.Key, arg.Value); } } // Checks if it should generate an example xml template. Argument xmlTemplatePath; if (ServiceManager.Services.ArgumentService.TryGetArgument(ArgumentService.XmlTemplatePath, out xmlTemplatePath)) { setup.GenerateXmlTemplate(xmlTemplatePath.Value); } // Checks if it should run setup execution. Argument xmlPath; if (ServiceManager.Services.ArgumentService.TryGetArgument(ArgumentService.XmlPath, out xmlPath)) { if (setup.Initalize(xmlPath.Value)) { exitCode = setup.Execute(); } } else { ServiceManager.Services.LogService.WriteSubHeader("Script Junkie needs an xml file to execute.", ConsoleColor.Red); } // End Script Junkie. ServiceManager.Services.LogService.WriteHeader("Script Junkie has completed."); #if (RELEASE) Environment.Exit(exitCode); #elif (DEBUG) Console.ReadLine(); #endif }
/// <summary> /// Generates a template xml to base custom scripts off of. /// </summary> /// <param name="savePath">The location the template will be saved.</param> public void GenerateXmlTemplate(string savePath) { ServiceManager.Services.LogService.WriteHeader("Generating Xml Template..."); #region Script 1 Script script = new Script(); script.Name = "Script 1"; script.Description = "Does nothing"; Executable executable = new Executable(); executable.Path = "C:/Temp/nothing.ps1"; ArgumentCollection argCollection = new ArgumentCollection(); argCollection.Add(new Argument() { Key = "-i", Value = "C:/Temp/something.bin" }); argCollection.Add(new Argument() { Key = "-x", Value = "" }); ExitCodeCollection exitCollection = new ExitCodeCollection(); exitCollection.Add(new ExitCode() { Value = 0, Message = "Files deleted", IsSuccess = true }); exitCollection.Add(new ExitCode() { Value = 1, Message = "Files failed to delete" }); exitCollection.Add(new ExitCode() { Value = 2, Message = "Couldn't find any files" }); // Add all elements into the single script file. script.Arguments = argCollection; script.ExitCodes = exitCollection; script.Executable = executable; #endregion #region Script 2 Script script2 = new Script(); script2.Name = "Script 2"; script2.Description = "Does nothing"; Executable executable2 = new Executable(); executable2.Path = "C:/Temp/Downloads/Extracted/SomethingThatWasInAZip.exe"; ArgumentCollection argCollection2 = new ArgumentCollection(); argCollection2.Add(new Argument() { Key = "-install" }); argCollection2.Add(new Argument() { Key = "-silent" }); ExitCodeCollection exitCollection2 = new ExitCodeCollection(); exitCollection2.Add(new ExitCode() { Value = 0, Message = "Script 2 has installed", IsSuccess = true }); exitCollection2.Add(new ExitCode() { Value = 1, Message = "Failed to install." }); exitCollection2.Add(new ExitCode() { Value = 2, Message = "Installed but limited." }); // Add all elements into the single script file. script2.Arguments = argCollection; script2.ExitCodes = exitCollection; script2.Executable = executable; // Add the single script above into a collection of scripts. ScriptCollection scriptCollection = new ScriptCollection(); scriptCollection.Add(script); scriptCollection.Add(script2); #endregion ServiceManager.Services.LogService.WriteLine("Generating Download Collection..."); DownloadCollection downloadCollection = new DownloadCollection(); downloadCollection.TimeOut = 60; downloadCollection.RefreshRate = 10; downloadCollection.Add(new Download() { Name = "Nothing Powershell Script", Description = "This script does nothing", DownloadUrl = "www.blank.com/nothing.ps1", DestinationPath = "C:/Temp/Downloads/nothing.ps1" }); downloadCollection.Add(new Download() { Name = "Test Zip File", Description = "This zip has nothing", DownloadUrl = "www.blank.com/nothing.zip", DestinationPath = "C:/Temp/Downloads", ExtractionPath = "C:/Temp/Downloads/Extracted" }); // Add the 2 main elements, the scripts to run and the downloads to download. Setup setup = new Setup(); setup.Scripts = scriptCollection; setup.Downloads = downloadCollection; ServiceManager.Services.LogService.WriteLine("Saving file to: \"{0}\"", savePath); setup.Serialize(savePath); }