sthModuleScope – functions for working with elements in module’s scope.

sthModuleScope – is a module containing four functions for working with elements in module’s scope. If you write your own modules, where some of the functions and variables are for internal use only, and not exported into caller’s scope, then this module can be used for diagnosing and debugging.

Function Enter-sthModuleScope allows entering the scope of a specified module and invoking command in that scope. This provides means to call internal module functions and access nonexported module variables. When you enter a module scope, command prompt changes to include the name of the module. To exit from the module scope type ‘exit’.

Function Get-sthModuleScopeFunction returns all the functions, defined in the module specified by -Module parameter, or the module, the scope of which was entered by using the Enter-sthModuleScope function. By default, Get-sthModuleScopeFunction gets all functions – public and private, but you can specify which functions you are interested in by using -PublicOnly and -PrivateOnly parameters.

Function Get-sthModuleScopeVariable returns all the variables, defined in the module specified by -Module parameter, or the module, the scope of which was entered by using the Enter-sthModuleScope function. Like the former function, Get-sthModuleScopeVariable by default gets all variables – public and private, but you can specify which variables you are interested in by using -PublicOnly and -PrivateOnly parameters.

Function Get-sthScopeDepth returns current scope depth, where 0 means, that you are in the global scope, 1 – you are one scope deeper than global, 2 – you are two scopes deeper than global, etc.

You can install sthModuleScope module from PowerShell Gallery:

Install-Module sthModuleScope

Also, you can find it on GitHub:
https://github.com/sethvs/sthModuleScope

But how to use it?

Enter-sthModuleScope

Example 1.

This command enters the scope of the module.
When enter, the command prompt changes to include the name of the module.
After that, the ‘exit’ command issued to exit the module’s scope.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> exit

PS C:\>

Example 2.

These commands enter the scope of the module using PSModuleInfo object.
The first command uses the Get-Module cmdlet to get PSModuleInfo object for the module ‘module_name’.
The second command enters the scope of the module ‘module_name’.
The third command exits the module’s scope.

PS C:\> $Module = Get-Module -Name 'module_name' -ListAvailable

PS C:\> Enter-sthModuleScope -Module $Module

[module_name] PS C:\>> exit

PS C:\>

Example 3.

This command enters the scope of the module using pipeline.

PS C:\> 'module_name' | Enter-sthModuleScope

[module_name] PS C:\>>

Example 4.

This command enters the scope of the module by sending PSModuleInfo object through the pipeline.

PS C:\> Get-Module module_name -ListAvailable | Enter-sthModuleScope

[module_name] PS C:\>>

Get-sthModuleScopeFunction

Example 1.

These commands get all functions, defined in the module ‘module_name’.
The first command enters the scope of the module ‘module_name’.
The second command gets all the module’s functions.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthModuleScopeFunction

Example 2.

These commands get all public functions, defined in the module ‘module_name’.
The first command enters the scope of the module ‘module_name’.
The second command gets all the module’s public functions.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthModuleScopeFunction -PublicOnly

Example 3.

These commands get all private functions, defined in the module ‘module_name’.
The first command enters the scope of the module ‘module_name’.
The second command gets all the module’s private functions.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthModuleScopeFunction -PrivateOnly

Example 4.

This command gets all functions, defined in the module ‘module_name’.

PS C:\> Get-sthModuleScopeFunction -Module 'module_name'

Example 5.

These commands get all functions, defined in the module ‘module_name’ by using PSModuleInfo object.

PS C:\> $Module = Get-Module -Name 'module_info' -ListAvailable

PS C:\> Get-sthModuleScopeFunction -Module $Module

Example 6.

This command gets all functions, defined in the module ‘module_name’ by using pipeline.

PS C:\> 'module_name' | Get-sthModuleScopeFunction

Example 7.

This command gets all functions, defined in the module ‘module_name’ by accepting PSModuleInfo object from the pipeline.

PS C:\> Get-Module -Name 'module_name' -ListAvailable | Get-sthModuleScopeFunction

Get-sthModuleScopeVariable

Example 1.

These commands get all variables, defined in the module ‘module_name’.
The first command enters the scope of the module ‘module_name’.
The second command gets all the module’s variables.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthModuleScopeVariable

Example 2.

These commands get all public variables, defined in the module ‘module_name’.
The first command enters the scope of the module ‘module_name’.
The second command gets all the module’s public variables.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthModuleScopeVariable -PublicOnly

Example 3.

These commands get all private variables, defined in the module ‘module_name’.
The first command enters the scope of the module ‘module_name’.
The second command gets all the module’s private variables.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthModuleScopeVariable -PrivateOnly

Example 4.

This command gets all variables, defined in the module ‘module_name’.

PS C:\> Get-sthModuleScopeVariable -Module 'module_name'

Example 5.

These commands get all variables, defined in the module ‘module_name’ by using PSModuleInfo object.

PS C:\> $Module = Get-Module -Name 'module_info' -ListAvailable

PS C:\> Get-sthModuleScopeVariable -Module $Module

Example 6.

This command gets all variables, defined in the module ‘module_name’ by using pipeline.

PS C:\> 'module_name' | Get-sthModuleScopeVariable

Example 7.

This command gets all variables, defined in the module ‘module_name’ by accepting PSModuleInfo object from the pipeline.

PS C:\> Get-Module -Name 'module_name' -ListAvailable | Get-sthModuleScopeVariable

Get-sthScopeDepth

Example 1.

This command gets current scope depth.
The result is 0, which means – you are in the global scope.

PS C:\> Get-sthScopeDepth
0

Example 2.

These commands get current scope depth from the module’s scope.
The first command enters the scope of the module ‘module_name’.
The second command gets current scope depth.
The result is 1, which means – you are one scope deeper than the global scope.

PS C:\> Enter-sthModuleScope -Module module_name

[module_name] PS C:\>> Get-sthScopeDepth
1

Example 3.

These commands get current module scope depth while debugging the Get-sthModuleScopeFunction function.
The first command enables script debugging.
The second command invokes the Get-sthModuleScopeFunction function with ‘sthModuleScope’ as the -Module parameter value.
Then, on the first debug request ‘Y’ is specified, which means continuing function execution.
On the second debug request ‘S’ is specified, which means suspending execution.
Then, at the debug prompt the Get-sthScopeDepth function invoked.
The result is 2, which means – you are two scopes deeper, than the global scope.

PS C:\> Set-PSDebug -Step
PS C:\> Get-sthModuleScopeFunction -Module sthModuleScope

Continue with this operation?
   1+  >>>> Get-sthModuleScopeFunction -Module sthModuleScope
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

Continue with this operation?
  26+  >>>> {
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): S

PS C:\>> Get-sthScopeDepth
2

Links:

Twitter: https://twitter.com/vsseth
Facebook: https://fb.com/inpowershell
VK: https://vk.com/inpowershell


Russian version of the blog:
https://sergeyvasin.net

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s