Exemple #1
0
        private void Init_Window()
        {
            try
            {
                WriteProgress("Setting console colors..", 30);
                if (LibraryConfiguration.IsNotNull("SetColors"))
                {
                    if (LibraryConfiguration.IsTrue("SetColors"))
                    {
                        InvokeScript(string.Format("$host.UI.RawUI.ForegroundColor = \"{0}\"", LibraryConfiguration.IsNotNull("ForegroundColor") ? LibraryConfiguration.Setting <string>("ForegroundColor") : "White"));
                        InvokeScript(string.Format("$host.UI.RawUI.BackgroundColor = \"{0}\"", LibraryConfiguration.IsNotNull("BackgroundColor") ? LibraryConfiguration.Setting <string>("BackgroundColor") : "DarkBlue"));
                    }
                }
            }
            catch (Exception ex)
            {
                WriteWarning(string.Format("Could not process ConsoleColors, error: {0}", ex.Message));
            }

            try
            {
                WriteProgress("Disabling window control box..", 35);
                if (LibraryConfiguration.IsNotNull("DisableControlBox"))
                {
                    if (LibraryConfiguration.IsTrue("DisableControlBox"))
                    {
                        InvokeScript("Disable-WindowControlBox");
                    }
                }
            }
            catch (Exception ex)
            {
                WriteWarning(string.Format("Could not process ControlBox, error: {0}", ex.Message));
            }
        }
Exemple #2
0
 private void Init_Prompt()
 {
     try
     {
         WriteProgress("Setting up prompt..", 25);
         if (LibraryConfiguration.IsNotNull("ReplacePrompt"))
         {
             if (LibraryConfiguration.IsTrue("ReplacePrompt"))
             {
                 InvokeScript((string)Resources.General.Prompt);
             }
         }
     }
     catch (Exception ex)
     {
         WriteWarning(string.Format("Could not process Prompt, error: {0}", ex.Message));
     }
 }
Exemple #3
0
 private void Init_Banner()
 {
     try
     {
         WriteProgress("Finishing touches..", 95);
         if (LibraryConfiguration.IsNotNull("PrintBanner"))
         {
             if (LibraryConfiguration.IsTrue("PrintBanner"))
             {
                 Version vInfo = Assembly.GetExecutingAssembly().GetName().Version;
                 InvokeScript("Clear-Host");
                 InvokeScript(string.Format("Write-Host \"Sorlov PowerShell Utilities (v{0}.{1}.{2}.{3})\" -ForegroundColor White", vInfo.Major, vInfo.Minor, vInfo.Build, vInfo.Revision));
                 InvokeScript("Write-Host \"Copyright (C) 2010-2017 by Daniel Sörlöv \" -ForegroundColor White");
                 InvokeScript("Write-Host \"All rights reserved. Freeware version.\" -ForegroundColor White");
                 InvokeScript("Write-Host");
                 InvokeScript("Write-Host");
             }
         }
     }
     catch (Exception ex)
     {
         WriteWarning(string.Format("Could not process PrintBanner, error: {0}", ex.Message));
     }
 }
Exemple #4
0
        private void Init_PersistedData(string pathName)
        {
            try
            {
                WriteProgress("Restoring data..", 50);
                string configFile = System.IO.Path.Combine(pathName, "Sorlov.PowerShell.PersistedState.xml");
                if (System.IO.File.Exists(configFile))
                {
                    WriteProgress("Loading persisted state..", 55);
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(configFile);

                    try
                    {
                        if (LibraryConfiguration.IsNotNull("PersistPath"))
                        {
                            if (LibraryConfiguration.IsTrue("PersistPath"))
                            {
                                WriteProgress("Processing general items..", 60);
                                XmlNode pwdNode = xmlDoc.SelectSingleNode("/PSState/General/Pwd");
                                InvokeScript(string.Format("Set-Location \"{0}\"", pwdNode.Attributes["Value"].Value));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteWarning(string.Format("Could not process PersistPath, error: {0}", ex.Message));
                    }

                    try
                    {
                        if (LibraryConfiguration.IsNotNull("PersistHistory"))
                        {
                            if (LibraryConfiguration.IsTrue("PersistHistory"))
                            {
                                WriteProgress("Restoring history..", 65);
                                int    id          = 0;
                                string historyFile = "@'" + Environment.NewLine;
                                historyFile += "#TYPE Microsoft.PowerShell.Commands.HistoryInfo" + Environment.NewLine;
                                historyFile += "\"Id\",\"CommandLine\",\"ExecutionStatus\",\"StartExecutionTime\",\"EndExecutionTime\"" + Environment.NewLine;
                                XmlNodeList history = xmlDoc.SelectNodes("/PSState/HistoryList/History");
                                foreach (XmlNode historyNode in history)
                                {
                                    historyFile += string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\"" + Environment.NewLine, historyNode.Attributes["Id"].Value, historyNode.Attributes["CommandLine"].Value, historyNode.Attributes["ExecutionStatus"].Value, historyNode.Attributes["StartExecutionTime"].Value, historyNode.Attributes["EndExecutionTime"].Value);
                                    id++;
                                }
                                historyFile += "'@ | ConvertFrom-CSV | Add-History";
                                InvokeScript(historyFile);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteWarning(string.Format("Could not process PersistHistory, error: {0}", ex.Message));
                    }

                    try
                    {
                        if (LibraryConfiguration.IsNotNull("PersistAliases"))
                        {
                            if (LibraryConfiguration.IsTrue("PersistAliases"))
                            {
                                WriteProgress("Restoring Aliases..", 75);
                                XmlNodeList aliases = xmlDoc.SelectNodes("/PSState/AliasList/Alias");
                                foreach (XmlNode alias in aliases)
                                {
                                    InvokeScript(string.Format("Set-Alias -Name {0} -Value {1} -Scope Global -Force -ErrorAction SilentlyContinue", alias.Attributes["Name"].Value, alias.Attributes["Definition"].Value));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteWarning(string.Format("Could not process PersistAliases, error: {0}", ex.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                WriteWarning(string.Format("Could not process StateRestore, error: {0}", ex.Message));
            }
        }