powershell - 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
在我重新导入它之前,我已经确保我一直这样做。
解决方案
FunctionsToExport
就像一个筛子——它只允许嵌套模块通过清单导出它们的功能,但它们仍然必须在某个地方定义。
确保脚本模块(文件)在清单中.psm1
被指定为RootModule
(或至少一个):NestedModule
@{
# Script module or binary module file associated with this manifest.
RootModule = 'AzureDD.psm1'
# ...
}
推荐阅读
- android - 返回改造响应的实时数据有什么好处
- algorithm - 死锁避免算法
- bash - 检查系统日期
- react-native - Expo ImagePicker在取消时卡在ActivityIndicator上
- scala - 向 SBT 项目添加 Maven 依赖项
- windows - Start-Transcript 的 Powershell 问题
- java - 捆绑未填充
- javascript - 如何在 TypeScript 中输入普通对象
- python-3.x - 当使用 self.__class__() 返回类 Foo 的实例时,为什么 PyCharm 认为类型应该是 Optional[Type[Foo]]?
- python - 使用棉花糖序列化来自 sqlalchemy 的聚合结果的值