How to retrieve exported variable from module? - powershell

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.

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'

Related

How to export variables from a PowerShell module

I've defined a variable in a psm1 file but when I try to access it in another script, after importing the module, I'm not seeing the value set in the psm1 file.
globals.psm1
$blah = "hello world"
my-script.ps1
Import-Module "$PSScriptRoot\globals.psm1" -Force -Verbose
Write-Output "blah: ${blah}"
output
PS C:\blah> .\my-script.ps1
VERBOSE: Loading module from path 'C:\blah\globals.psm1'.
blah: ''
I thought all variables get exported by default. I must be interrupting this wrong:
Specifies the variables that the module exports to the caller's session state. Wildcard characters are permitted. By default, all variables ('*') are exported
source: MSFT Docs -> How to write a PowerShell module manifest
(CTRL + F on 'VariablesToExport' to find the quoted text)
And yes, if I export the variable, I can access it but the documentation says: 'By default, all varialbes ('*') are exported so what am I doing wrong or misunderstanding? 🤔
globals.psm1
$blah = "hello world"
Export-ModuleMember -Variable blah
Your module is not using a module manifest (a companion .psd1 file whose RootModule entry points to your .psm1 file in the case of script modules), whereas the documentation you quote pertains to module manifest-based modules.
If a module consists only of a .psm1 file, and that file contains no Export-ModuleMember calls, the following rule applies:
Only functions and aliases are automatically exported.
Conversely, this means: in order to also export variables, you must use an Export-ModuleMember call - and if you do, the slate is wiped clean, so to speak, and you must explicitly specify all definitions you want to export (in the simplest case, use Export-ModuleMember -Function * -Alias * -Variable *).
Also, be sure to place this call at the end of your .psm1 file, to ensure that all definitions to export have already been defined.
Caveat, if a manifest (.psd1) is used:
The manifest's *ToExport keys apply on top of what the .psm1 file - implicitly or explicitly - exports, i.e. you can use it to further narrow what is to be exported, by explicitly enumerating the elements to export, which not only makes the module more self-describing, but also helps performance when PowerShell auto-discovers the commands in available, but not-(yet)-imported modules.
Therefore, if a manifest-based module wants to export variables, it too must have an explicit Export-ModuleMember call in its .psm1 file, with the manifest potentially narrowing down what variables are ultimately to be exported.
Generally, exporting variables from modules is best avoided, because:
it increases the risk of name collisions with variables of the same name defined elsewhere.
discovering which variables are exported by what module isn't as well-known as use of Get-Command is in order to determine what module a given function, cmdlet, or alias comes from. This is because (a) modules that export variables are rare and users generally don't expect it, and (b) the Get-Variable cmdlet - which can tell you what module a variable is defined in - isn't often used in practice.
To see which definitions a given module exports, pass -Verbose to the Import-Module call that imports it. Additionally, pass -Force in order to force re-loading of an already imported module.

Can I hide a definition from Import-Module?

I have a file with a group of function definitions and variable assignments, some of which are only used to help other functions or definitions. Is it possible to hide these from the Import-Module command such that the user cannot see them?
Alternatively, is there a method for manually declaring exports?
It seems you haven't been exporting your module members manually. Use the Export-ModuleMember Cmdlet to do so. If you do not export a member, it won't be seen by the user.
To export a function/cmdlet/alias/variable, write
Export-ModuleMember -Function MyFunction -Alias MyAlias -Cmdlet MyCmdlet -Variable MyVariable
at the bottom of your module file.

A Variable from a module will not be exported

I have a PowerShell Modul: SQLModul.psm1 with a variable:
$logFile="C:\logfiles\SQLLog.txt".
I also have a manifest for this module: SQLModul.psd1 where I state:
VariablesToExport = '*'
When I Import this module: import-modul SQLModul all Functions but not this variable will be exported.
Thansk for your help.
Think of the module manifest as a facade or proxy in front of the module(s) it load(s) - the manifest exporting * variables will only affect whatever variables were already exported from a nested psm1 or managed module.
Place Export-ModuleMember -Variable list,of,variable,names at the bottom of your psm1 file, in your case:
# define module functions, variables etc.
# ...
Export-Module -Variable logfile
Placing Export-ModuleMember -Variable * in the psm1 should also work, although that's probably a pretty bad idea, and is generally considered an anti-pattern

Powershell - Export-ModuleMember doesn't work

I'm new with PowerShell and I've just started to create my own module.
I created the script & manifest files and placed them into the directory C:\Program Files\WindowsPowerShell\Modules\ who is one listed of the $env:PSModulePath command.
I can import and remove the module as well but it's not recognized as an applet or command when I try to call one of the function.
I use the Import-Module command :
Import-Module MyModule
My script file is something like this :
function MyFunction() {}
Export-ModuleMember -Function *
And my manifest looks like this :
FunctionsToExport = '*'
Do I need to write all of the exported functions into the manifest ?
Thank you for your help.
Ok so after a few hours I found the answer :
ExportedCommand empty custom module PowerShell
You need to put RootModule = 'MyModule.psm1' into the manifest.
Have a nice day !
for me, the following ( based on Mica's answer) worked:
Change the RootModule as Mica suggested in the psd1 file to the psm1 file.
Use the Import-Module -Force MyModule command
(with the '-Force' flag:
"This parameter causes a module to be loaded, or reloaded, over top of the current one." as said at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/import-module?view=powershell-7.1#examples)
To export everything:
Module manifest (.psd1):
FunctionsToExport = '*'
Module script file (.psm1):
Export-ModuleMember -Function *
To restrict what gets exported:
Module manifest (.psd1):
FunctionsToExport = 'Foo', 'Bar'
Module script file (.psm1):
Export-ModuleMember -Function Foo, Bar
Note that if quotes are not used in this specific way, it will not work, and no error will be reported.

Using Export-ModuleMember to export a script as a function or alias

I have a module in which I am trying to export a function/alias. The item I am trying to export is a script file foo.ps1 which does not declare a named function, but I want to export it as available in the session as being able to invoke foo. Is this supported? If so, how do I configure the Export-ModuleMember call?
Here is a snippet from the very top of the file:
[CmdletBinding(DefaultParameterSetName='default')]
param(
[Parameter(Position=0,Mandatory=$false,HelpMessage="You must specify which task to execute.")]
[ValidateSet('install','update', 'uninstall', 'outdated', 'init', 'help', '?')]
....
There are no function definitions. Rather the function name is the file name.
I was able to load the script contents into a ScriptBlock and create a function out of it.
[string]$content = Get-Content $here\foo.ps1 -Delimiter ([Environment]::NewLine)
$block = [ScriptBlock]::Create($content)
Invoke-Expression "function foo { $block }"
Export-ModuleMember -function foo
This works, but is far from ideal.
Not tested, but try to dot-source the script in the module psm1 file and the use Export-ModuleMember to export its function(s).
try this
PS II> # UNTESTED
PS II> $excmd = gcm G:\inventory.ps1
PS II> set-item function:$($excmd.name.replace('.','_')) -val $excmd.scriptblock
PS II> inventory_ps1