简体   繁体   English

为什么不能使用 VariablesToExport 导出 PowerShell 模块中的变量成员?

[英]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 :警告

  • Once a *.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 ) 条目- FunctionsToExportAliasesToExportVariablesToExportCmdletsToExport仅适用于从程序集导出的二进制cmdlet ) -进一步过滤这些候选导出,过滤结果是模块的有效导出。

    • Note: As the comments in newly generated module manifests advise, wildcard patterns such as '*' , 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

    • The Importing... lines are the exported definitions being imported into the caller's scope. Importing...行是将导出的定义导入调用者的 scope。
    • If the *.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.

相关问题 为什么我的 PowerShell 模块不能正确导出成员,为什么不能导出嵌套模块? - Why isn't my PowerShell module exporting members correctly and why isn't it exporting Nested Modules? 为什么我不能将此对象导出到csv - powershell why can't I export this object to csv 如何从Powershell模块函数导出变量? - How to export a variable from a powershell module function? 为什么ASM powershell模块无法访问某些订阅? - Why can't the ASM powershell module access some subscriptions? 为什么 PowerShell 不能加载文件或程序集来运行 Azure PowerShell 模块? - Why can't PowerShell load file or assembly to run Azure PowerShell Module? 为什么我的Azure PowerShell任务脚本可以使用find-module而不是import-module来找到模块​​? - Why can my Azure PowerShell Task script find a module using find-module but not with import-module? 导出所有 Azure AD 组及其成员 (Powershell) - Export all Azure AD Groups and their members (Powershell) powershell 脚本导出所有组和成员以及组成员中的组 - powershell script to export all groups and members and the groups with in the group members 为什么我不能使用 powershell (5.1) 执行远程脚本 - Why can't I execute remote scripts using powershell (5.1) 为什么不能使用PowerShell在if语句中组合变量和函数? - Why can one not combine a variable and a function in an if-statement using PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM