Sunday, 15 January 2012

powershell - Invoking functions from nested modules in a script module do not always trigger a module to autoload -



powershell - Invoking functions from nested modules in a script module do not always trigger a module to autoload -

if create manifest module nested modules, exported functions nested modules after first not appear in list of available commands , don't trigger module autoload.

they not appear when run "get-module -listavailable".

only exported functions first nested module appear in list of commands.

if explicitly import module, exported functions available.

in illustration below, update-legacyservices not available until module has been explicitly imported.

the way can create work rename module files end ps1 instead of psm1 , include them in scriptstoprocess, seems bad idea.

module manifest (psd1)

@{ # script module or binary module file associated manifest. # rootmodule = '' # version number of module. moduleversion = '1.0.0.1' # id used uniquely identify module guid = 'c11d6aca-d531-4d06-a732-5fb95113357f' # author of module author = 'luke' # company or vendor of module companyname = '' # copyright statement module copyright = '' # description of functionality provided module # description = 'mybudget developer powershell module' # minimum version of windows powershell engine required module powershellversion = '4.0' # name of windows powershell host required module # powershellhostname = '' # minimum version of windows powershell host required module # powershellhostversion = '' # minimum version of .net framework required module dotnetframeworkversion = '4.5.0' # minimum version of mutual language runtime (clr) required module clrversion = '4.0.30319.18444' # processor architecture (none, x86, amd64) required module # processorarchitecture = '' # modules must imported global environment prior importing module requiredmodules = 'bitstransfer' # assemblies must loaded prior importing module # requiredassemblies = @() # script files (.ps1) run in caller's environment prior importing module. scriptstoprocess = @() # type files (.ps1xml) loaded when importing module # typestoprocess = @() # format files (.ps1xml) loaded when importing module # formatstoprocess = @() # modules import nested modules of module specified in rootmodule/moduletoprocess nestedmodules = @('database\database.psm1', 'build\build.psm1') # functions export module #functionstoexport = '*' # cmdlets export module cmdletstoexport = '*' # variables export module variablestoexport = '*' # aliases export module aliasestoexport = '*' # list of modules packaged module. modulelist = @('database\database.psm1', 'build\build.psm1') # list of files packaged module # filelist = @() # private info pass module specified in rootmodule/moduletoprocess # privatedata = '' # helpinfo uri of module # helpinfouri = '' # default prefix commands exported module. override default prefix using import-module -prefix. # defaultcommandprefix = '' }

module 1 (build\build.psm1)

function update-legacyservices() { echo "update" } export-modulemember -function update-legacyservices

module 2 (database\database.psm1)

function get-backup($directory, $name) { echo "get-backup" } export-modulemember -function get-backup

i had line in .psd1 file

functionstoexport = 'funcfrommainpsm1 funcfromsecondpsm1'

which inferred line generated powershell tools visual studio:

# functions export module functionstoexport = '*'

this caused get-module -listavailable show looked right thing

script 1.0 mymmodule funcfrommainpsm1 funcfromsecondpsm1

but when called funcfromsecondpsm1 i'd "the term 'funcfromsecondpsm1' not recognized...".

so changed export line to

functionstoexport = @('funcfrommainpsm1', 'funcfromsecondpsm1')

and works. can phone call both functions after module loads, whether via autoload or import-module.

i have tried , without both modulelist , filelist set. create no difference.

powershell

No comments:

Post a Comment