[英]How to export variables from a PowerShell module
I've defined a variable in a psm1
file but when I try to access it in another script, after importing the module, I'm not seeing the value set in the psm1
file.我在psm1
文件中定义了一个变量,但是当我尝试在另一个脚本中访问它时,在导入模块后,我没有看到psm1
文件中设置的值。
globals.psm1全局变量.psm1
$blah = "hello world"
my-script.ps1我的脚本.ps1
Import-Module "$PSScriptRoot\globals.psm1" -Force -Verbose
Write-Output "blah: ${blah}"
output output
PS C:\blah>.\my-script.ps1 PS C:\blah>.\my-script.ps1
VERBOSE: Loading module from path 'C:\blah\globals.psm1'.详细:从路径“C:\blah\globals.psm1”加载模块。
blah: ''废话:''
I thought all variables get exported by default.我认为默认情况下会导出所有变量。 I must be interrupting this wrong:我一定是打断了这个错误:
Specifies the variables that the module exports to the caller's session state.指定模块导出到调用者的 session state 的变量。 Wildcard characters are permitted.允许使用通配符。 By default, all variables
('*')
are exported默认情况下,所有变量('*')
都被导出
source: MSFT Docs -> How to write a PowerShell module manifest来源: MSFT Docs -> 如何编写 PowerShell 模块清单
( CTRL + F
on 'VariablesToExport' to find the quoted text) ( 'VariablesToExport' 上的CTRL + F
以查找引用的文本)
And yes, if I export the variable, I can access it but the documentation says: 'By default, all varialbes ('*')
are exported so what am I doing wrong or misunderstanding?是的,如果我导出变量,我可以访问它,但文档说:'默认情况下,所有变量('*')
都被导出,所以我在做什么错或误解?
globals.psm1全局变量.psm1
$blah = "hello world"
Export-ModuleMember -Variable blah
You aren't using a module manifest (a companion .psd1
file whose RootModule
entry points to your .psm1
file in the case of script modules).您没有使用模块清单(在脚本模块的情况下,它的RootModule
条目指向您的.psm1
文件的伴随.psd1
文件)。
If a module consists only of a .psm1
file , and that file contains no Export-ModuleMember
calls, the following rule applies:如果模块仅包含.psm1
文件,并且该文件不包含Export-ModuleMember
调用,则适用以下规则:
Only functions and aliases are automatically exported .只有函数和别名会自动导出。
Conversely, this means: in order to also export variables , you must use an Export-ModuleMember
call - and if you do, the slate is wiped clean, so to speak, and you must explicitly specify all definitions you want to export (in the simplest case, use Export-ModuleMember -Function * -Alias * -Variable *
).相反,这意味着:为了也导出variables ,您必须使用Export-ModuleMember
调用 - 如果您这样做了,那么可以这么说,您必须明确指定要导出的所有定义(在最简单的情况,使用Export-ModuleMember -Function * -Alias * -Variable *
)。
Also, be sure to place this call at the end of your .psm1
file, to ensure that all definitions to export have already been defined.此外,请务必将此调用放在.psm1
文件的末尾,以确保已定义所有要导出的定义。
Caveat , if a manifest ( .psd1
) is used:警告,如果使用清单( .psd1
):
The manifest's *ToExport
keys apply on top of what the .psm1
file - implicitly or explicitly - exports, ie you can use it to further narrow what is to be exported, by explicitly enumerating the elements to export, which not only makes the module more self-describing, but also helps performance when PowerShell auto-discovers the commands in available, but not-(yet)-imported modules.清单的*ToExport
键适用于.psm1
文件 - 隐式或显式 - 导出的内容,即您可以使用它来进一步缩小要导出的内容,通过显式枚举要导出的元素,这不仅使模块更自我描述,但在 PowerShell 自动发现可用但(尚未)导入的模块中的命令时也有助于提高性能。
Therefore, if a manifest-based module wants to export variables, it too must have an explicit Export-ModuleMember
call in its .psm1
file, with the manifest potentially narrowing down what variables are ultimately to be exported.因此,如果基于清单的模块想要导出变量,它也必须在其.psm1
文件中具有显式的Export-ModuleMember
调用,清单可能会缩小最终要导出的变量的范围。
Generally, exporting variables from modules is best avoided , because:一般来说,最好避免从模块中导出变量,因为:
it increases the risk of name collisions with variables of the same name defined elsewhere.它增加了与其他地方定义的同名变量发生名称冲突的风险。
discovering which variables are exported by what module isn't as well-known as use of Get-Command
is in order to determine what module a given function, cmdlet, or alias comes from.发现哪些变量由哪个模块导出并不像使用Get-Command
那样广为人知,目的是确定给定 function、cmdlet 或别名来自哪个模块。 This is because (a) modules that export variables are rare and users generally don't expect it, and (b) the Get-Variable
cmdlet - which can tell you what module a variable is defined in - isn't often used in practice.这是因为 (a) 导出变量的模块很少见,用户通常不期望它,并且 (b) Get-Variable
cmdlet - 它可以告诉您在哪个模块中定义了变量 - 在实践中并不经常使用.
To see which definitions a given module exports, pass -Verbose
to the Import-Module
call that imports it.要查看给定模块导出了哪些定义, -Verbose
传递给导入它的Import-Module
调用。 Additionally, pass -Force
in order to force re-loading of an already imported module.此外,传递-Force
以强制重新加载已导入的模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.