From 21ec6a5f8e646b564e8b11fa4fba8de41c1fdfd7 Mon Sep 17 00:00:00 2001 From: halvarsson Date: Sat, 25 Feb 2023 18:55:33 +0100 Subject: [PATCH] Use AddCommand instead of InvokeCommand The current way of executing it is unable to parse arguments The updated approach works on both powershell 5.1 and 7 :-) --- ShowEditorCommand.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ShowEditorCommand.cs b/ShowEditorCommand.cs index 611f113..743398b 100644 --- a/ShowEditorCommand.cs +++ b/ShowEditorCommand.cs @@ -190,10 +190,16 @@ private void Format() var formatValue = textEditor.Text.ToString(); if (!System.String.IsNullOrEmpty(formatValue)) { - var formatted = InvokeCommand.InvokeScript("Invoke-Formatter -ScriptDefinition $args[0]", formatValue).FirstOrDefault(); - if (formatted != null) + using (var powerShell = PowerShell.Create(RunspaceMode.CurrentRunspace)) { - textEditor.Text = formatted.BaseObject as string; + powerShell.AddCommand("Invoke-Formatter"); + powerShell.AddParameter("ScriptDefinition", formatValue); + var result = powerShell.Invoke(); + var formatted = result.FirstOrDefault(); + if (formatted != null) + { + textEditor.Text = formatted.BaseObject as string; + } } } }