-
Notifications
You must be signed in to change notification settings - Fork 0
The 'Export ModuleMember' Problem
Christian Seidlitz edited this page Nov 14, 2022
·
4 revisions
I have got a pretty nice solution to the "Export-ModuleMember" problem which my OCD often faces. Problem is, that the declaration of a function is separated from the "Export-ModuleMember" command, which makes a module harder to maintain, as you have to check the functions you export on every rename or addition/removal.
A soltion is to 'invent' an attribute in your psm1 file:
class InternalAttribute : System.Attribute {}
then apply it to every param
block of the functions which you want to keep internal
function ConvertTo-QueryParameters {
[Internal()]
param( ... )
...
}
Now at the end of your .psm1 file, after loading all the functions, you can import only the functions without the attribute
Get-Command -Module $ExecutionContext.SessionState.Module
| Where-Object { "Internal" -notin $_.ScriptBlock.Ast.Body.ParamBlock.Attributes.TypeName.FullName }
| Export-ModuleMember