question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide/document an official way to run a PSCmdlet from another PSCmdlet

See original GitHub issue

Summary of the new feature / enhancement

There’s an official article describing how to invoke a Cmdlet from another cmdlet: https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/how-to-invoke-a-cmdlet-from-within-a-cmdlet?view=powershell-7.2 However, it does not describe any way to do the same for PSCmdlet, which I believe is a much more common request, given that most of builtin cmdlets inherit from PSCmdlet. On Stack Overflow, one can find guides how to do that, but at least some of them are either wrong, or grossly inefficient (e.g. spawning a new PowerShell instance just to run a single cmdlet).

I’d appreciate an official documentation on how to invoke PSCmdlet-deriving cmdlets, and ideally a new, discoverable, type-safe method on PSCmdlet to do that without extra workarounds.

Proposed technical implementation details (optional)

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
SeeminglySciencecommented, Sep 26, 2022

A PSCmdlet inherits from Cmdlet so whatever you can do with Cmdlet should be possible from PSCmdlet. You can see PSCmdlet.Invoke<T> in the main doc page for that class (although it just redirects to the Cmdlet one due to inheritance.

Nah it throws at runtime for PSCmdlet. The only way to invoke another command from a binary cmdlet is with the SMA.PowerShell API

1reaction
jborean93commented, Sep 26, 2022

Ah ok, good to know, did not realise that was the case

Read more comments on GitHub >

github_iconTop Results From Across the Web

To invoke a cmdlet from within a PSCmdlet
To invoke a script based cmdlet or binary cmdlet inheriting from [PSCmdlet] you must build a PowerShell pipeline with the command and parameters ......
Read more >
Invoke a PSCmdlet from within a cmdlet
1. Option. Invoke CommandB by creating an instance of it. That would've been my first guess. Like so: var cmd = new CommandB();...
Read more >
How to Invoke a Cmdlet from Within a Cmdlet - PowerShell
This example shows how to invoke a binary cmdlet that derives from [System. Management. Automation. Cmdlet] directly from within another binary ...
Read more >
Testing PowerShell cmdlets written in C#
The article describes ways of testing PowerShell cmdlets written in C#. From manual, through invocation in PS to standard unit testing.
Read more >
Introduction to PowerShell Session State
The PSCmdlet.SessionState.PSVariable object has Set() and GetValue() methods, both of which take a name and an arbitrary object: the former is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found