gpt4 book ai didi

powershell - 如何从模块中检索导出的变量?

转载 作者:行者123 更新时间:2023-12-04 01:03:30 24 4
gpt4 key购买 nike

如何在导入后检索从我的模块导出的变量?

# in mymodule.psm1
$myVar = New-Object VarClass
#assume a function or two here.
Export-Modulemember -Function * -Variable *

# in myScript.ps1 after module import
# how do I reference this variable?
$myScriptVar = $myVar

很抱歉这个基本问题,我找不到任何关于这个简单问题的例子。

最佳答案

导出模块成员 cmdlet 时,您必须明确声明函数 变量。我的问题是我也有一个函数。

来自 powershell documentation :

If you want to export a variable, in addition to exporting the functions in a module, the Export-ModuleMember command must include the names of all of the functions and the name of the variable.

在这种情况下,我的模块中必须包含以下内容:

Export-Modulemember -Function Verb-Noun -Variable myVar 

我卡住了,因为 list 中也是如此,如果同时导出两者,则不能使用通配符。

# Functions to export from this module
FunctionsToExport = 'Verb-Noun'

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

关于powershell - 如何从模块中检索导出的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38253689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com