Powershell: Add a Invoke-PrivateCommand into PowerShell.Core

Created on 3 Jul 2020  路  6Comments  路  Source: PowerShell/PowerShell

Allows the use of Privately Scoped Functions/Cmdlets in a more PowerShell friendly way than doing this

$module = Get-Module MyModule
& $module { Get-PrivateFunction }

Proposed technical implementation details (optional)

Using pure PowerShell this can be done like so - https://github.com/kilasuit/Invoke-PrivateCommand/blob/master/InvokePrivateCommand.ps1

But I am not sure how you'd do the same in C# to add this to Microsoft.PowerShell.Core (or Microsoft.PowerShell.Utility)

It is potentially going to be easier adding this as a script function that is shipped alongside and loaded as part of the loading of the module anyway.

Area-Cmdlets-Core Issue-Enhancement

Most helpful comment

Adding to the Core only makes sense if commonly used scenarios exist. It also violates an intent of the author of the module. Private functions can be changed by the author at any time. This cannot be used on a regular basis. Similar code could be in an analysis module (like Pester) but not in Core.

All 6 comments

Adding on some of @SeeminglyScience's suggestions from the PS discord in case they were missed:

// moduleInfo should be an instance of PSModuleInfo
var sb = moduleInfo.NewBoundScriptBlock(originalSb);
PowerShell.Create(RunspaceMode.CurrentRunspace).AddScript(". $args[0]", useLocalScope: false).AddArgument(sb)

and

InvokeCommand.InvokeScript(moduleInfo.SessionState, scriptBlock.Ast.GetScriptBlock())

Note that Ast.GetScriptBlock() is only required if scriptBlock has an existing session state affinity (Due to a bug, see #12883). If it was created with ScriptBlock.Create it can be skipped.

That said, the original description reads like you're looking to add a Invoke-PrivateCommand as a built in command? If so, maybe a separate issue should be opened discussing the use cases before you put in the effort of drafting a PR.

That said, the original description reads like you're looking to add a Invoke-PrivateCommand as a built in command? If so, maybe a separate issue should be opened discussing the use cases before you put in the effort of drafting a PR.

I opened this issue to see if there was a desire to have this built in or not (my view it should be). As otherwise I'll release it as a seperate module just making use of the PowerShell implementation due to it's simplicity.

Adding to the Core only makes sense if commonly used scenarios exist. It also violates an intent of the author of the module. Private functions can be changed by the author at any time. This cannot be used on a regular basis. Similar code could be in an analysis module (like Pester) but not in Core.

Pester provides an InModuleScope function for this purpose, yeah. Dunno if I think it should / needs to be a default cmdlet. 馃し

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HumanEquivalentUnit picture HumanEquivalentUnit  路  3Comments

garegin16 picture garegin16  路  3Comments

concentrateddon picture concentrateddon  路  3Comments

rkeithhill picture rkeithhill  路  3Comments

andschwa picture andschwa  路  3Comments