首页 > 解决方案 > PowerShell 自定义模块清单不公开声明的函数

问题描述

我创建了一个 PowerShell 模块。该模块公开了 3 个功能。当我直接在没有清单的情况下安装它时,输出将是:

> Import-Module AzureDD
> Get-Module | Where { $_.Name -eq 'AzureDD' }

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        AzureDD                             {New-AzureDDAppPermission, New-AzureDDAppRegistration, Sync-AzureDDStorageContainer}

这是因为我在 psm 文件中的最后一行是:

Export-ModuleMember -Function New-AzureDDAppRegistration, New-AzureDDAppPermission, Sync-AzureDDStorageContainer

现在我想添加版本控制和更多元数据并继续

> New-ModuleManifest -Path .\AzureDD.psd1 -ModuleVersion "2.0"

这会创建一个新文件AzuerDD.psd1。在这里我编辑了很多东西。除了其他更改之外,我还定义了导出的函数,如下所示:

FunctionsToExport = @('New-AzureDDAppPermission', 'New-AzureDDAppRegistration', 'Sync-AzStorageContainer')

我可以成功测试:

> Test-ModuleManifest .\AzureDD.psd1

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   2.0        AzureDD                             {New-AzureDDAppPermission, New-AzureDDAppRegistration, Sync-AzStorageContainer}

但是当我实际导入它时,它不会显示任何导出的命令:

> Import-Module .\AzureDD.psd1
> Get-Module | Where { $_.Name -eq 'AzureDD' }

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   2.0        AzureDD

看看它Manifest与我的第一个片段相比有何变化!Remove-Module AzureDD -Force在我重新导入它之前,我已经确保我一直这样做。

标签: powershell

解决方案


FunctionsToExport就像一个筛子——它只允许嵌套模块通过清单导出它们的功能,但它们仍然必须在某个地方定义

确保脚本模块(文件)在清单中.psm1被指定为RootModule(或至少一个):NestedModule

@{
  # Script module or binary module file associated with this manifest.
  RootModule = 'AzureDD.psm1'

  # ...
}

推荐阅读