[英]Why can't export variable members in a PowerShell module using VariablesToExport?
From How to Write a PowerShell Module Manifest I learned that I can export all the variables in the module using VariablesToExport = '*'
.从如何编写 PowerShell 模块清单中,我了解到我可以使用
VariablesToExport = '*'
导出模块中的所有变量。 However, after using it, I found that I could not export any variables.但是,使用后发现无法导出任何变量。
After writing tests to confirm and reading more documentation, I didn't find the cause of the problem.在编写测试以确认并阅读更多文档后,我没有找到问题的原因。 I may have overlooked something important.
我可能忽略了一些重要的事情。 What is going on here?
这里发生了什么?
TestModule.psm1
excerpt: TestModule.psm1
摘录:
0..9 | ForEach-Object { New-Variable -Name "v_$_" -Value $_ }
TestModule.psd1
excerpt: TestModule.psd1
摘录:
@{ModuleVersion = '1.0';RootModule = 'TestModule.psm1';VariablesToExport = '*'}
TestModule.Tests.ps1
: TestModule.Tests.ps1
:
# Confirm that the module has indeed been imported.
$ModuleName | Get-Module | ForEach-Object 'name' | Should -Be $ModuleName
# All variables do not exist.
0..9 | ForEach-Object { "variable:v_$_" | Test-Path | Should -BeFalse }
It is far from obvious, but, as of PowerShell 7.2, in order to export variables from a PowerShell module , listing them in the module manifest 's ( *.psd1
file's) VariablesToExport
entry alone is not enough :这远非显而易见,但是,从 PowerShell 7.2 开始,为了从 PowerShell 模块中导出变量,仅在模块清单的(
*.psd1
文件的) VariablesToExport
条目中列出它们是不够的:
You also need an Export-ModuleMember
-Variable
call in your script module file ( *.psm1
) - be sure to place it at the end of the file , to ensure that all definitions to be exported have already been made.您还需要在脚本模块文件 (
*.psm1
) 中调用Export-ModuleMember
-Variable
- 确保将其放在文件末尾,以确保所有要导出的定义都已完成。
Caveat :警告:
*.psm1
module calls Export-ModuleMember
, all definitions to be exported must be exported explicitly ;*.psm1
模块调用Export-ModuleMember
,所有要导出的定义都必须显式导出; that is, -Function
, -Cmdlet
, and -Alias
arguments must also be specified, as appropriate;-Cmdlet
-Function
-Alias
arguments; in all cases, wildcard *
can be used to specify all definitions of the given type, and more specific wildcard name patterns may be used too.*
可用于指定给定类型的所有定义,也可以使用更具体的通配符名称模式。 Eg, to export all your $v_*
variables, as well as all functions matching the name pattern *-Foo*
, place the following at the bottom of your *.psm1
file:例如,要导出所有
$v_*
变量以及匹配名称模式*-Foo*
的所有函数,请将以下内容放在*.psm1
文件的底部:
Export-ModuleMember -Variable v_* -Function *-Foo*
Important :重要:
With a module that has a module manifest ( *.psd1
) - as is typical - and a script module ( *.psm1
), the export logic is a two -step process :对于具有模块清单 (
*.psd1
) 的模块 - 通常情况下 - 和脚本模块 ( *.psm1
),导出逻辑是一个两步过程:
Candidate export definitions are all those definitions exported from the *.psm1
itself - either implicitly , or explicitly with Export-ModuleMember
statements.候选导出定义是从
*.psm1
本身导出的所有定义-隐式或使用Export-ModuleMember
语句显式导出。
Implicit exports - ie automatic exports performed in the absence of Export-ModuleMember
calls - comprise all functions and aliases , but not variables .隐式导出- 即在没有
Export-ModuleMember
调用的情况下执行的自动导出 - 包括所有函数和别名,但不包括变量。
Curiously, for dynamic modules - in-memory modules created with New-Module
- it is functions only .奇怪的是,对于动态模块——用
New-Module
创建的内存模块——它只是函数。
The export-relevant manifest ( *.psd1
) entries - FunctionsToExport
, AliasesToExport
, VariablesToExport
( CmdletsToExport
only applies to binary cmdlets exported from assemblies ) - further filter down these candidate exports and the filtered results are a module's effective exports.与导出相关的清单 (
*.psd1
) 条目- FunctionsToExport
、 AliasesToExport
、 VariablesToExport
( CmdletsToExport
仅适用于从程序集导出的二进制cmdlet ) -进一步过滤这些候选导出,过滤结果是模块的有效导出。
'*'
, should be avoided in manifests, because they slow command discovery for auto-loading modules down;'*'
,因为它们会减慢自动加载模块的命令发现速度; definitions should be enumerated explicitly, by their full names. Debugging tips :调试提示:
To see which definitions a given module exports, call Import-Module
with the -Verbose
switch.要查看给定模块导出的定义,请使用
-Verbose
开关调用Import-Module
。
Importing...
lines are the exported definitions being imported into the caller's scope. Importing...
行是将导出的定义导入调用者的 scope。*.psm1
file does not call Export-ModuleMember
, you'll also see the two-step process described above in action: Exporting...
lines preceding the Importing...
ones describe the implicit candidate exports.*.psm1
文件没有调用Export-ModuleMember
,您还将看到上述两步过程的实际操作: Exporting...
Importing...
之前的行描述了隐式候选导出。 To force an already imported module to be re-imported after modifying its definition, call Import-Module
with the -Force
switch.要在修改其定义后强制重新导入已导入的模块,请使用
-Force
开关调用Import-Module
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.