Reputation: 634
I have a myFunc.psm1 file like this:
$ApiVersion = "201846465"
Export-ModuleMember -variable ApiVersion
function Get-Something {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Id]
)
process {
# ...
}
When I import this to another setup.ps1 file, I saw this in execution:
VERBOSE: Loading module from path 'D:\myFunc.psm1'.
VERBOSE: Importing variable 'ApiVersion'.
VERBOSE: Hi from setup.ps1
When I remove
$ApiVersion = "201846465"
Export-ModuleMember -variable ApiVersion
I can see:
VERBOSE: Exporting function 'Get-Something'.
VERBOSE: Importing function 'Get-Something'.
VERBOSE: Hi from setup.ps1
Why is this happening and how do I fix it?
Upvotes: 1
Views: 310
Reputation: 437197
In the absence of a Export-ModuleMember
call inside a *.psm1
file, all functions and aliases[1] are automatically exported - but not variables.
Once you use an Export-ModuleMember
call, the automatic exporting is deactivated, and you must then explicitly name all elements to be exported - including functions and aliases.
Therefore:
Export-ModuleMember -Variable ApiVersion -Function Get-Something
Be sure to place the Export-ModuleMember
call at the bottom of your file, to make sure that all elements you want to export have already been defined - otherwise, they're ignored.
[1] Curiously, in dynamic modules created with New-Module
, it is functions only (not also aliases) that are automatically exported.
Upvotes: 1