Skip to content

Add more methods to directly add a CommandInfo to a PSCommand #12295

Closed

Description

Summary of the new feature/enhancement

Currently PowerShell has a method that allows this, AddCommand(CommandInfo). However, there's no supported way to add a CommandInfo to a PSCommand instance directly. PowerShellEditorServices (and likely other projects) construct a PSCommand instance well before actually getting to the internal PowerShell object, so working with CommandInfo directly can only be done with reflection.

Proposed technical implementation details (optional)

using System.Management.Automation.Runspaces;

namespace System.Management.Automation
{
    public partial sealed class PSCommand
    {
        public PSCommand AddCommand(CommandInfo commandInfo)
        {
            return AddCommand(new Command(commandInfo));
        }
    }
}
namespace System.Management.Automation.Runspaces
{
    public sealed class Command
    {
-       internal Command(CommandInfo commandInfo) : this(commandInfo, false)
+       public Command(CommandInfo commandInfo) : this(commandInfo, false)
        {
        }
    }
}

/cc TylerLeonhardt Rob Holt (@rjmholt)

Activity

  1. added
    Issue-Enhancement
    on Apr 10, 2020
  2. TylerLeonhardt commented on Apr 10, 2020

    @TylerLeonhardt
    Member

    Yes, we have to use reflection to achieve this in PowerShell Editor Services:

    TylerLeonhardt/PowerShellEditorServices@cc24162#diff-fa9220ea78f2263973eaab89e4e26572

    There should be better API surface area here for us to take advantage of.

  3. iSazonov commented on Apr 11, 2020

    @iSazonov
    Collaborator

    The new public API should be approved by PowerShell Committee.
    /cc Steve Lee (@SteveL-MSFT) Dongbo Wang (@daxian-dbw)

    Also question - should it be in 7.0 Servicing and SDK?

  4. added
    WG-Engine
    Review - Committee
    on Apr 11, 2020
  5. SteveL-MSFT commented on Apr 15, 2020

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this, the PowerShell class already exposes a method AddCommand() that takes a CommandInfo so some concerns regarding passing in a CommandInfo from one runspace into another that may be out of date is already an issue, so from that perspective, it seems there is no concern about making this public since it's already public via the PowerShell class.

  6. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Apr 15, 2020
  7. linked a pull request that will close this issueAdd CmdletInfo to Command Clone() #12301on Apr 16, 2020
  8. bpayette commented on Apr 16, 2020

    @bpayette
    Contributor

    TylerLeonhardt InvocationIntrinsics has a bunch of methods for creating CommandInfo objects: GetCmdlet(), GetCmdletByTypename() , GetCommand(), etc. and the PowerShell object has an AddCommand() overload that takes CommandInfo. So there's really no need to explicitly create `PSCommand' objects.

    Patrick Meinecke (@SeeminglyScience)

    construct a PSCommand instance well before actually getting to the internal PowerShell object,

    That seems kind of backwards. Why don't you create the PowerShell object first, then pass it around instead of the command objects? A PowerShell API object is really just a future computation that's reified when you call Invoke(). The intent of the PowerShell class was to hold all of the metadata for that computation.

  9. SeeminglyScience commented on Apr 16, 2020

    @SeeminglyScience
    ContributorAuthor

    TylerLeonhardt InvocationIntrinsics has a bunch of methods for creating CommandInfo objects: GetCmdlet(), GetCmdletByTypename() , GetCommand(), etc. and the PowerShell object has an AddCommand() overload that takes CommandInfo. So there's really no need to explicitly create `PSCommand' objects.

    Can't make a CommandInfo of just a generic script though. Also can't add parameters, multiple statements, or multiple pipeline elements.

    Patrick Meinecke (@SeeminglyScience)

    construct a PSCommand instance well before actually getting to the internal PowerShell object,

    That seems kind of backwards. Why don't you create the PowerShell object first, then pass it around instead of the command objects?

    One so that we can cache instances, two so that state details like nested vs remote vs local type state can be determined way later in the code path.

    A PowerShell API object is really just a future computation that's reified when you call Invoke().
    The intent of the PowerShell class was to hold all of the metadata for that computation.

    True but it's got a lot more metadata than just the command that needs to be invoked. It's got all the streams, whether it's nested, what runspace it's going on, current invocation state.

  10. TylerLeonhardt commented on Apr 18, 2020

    @TylerLeonhardt
    Member

    I don't want to derail this discussion - but for context, in PSES we have this helper method to get CommandInfo's for use in Intellisense help:
    https://github.com/PowerShell/PowerShellEditorServices/blob/765c76c930ffcc7f4727a2d1f1dd7ee3ac71e4ee/src/PowerShellEditorServices/Services/PowerShellContext/Utilities/CommandHelpers.cs#L64-L105

    because it's:

    • thread safe (ish)
    • the only public way of retrieving CommandInfo's

    I stumbled upon this internal API CommandDiscovery.LookupCommandInfo and was wondering if this could be leveraged instead since it:

    • doesn't involve invoking PowerShell
    • will be much faster

    however... I have concerns about thread safety with that API...

    Bruce Payette (@bpayette) any thoughts?

    GitHub
    A common platform for PowerShell development support in any editor or application! - PowerShell/PowerShellEditorServices
  11. SeeminglyScience commented on Apr 18, 2020

    @SeeminglyScience
    ContributorAuthor

    TylerLeonhardt We'd still need to be on the pipeline thread or we'd hit some pretty nasty race conditions. Most of the slowness problems we have are around managing execution on that thread, I don't think skipping the command execution specifically would save us much.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. added
    Resolution-No Activity
    on Nov 16, 2023
  16. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedIssue-EnhancementResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions