View nested private function definitions in PowerShell

PowerShell provides an easy way to view the contents of a function, for example.

Get-Content function:MyFuncName # (A) 

or equivalent

 (Get-ChildItem function:MyFuncName).definition # (B) 

where MyFuncName is the name of my function. This is great for simple functions (i.e. Functions that use only the base language constructs and do not call other functions). But consider the function foo below, which contains a call to the bar function. In a typical scenario, they will be contained in the same module, the open API of which consists only of the function foo and, therefore, this is the only function exported.

 function foo () { $p = bar "here" "result is '$p'" } function bar ([string] $s) { $s + $s } Export-ModuleMember foo 

Is there a way to view nested, unexported functions (e.g. bar function) in another function compared to (A) or (B) above? (That is, without opening the .psm1 file in the editor :-)

+4
source share
2 answers

I'm not sure that you can do this for a specific function in a module, but you can do this for an entire module:

 Import-Module C:\Test.psm1 (Get-Module Test).Definition 

I think the fact that the foo function calls the function panel is unknown until runtime.

Update

Where there is a will, there is a way :-) Here you can access the private members of the module. Call the module using the script block. Private members are visible inside the script block.

 Import-Module C:\Test.psm1 $module = Get-Module Test & $module { (get-item function:bar).Definition } 

Thanks to PowerTips :-) http://powershell.com/cs/blogs/tips/archive/2009/09/18/accessing-hidden-module-members.aspx

Update 2

After discovering a small PowerTip fragment, I was curious what was really happening ... The fragment uses the & call operator with two arguments.

  • Module object ( System.Management.Automation.PSModuleInfo )
  • A script block

So what really happens is the Invoke method of type PSModuleInfo . The code in the script block is executed in the same session state as the rest of the module code, so it has access to private members. This code does the same as the PowerTip snippet:

 $module = Get-Module Test $module.Invoke( { (get-item function:bar).Definition } ) 

Check out the calling method here: http://msdn.microsoft.com/en-us/library/system.management.automation.psmoduleinfo.invoke(v=vs.85).aspx

+4
source

Not. The method you use gets the function definition through the function provider in the local scope. It will only see functions that have been defined in the local scope or that are visible in the parent scopes.

When you call a function, it starts in its own area. Any functions created by the function will be created in this child area and will exist only during the execution of this function. When the function is executed, the area in which it is launched is deleted, and all the functions that it created go with it.

0
source

Source: https://habr.com/ru/post/1397646/


All Articles