【发布时间】:2019-04-15 18:51:43
【问题描述】:
请注意:
C:\> $TestVariable
C:\> $a
C:\> get-command Test-VariableExport
CommandType Name Version Source
----------- ---- ------- ------
Function Test-VariableExport 0.0.0.1 Test
C:\> (get-command Test-VariableExport).ScriptBlock
$script:TestVariable = Get-Date
C:\> Test-VariableExport
C:\> $TestVariable
C:\> function f() { $script:a = Get-Date }
C:\> f
C:\> $a
Monday, April 15, 2019 2:48:59 PM
C:\> f
C:\> $a
Monday, April 15, 2019 2:49:03 PM
C:\>
仅在函数内部起作用时,完全相同的代码 - 变量 a 从函数导出到外部作用域。但是,当函数是模块的一部分时,就像 Test-VariableExport 函数一样 - 导出不起作用。
如何使它适用于模块功能?
【问题讨论】:
-
不。调用 Export-ModuleMember 时该变量不存在。
标签: powershell module scope powershell-module