Skip to content

Add API support for dot sourcing a ScriptBlock #3581

@lzybkr

Description

@lzybkr

It is currently not possible to implement a cmdlet like ForEach-Object or Measure-Command outside of PowerShell because there is no api support for invoking a script block instance in the callers scope - or dot sourcing.

The workaround is quite expensive - it looks something like:

void InvokeInCurrentScope(ScriptBlock sb)
{
    var ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
    ps.AddScript(sb.ToString());
    ps.Invoke();
}

This is expensive we must reparse the script block.

There are a couple possible fixes, maybe we do both:

  • ScriptBlock.Invoke (and family) should expose this parameter in a public api
  • Add a new api or overload to the PowerShell.Add* family of commands, something like AddScriptBlock, or maybe just a new overload on AddCommand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions