public void WriteNativeOutput(string message) { // Write is Dispatcher checked PoshConsole.Write(ConsoleBrushes.NativeOutputForeground, ConsoleBrushes.NativeOutputBackground, message, PoshConsole.Current); PoshConsole.Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)(() => PoshConsole.SetPrompt())); // TODO: REIMPLEMENT NATIVE prompt using Begin/End and Prompt() }
public CmdletPickerWindow(PoshConsole shellHost) { _shellHost = shellHost; InitializeComponent(); setBtn.Click += delegate { Close(); }; Task.Run(() => { var commandsTemp = _shellHost.GetCommands("SourceRun"); if (commandsTemp == null) { return; } Dispatcher.BeginInvoke(new Action(() => { foreach (var pair in commandsTemp) { _commands.Add(pair.Value); } cmdletList.ItemsSource = _commands; })); }); }
internal Host(PoshConsole control, Options options) { PoshConsole = control; _options = options; _UI = new HostUI(PoshConsole); MakeConsole(); }
internal Host(PoshConsole control, Panel progress, Options options) { PoshConsole = control; _options = options; _UI = new HostUI(PoshConsole, progress); MakeConsole(); }
internal Host(PoshConsole control, Panel progress, Options options) { PoshConsole = control; _options = options; _UI = new HostUI(PoshConsole, progress); MakeConsole(); }
async void Exception_Click(object sender, RoutedEventArgs e) { var files = await PoshConsole.InvokeAsync("throw 'whatever'"); if (files.State == PipelineState.Failed) { Dispatcher.Invoke(() => MainContent.DataContext = "Failed Command"); } }
private void Invoke_Click(object sender, RoutedEventArgs e) { if (CommandInput.Text.Length > 0) { // Oh man, please don't do anything like this with user inputs in the real world ... // But if you do, at least have the sense to make sure that isScript:false PoshConsole.InvokeAsync(Command.Text, isScript: false, input: CommandInput.Text.Split(' ')); } }
public ToolControl(PoshConsole shellHost, Tool tool) { _shellHost = shellHost; Tool = tool; InitializeComponent(); mainGrid.DataContext = Tool; AutoSetColor(); }
/// <summary> /// Returns help for a single PowerShell command. /// </summary> /// <param name="command">String containing the name of the command.</param> /// <returns>Preformatted string containing the help for the command.</returns> public static string GetCommandHelp(this PoshConsole shellHost, string command) { var par = shellHost.Invoke($"Get-Help -Name {command} | Out-String"); if (par.Count <= 0 || par[0] == null) { return(null); } return(par[0].ToString()); }
/// <summary> /// Returns information from a single PowerShell command. /// </summary> /// <param name="shellHost">Shell host.</param> /// <param name="command">String containing the name of the command.</param> /// <returns>CommandInfo with information for the command.</returns> public static CommandInfo GetCommand(this PoshConsole shellHost, string command) { var par = shellHost.Invoke($"Get-Command -Name {command}"); if (par.Count <= 0 || par[0] == null) { return(null); } return(par[0].BaseObject as CommandInfo); }
async void Input_Click(object sender, RoutedEventArgs e) { // this is not part of the test/demo ... we just need something to use as input // You should _not_ do this in lieu of using the pipeline var processes = await PoshConsole.InvokeAsync("Get-Process | Sort CPU -Descending | Select -First 10", output : ConsoleOutput.None); // Now invoke a command with pipeline input: await PoshConsole.InvokeAsync("Format-Table", isScript : false, input : processes.Output); Dispatcher.Invoke(() => MainContent.DataContext = processes.Output); }
/// <summary> /// Returns a list of parameters a single command accepts. /// </summary> /// <param name="command">String containing the name of the command.</param> /// <returns>IDictionary with a key containing the parameter name and a value containing the metadata.</returns> public static IDictionary <string, ParameterMetadata> GetParameters(this PoshConsole shellHost, string command) { var commandMeta = shellHost.GetCommand(command); if (command == null) { return(null); } var meta = commandMeta.Parameters as IDictionary <string, ParameterMetadata>; return(meta); }
/// <summary> /// Returns a list of commands from a PowerShell module. /// </summary> /// <param name="shellHost">Shell host.</param> /// <param name="module">String containing the name of the module.</param> /// <returns>IDictionary containing the command and command info.</returns> public static IDictionary <string, CommandInfo> GetCommands(this PoshConsole shellHost, string module) { var par = shellHost.Invoke($"(Get-Module -Name {module}).ExportedCommands"); if (par.Count <= 0 || par[0] == null) { return(null); } var meta = par[0].BaseObject as IDictionary <string, CommandInfo>; return(meta); }
public WorkspaceSettingsWindow(IServiceProvider serviceProvider, PoshConsole shellHost, Workspace workspace) { _shellHost = shellHost; _workspace = workspace; workspace.UnappliedChanges = false; _perforce = serviceProvider.GetService <PerforceService>(); _steam = serviceProvider.GetService <SteamService>(); InitializeComponent(); MainGrid.DataContext = workspace; BranchSelector.ItemsSource = _steam.Cache.SourceGames; }
async void Error_Click(object sender, RoutedEventArgs e) { var files = await PoshConsole.InvokeAsync("Get-ChildItem NoSuchFile"); if (files.State == PipelineState.Failed) { Dispatcher.Invoke(() => MainContent.DataContext = "Failed Command"); } else if (!files.HadErrors) { Dispatcher.Invoke(() => MainContent.DataContext = files.Output); } }
public void Render(bool printColumnNames, int leftPad = 0, params ConsoleColor[] columnColors) { //get max length for each column int[] widths = new int[_columnNames.Length]; for (int i = 0; i < _columnNames.Length; i++) { int max = printColumnNames ? _columnNames[i].Length : -1; foreach (object[] row in _rows) { int l = row[i].ToString().Length; if (l > max) { max = l; } } widths[i] = max; } //draw header if (printColumnNames) { //todo } foreach (object[] row in _rows) { for (int i = 0; i < _columnNames.Length; i++) { if (leftPad > 0 && i == 0) { Console.Write(new string(' ', leftPad)); } string v = row[i]?.ToString() ?? string.Empty; v = v.PadRight(widths[i]); ConsoleColor color = (columnColors != null && i < columnColors.Length) ? columnColors[i] : PoshConsole.T.NormalTextColor; PoshConsole.Write(v, color); Console.Write(" "); } Console.WriteLine(); } }
public static CmdletTool PickTool(PoshConsole shellHost) { var pickerWindow = new CmdletPickerWindow(shellHost); pickerWindow.ShowDialog(); if (pickerWindow.cmdletList.SelectedItem == null) { return(null); } var item = (CommandInfo)pickerWindow.cmdletList.SelectedItem; var newTool = new CmdletTool(item); return(newTool); }
public ToolControlWindow(PoshConsole shellHost, Canvas canvas, Tool tool, int tabIndex = 0) { _shellHost = shellHost; _canvas = canvas; Tool = tool; InitializeComponent(); tabControl.TabIndex = tabIndex; availableList.ItemsSource = _availableParameters; staticButton.Click += delegate { _selectedParam.ParamMode = ParameterMode.Content; }; connectionButton.Click += delegate { _selectedParam.ParamMode = ParameterMode.Reference; }; variableButton.Click += delegate { _selectedParam.ParamMode = ParameterMode.Variable; }; switchButton.Click += delegate { _selectedParam.ParamMode = ParameterMode.Switch; }; RefreshTool(); }
async void Pipeline_Click(object sender, RoutedEventArgs e) { // When you want to accept user input for parameters, you should always build your pipeline using Commands var ps = new Command("Get-Process"); var sort = new Command("Sort-Object"); // That way, you can pass the user input to a specific parameter, and avoid code injection: sort.Parameters.Add("Property", ProcessSort.SelectedValue); // Switch parameters... if (ProcessDescending.IsChecked == true) { sort.Parameters.Add("Descending"); } // Note that PowerShell is still dynamic, so there's no validation going on here // But these parameter values are not script, so the user can't use $(tricks) to execute code var select = new Command("Select-Object"); select.Parameters.Add("First", (int)ProcessCount.Value); // Now pass them in order to InvokeAsync: var processes = await PoshConsole.InvokeAsync(new [] { ps, sort, select }); }
public void WriteNativeError(string message) { // Write is Dispatcher checked PoshConsole.Write(ConsoleBrushes.NativeErrorForeground, ConsoleBrushes.NativeErrorBackground, message, PoshConsole.Current); PoshConsole.Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)(() => PoshConsole.SetPrompt())); }
public Options(PoshConsole wpfConsole) { WpfConsole = wpfConsole; }
/// <summary> /// Returns help for a single parameter of a PowerShell command. /// </summary> /// <param name="command">String containing the name of the command.</param> /// <param name="parameter">String containing the name of the parameter.</param> /// <returns>Preformatted string containing the help for the parameter.</returns> public static string GetParameterHelp(this PoshConsole shellHost, string command, string parameter) { var par = shellHost.Invoke($"Get-Help -Name {command} -Parameter {parameter} | Out-String"); return(par.Count <= 0 ? null : par[0].ToString()); }
public HostUI(PoshConsole control, Panel progress) { ProgressPanel = progress; _control = control; RawUI = new HostRawUI(control); }
public Options(PoshConsole wpfConsole) { WpfConsole = wpfConsole; }
/* * private void Console_PromptForObject(object sender, PromptForObjectEventArgs e) * { * * var results = new Dictionary<string, PSObject>(); * foreach (var fieldDescription in e.Descriptions) * { * var type = Type.GetType(fieldDescription.ParameterAssemblyFullName); * var prompt = string.IsNullOrEmpty(fieldDescription.Label) ? fieldDescription.Name : fieldDescription.Label; * if (type != null && type.IsArray) * { * type = type.GetElementType(); * var output = new List<PSObject>(); * int count = 0; * do * { * var single = GetSingle(e.Caption, e.Message, $"{prompt}[{count++}]", fieldDescription.HelpMessage, fieldDescription.DefaultValue, type); * if (single == null) break; * * if (!(single.BaseObject is string) || ((string)single.BaseObject).Length > 0) * { * output.Add(single); * } * else break; * } while (true); * * results[fieldDescription.Name] = PSObject.AsPSObject(output.ToArray()); * } * else * { * results[fieldDescription.Name] = GetSingle(e.Caption, e.Message, prompt, fieldDescription.HelpMessage, fieldDescription.DefaultValue, type); * } * * } * e.Results = results; * } * * private PSObject GetSingle(string caption, string message, string prompt, string help, PSObject psDefault, Type type) * { * if (null != type && type == typeof(PSCredential)) * { * return PSObject.AsPSObject(CredentialUI.Prompt(caption, message)); * } * * while (true) * { * // TODO: Only show the help message if they type '?' as their entry something, in which case show help and re-prompt. * if (!String.IsNullOrEmpty(help)) * Write(help + "\n"); * * Write($"{prompt}: "); * * if (null != type && typeof(SecureString) == type) * { * var userData = ReadLineAsSecureString() ?? new SecureString(); * return PSObject.AsPSObject(userData); * } // Note: This doesn't look the way it does in PowerShell, but it should work :) * else * { * if (psDefault != null && psDefault.ToString().Length > 0) * { * if (Dispatcher.CheckAccess()) * { * CurrentCommand = psDefault.ToString(); * _commandBox.SelectAll(); * } * else * { * Dispatcher.BeginInvoke(DispatcherPriority.Input, (Action<string>)(def => * { * CurrentCommand = def; * _commandBox.SelectAll(); * }), psDefault.ToString()); * } * } * * var userData = ReadLine(); * * if (type != null && userData.Length > 0) * { * object output; * var ice = TryConvertTo(type, userData, out output); * // Special exceptions that happen when casting to numbers and such ... * if (ice == null) * { * return PSObject.AsPSObject(output); * } * if ((ice.InnerException is FormatException) || (ice.InnerException is OverflowException)) * { * Write(ConsoleBrushes.ErrorForeground, ConsoleBrushes.ErrorBackground, * $@"Cannot recognize ""{userData}"" as a {type.FullName} due to a format error.\n"); * } * else * { * return PSObject.AsPSObject(String.Empty); * } * } * else if (userData.Length == 0) * { * return PSObject.AsPSObject(String.Empty); * } * else return PSObject.AsPSObject(userData); * } * } * } */ async void Capture_Click(object sender, RoutedEventArgs e) { var files = await PoshConsole.InvokeAsync(new Command("Get-ChildItem")); Dispatcher.Invoke(() => MainContent.DataContext = files.Output); }
public HostUI(PoshConsole control, Panel progress) { ProgressPanel = progress; _control = control; RawUI = new HostRawUI(control); }
async void Secret_Click(object sender, RoutedEventArgs e) { var processes = await PoshConsole.InvokeAsync("Get-Process | Select -First 25", output : ConsoleOutput.None); Dispatcher.Invoke(() => MainContent.DataContext = processes.Output); }
public HostUI(PoshConsole control) { _control = control; RawUI = new HostRawUI(control); }
void Console_Click(object sender, RoutedEventArgs e) { PoshConsole.Invoke("Write-Output $PSVersionTable"); }