使用 Powershell 模块清单发布 C# CmdLet

Release C# CmdLet with Powershell Module Manifest

我们有大量的 Powershell 函数,这些函数是用 Powershell 编写的,并使用包含以下行的 Powershell 清单文件发布:

# Script module or binary module file associated with this manifest.
RootModule = 'Module1.psm1'`

其中 Module1.psml 循环并导入我们所有的函数:

#Dot source the files
Foreach($import in @($Public + $Private))
{
    Try
    {
        . $import.fullname
    }
    Catch
    {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

Export-ModuleMember -Function $Public.Basename

我们现在有一个 C# cmdlet 公开了 1 个我们需要作为其一部分发布的函数。 c# cmdlet 在 bin 目录中包含“Cmdlet1.dll”。

使用模块清单发布这个新的 dll 的正确方法是什么,以便当我们导入 Module1 时,我们也可以从 Cmdlet1 获取函数?

感谢您的帮助!

您需要使用清单文件的 NestedModulesCmdletsToExport 属性。

# ...
NestedModules = @('Cmdlet1.dll');
CmdletsToExport = @('CmdletsFromTheDllFile');
# ...

我发现 this 页的文档非常有助于为包含 PowerShellC# CmdLets 的项目创建良好的结构。