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)); } }
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)); } }
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)); } }
protected override void BeginProcessing() { string pathName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "WindowsPowershell\\Modules\\Sorlov.PowerShell"); base.BeginProcessing(); WriteProgress("Initializing..", 0); WriteProgress("Creating configuration..", 10); XmlDocument xmlDocument = new XmlDocument(); XmlNode xmlRoot = xmlDocument.CreateNode(XmlNodeType.Element, "PSState", ""); xmlDocument.AppendChild(xmlRoot); if (LibraryConfiguration.IsTrue("PersistPath")) { WriteProgress("General settings..", 20); XmlNode xmlGeneral = CreateXmlNode(xmlRoot, "General"); CreateXmlNode(xmlGeneral, "Pwd", CreateSimpleDictionary("Value", SessionState.Path.CurrentLocation.ProviderPath)); } if (LibraryConfiguration.IsTrue("PersistHistory")) { WriteProgress("History..", 30); List <PSObject> history = SessionState.InvokeCommand.InvokeScript("Get-History -Count 100").ToList <PSObject>(); XmlNode xmlHistory = CreateXmlNode(xmlRoot, "HistoryList"); foreach (PSObject historyObject in history) { Dictionary <string, string> props = CreateSimpleDictionary("Id", historyObject.Properties["Id"].Value.ToString()); props.Add("CommandLine", historyObject.Properties["CommandLine"].Value.ToString()); props.Add("ExecutionStatus", historyObject.Properties["ExecutionStatus"].Value.ToString()); props.Add("StartExecutionTime", historyObject.Properties["StartExecutionTime"].Value.ToString()); props.Add("EndExecutionTime", historyObject.Properties["EndExecutionTime"].Value.ToString()); CreateXmlNode(xmlHistory, "History", props); } } if (LibraryConfiguration.IsTrue("PersistAliases")) { SessionStateAliasEntry[] aliasInfo = typeof(System.Management.Automation.Runspaces.InitialSessionState).GetProperty("BuiltInAliases", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null, new object[0]) as SessionStateAliasEntry[]; Dictionary <string, SessionStateAliasEntry> builtInAliases = new Dictionary <string, SessionStateAliasEntry>(); foreach (SessionStateAliasEntry entry in aliasInfo) { builtInAliases.Add(entry.Name.ToLower(), entry); } WriteProgress("Aliases..", 50); XmlNode xmlAlias = CreateXmlNode(xmlRoot, "AliasList"); List <PSObject> alias = SessionState.InvokeCommand.InvokeScript("Get-Alias").ToList <PSObject>(); foreach (PSObject aliasObject in alias) { if (!builtInAliases.ContainsKey(aliasObject.Properties["Name"].Value.ToString().ToLower())) { Dictionary <string, string> props = CreateSimpleDictionary("Name", aliasObject.Properties["Name"].Value.ToString()); props.Add("Definition", aliasObject.Properties["Definition"].Value.ToString()); CreateXmlNode(xmlAlias, "Alias", props); } } } WriteProgress("Saving..", 90); string savePath = System.IO.Path.Combine(pathName, "Sorlov.PowerShell.PersistedState.xml"); xmlDocument.Save(savePath); WriteProgress("Done", 100); }
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)); } }