TacoMaster6000
TacoMaster6000

Reputation: 324

How to retrieve exported variable from module?

How do I retrieve a variable exported from my module after an import?

# in mymodule.psm1
$myVar = New-Object VarClass
#assume a function or two here.
Export-Modulemember -Function * -Variable *

# in myScript.ps1 after module import
# how do I reference this variable?
$myScriptVar = $myVar

Sorry for the basic question, I cannot find any examples on this simple problem.

Upvotes: 3

Views: 945

Answers (1)

TacoMaster6000
TacoMaster6000

Reputation: 324

You must explicitly state the functions and variables in the export module-member cmdlet when exporting both. My problem was that I had a functions as well.

From powershell documentation:

If you want to export a variable, in addition to exporting the functions in a module, the Export-ModuleMember command must include the names of all of the functions and the name of the variable.

In this instance I have to have the following in my module:

Export-Modulemember -Function Verb-Noun -Variable myVar 

I got stuck because the same is true in the manifest, you cannot use a wildcard if you're exporting both.

# Functions to export from this module
FunctionsToExport = 'Verb-Noun'

# Variables to export from this module
VariablesToExport = 'myVar'

Upvotes: 2

Related Questions