How to hide “Helper” Functions in Powershell Modul

2020-02-20 07:02发布

I have a module xy which has several functions for the end user and several internal helper functions called by functions but not by the end user.

Get-Command -Module xy -CommandType function

lists all functions I have in my module folder (e.g. get-foo and get-foo_helper)

Is there a way to hide get-foo_helper function from the end user who is using:

Get-Command -Module tcaps -CommandType function

4条回答
祖国的老花朵
2楼-- · 2020-02-20 07:12

One thing I have done is use the verb-noun naming convention for functions I want to export, but leave out the hyphen in helper functions.

Then, export-modulemember *-* takes care of only exporting what you want to export.

查看更多
看我几分像从前
3楼-- · 2020-02-20 07:19

You can use Export-ModuleMember or create a Module Manifest and specify the exported commands. You can use New-ModuleManifest to create a manifest file.

查看更多
可以哭但决不认输i
4楼-- · 2020-02-20 07:25

Just add Export-ModuleMember to the bottom of you module.

Let's say you have the following Functions in your module:

New-Function0
New-Function1
New-Function2
New-HelperFunction0

Add these lines to the bottom of the module file:

Export-ModuleMember -function New-Function0
Export-ModuleMember -function New-Function1
Export-ModuleMember -function New-Function2

When you run Import-Module on this module, it will only import the functions defined by Export-ModuleMember.

Now let's say you also wanted to export an alias for New-Function1. just add this to the end of your module:

Export-ModuleMember -alias nf1

Now when you use Import-Module, it will load the functions you defined, as well as an alias (nf1) for New-Function1.

查看更多
你好瞎i
5楼-- · 2020-02-20 07:27

In many cases, a declared function can be replaced with a scriptblock (ie an anonymous function).

查看更多
登录 后发表回答