Reputation: 3132
I am trying to package multiple modules in one manifest module with PowerShell 4.0. Basically, I have 3 Setup Modules that do some stuff. I package these three using my manifest module. I then export only some functions and variables from all three modules. However, only my functions are callable from outside, my variables are nowhere to be seen. Can anyone help me out here? I basically followed this guide.
Here is my code:
Setup.ps1 (Startup-Script):
$currentDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$setupScriptPath = $currentDirectory + "\Setup.psd1"
Import-module $setupScriptPath
# This has to be set for every environment
$firstVariable # Not defined?
# $secondVariable= "http://url/"
# $thirdVariable= "http://url/"
Start-FirstSetup
Remove-Module -Name Setup
Setup.psd1 (Manifest module):
#
# Module manifest for module 'Setup'
#
# Generated by: Name
#
# Generated on: 1/7/2017
#
@{
# Script module or binary module file associated with this manifest.
# RootModule = ''
# Version number of this module.
ModuleVersion = '1.0'
# ID used to uniquely identify this module
GUID = 'guid'
# Author of this module
Author = 'Author name'
# Company or vendor of this module
CompanyName = 'Company'
# Copyright statement for this module
Copyright = '(c) 2017 Company'
# Description of the functionality provided by this module
Description = 'Starts three setups.'
# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''
# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('FirstSetup.psm1', 'SecondSetup.psm1', 'ThirdSetup.psm1')
# Functions to export from this module
FunctionsToExport = @('Start-FirstSetup', 'Start-SecondSetup', 'Start-ThirdSetup')
# Cmdlets to export from this module
CmdletsToExport = '*'
# Variables to export from this module
VariablesToExport = @('firstVariable', 'secondVariable', 'thirdVariable')
# Aliases to export from this module
AliasesToExport = '*'
# List of all modules packaged with this module
ModuleList = @('FirstSetup.psm1', 'SecondSetup.psm1', 'ThirdSetup.psm1')
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess
# PrivateData = ''
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
FirstSetup.psm1 (First Module):
$firstVariable # This is not getting exported. Why?
function Start-FirstSetup{
Register-FirstVariables
echo "First setup started..."
}
Second and third setups: Same as first, only varables and functions are named firstVariable, thirdVariable, Start-SecondSetup, etc.
So my specific problem is, when I try to access $firstVariable
from Setup.psm1, I get an error that it's not defined. But I marked it for export in my manifest module. So what did I miss here? When Start-FirstSetup
is called, it goes through without any problem and I can even debug my module, but even then my $firstVariable is undefined.
Upvotes: 1
Views: 775
Reputation: 200203
You need to do 3 things to get a variable exported from a module:
Define the variable explicitly:
New-Variable -Name firstvariable
An implicit definition ($firstvariable
) does not suffice.
Export the variable in the module (the .psm1 file):
Export-ModuleMember -Variable firstvariable
Define the exported variables in the manifest (the .psd1 file). A wildcard should normally suffice here:
VariablesToExport = '*'
but you can also provide a list:
VariablesToExport = @('firstvariable', 'secondvariable', 'thirdvariable')
This setting is basically a filter for defining which of the exported variables will actually be exposed. If a variable does not have a match here it will not be exported from the module, even if it's exported in a .psm1 file.
Upvotes: 1