从脚本模块中的嵌套模块调用函数并不总是触发模块自动加载 [英] Invoking functions from nested modules in a script module do not always trigger a module to autoload

查看:46
本文介绍了从脚本模块中的嵌套模块调用函数并不总是触发模块自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建一个带有嵌套模块的清单模块,从第一个之后的所有嵌套模块导出的函数不会出现在可用命令列表中,也不会触发模块自动加载.

If I create a manifest module with nested modules, exported functions from all nested modules after the first do not appear in the list of available commands and don't trigger the module to autoload.

当我运行Get-Module -ListAvailable"时它们也不会出现.

They also do not appear when I run "Get-Module -ListAvailable".

只有从第一个嵌套模块导出的函数出现在命令列表中.

Only the exported functions from the first nested module appear in the list of commands.

如果我明确导入模块,则所有导出的函数都可用.

If I explicitly import the module, all exported functions are available.

在下面的示例中,Update-LegacyServices 在显式导入模块之前不可用.

In the example below, Update-LegacyServices is not available until the module has been explicitly imported.

唯一能让我的模块文件重命名为以 ps1 而不是 psm1 结尾并将它们包含在 ScriptsToProcess 中的方法,这似乎是个坏主意.

The only way I can make it work it to rename my module files to end with ps1 instead of psm1 and include them in ScriptsToProcess, which seems like a bad idea.

模块清单 (psd1)

@{

# Script module or binary module file associated with this manifest.
# RootModule = ''

# Version number of this module.
ModuleVersion = '1.0.0.1'

# ID used to uniquely identify this module
GUID = 'c11d6aca-d531-4d06-a732-5fb95113357f'

# Author of this module
Author = 'luke'

# Company or vendor of this module
CompanyName = ''

# Copyright statement for this module
Copyright = ''

# Description of the functionality provided by this module
# Description = 'MyBudget Developer Powershell Module'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '4.0'

# 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 the .NET Framework required by this module
DotNetFrameworkVersion = '4.5.0'

# Minimum version of the common language runtime (CLR) required by this module
CLRVersion = '4.0.30319.18444'

# 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 = 'BitsTransfer'

# 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 =  @('database\Database.psm1', 'build\Build.psm1')

# Functions to export from this module
#FunctionsToExport = '*'

# Cmdlets to export from this module
CmdletsToExport = '*'

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

# Aliases to export from this module
AliasesToExport = '*'

# List of all modules packaged with this module.
ModuleList = @('database\Database.psm1', 'build\Build.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 = ''

}

模块 1 (Build\Build.psm1)

function Update-LegacyServices()
{
    echo "Update"
}

Export-ModuleMember -Function Update-LegacyServices

模块 2 (database\Database.psm1)

Function Get-Backup($directory, $name)
{
    echo "Get-Backup"
}

Export-ModuleMember -Function Get-Backup

推荐答案

我的 .psd1 文件中有这一行

I had this line in my .psd1 file

FunctionsToExport = 'FuncFromMainPsm1 FuncFromSecondPsm1'

这是我从 Powershell Tools for Visual Studio 生成的行中推断出来的:

which was what I inferred from the line generated by Powershell Tools for Visual Studio:

# Functions to export from this module
FunctionsToExport = '*'

这导致我的 Get-Module -ListAvailable 显示正确的东西

This caused my Get-Module -ListAvailable to show what looked like the right thing

Script     1.0        MyMModule                  FuncFromMainPsm1 FuncFromSecondPsm1

但是当我调用 FuncFromSecondPsm1 时,我会得到无法识别术语‘FuncFromSecondPsm1’...".

But when I called FuncFromSecondPsm1 I'd get "The term 'FuncFromSecondPsm1' is not recognized...".

所以我将导出行更改为

FunctionsToExport = @('FuncFromMainPsm1', 'FuncFromSecondPsm1')

现在一切正常.我可以在模块加载后调用这两个函数,无论是通过自动加载还是通过 Import-Module.

And now it all works. I can call both functions after my module loads, whether via autoload or Import-Module.

无论是否设置了 ModuleListFileList,我都试过了.它们没有区别.

I have tried this with and without both ModuleList and FileList set. They make no difference.

这篇关于从脚本模块中的嵌套模块调用函数并不总是触发模块自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆