Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 无法导出.psm1中的函数_Powershell - Fatal编程技术网

Powershell 无法导出.psm1中的函数

Powershell 无法导出.psm1中的函数,powershell,Powershell,我有一个myFunc.psm1文件,如下所示: $ApiVersion = "201846465" Export-ModuleMember -variable ApiVersion function Get-Something { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Id] ) process { # ... } 当我将其导

我有一个myFunc.psm1文件,如下所示:

$ApiVersion = "201846465"
Export-ModuleMember -variable ApiVersion

function Get-Something {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Id]
    )
    process {
        # ...
}
当我将其导入另一个setup.ps1文件时,我在执行过程中看到:

VERBOSE: Loading module from path 'D:\myFunc.psm1'.
VERBOSE: Importing variable 'ApiVersion'.
VERBOSE: Hi from setup.ps1
当我移除

$ApiVersion = "201846465"
Export-ModuleMember -variable ApiVersion
我可以看到:

VERBOSE: Exporting function 'Get-Something'.
VERBOSE: Importing function 'Get-Something'.
VERBOSE: Hi from setup.ps1

为什么会发生这种情况以及如何修复它?

*.psm1
文件中没有
导出模块成员
调用时,所有函数和别名[1]都会自动导出,但不会导出变量

一旦使用
导出模块成员
调用,自动导出将停用,然后必须明确命名所有要导出的元素,包括函数和别名。

因此:

Export-ModuleMember -Variable ApiVersion -Function Get-Something
确保将
导出模块成员
调用放在文件底部
,以确保要导出的所有元素都已定义,否则将忽略它们



[1] 奇怪的是,在使用
新模块创建的动态模块中,只有函数(不包括别名)会自动导出。

@Daolin:Good point重新放置
导出模块成员
-我已经更新了答案。