PowerShell二进制模块导入无导出命令问题解决及文档方案确认
我有一个名为PSInvestigator的PowerShell C#二进制模块,目录结构为PSInvestigation/PSInvestigator/,包含PSIUptime.cs、PSIPartitions.cs等文件。
我可以通过以下步骤在PowerShell中正常运行并执行该C# .dll:
dotnet build ipmo /home/deca/RiderProjects/PSInvestigator/PSInvestigator/bin/Debug/net8.0/PSInvestigator.dll
执行后可以正常运行Get-PSIVersion等命令。但现在我想为C# PowerShell cmdlets添加帮助文档,看起来需要添加.psd1文件、包含xml帮助文件的en-us文件夹以及psm1文件。当前目录结构如下:
PS ~/RiderProjects/PSInvestigator/PSInvestigator> tree . . ├── bin │ └── Debug │ └── net8.0 │ ├── PSInvestigator.deps.json │ ├── PSInvestigator.dll │ └── PSInvestigator.pdb ├── en-us │ └── PSIPartitions-Help.xml ├── PSICmdline.cs ├── PSInvestigator.csproj ├── PSInvestigator.psd1 ├── PSInvestigator.psm1 ├── PSIPartitions.cs ├── PSIUptime.cs └── PSIVersion.cs
我使用NewModuleManifest PowerShell cmdlet生成了.psd1文件,并添加了以下配置行以导出所有函数并指定.psm1文件(不确定是否需要.psm1文件):
# Script module or binary module file associated with this manifest. RootModule = 'PSInvestigator.psm1' # Assemblies that must be loaded prior to importing this module RequiredAssemblies = @("./bin/Debug/net8.0/PSInvestigator.dll") # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = '*' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = '*' # Variables to export from this module VariablesToExport = '*' # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. AliasesToExport = '*'
我在.psm1文件中添加了以下内容,但无论是否使用.psm1文件导入模块,都无法解决问题:
# Get the directory of the script $scriptDirectory = $PSScriptRoot # Load the assembly $assemblyPath = Join-Path -Path $scriptDirectory -ChildPath "bin/Debug/net8.0/PSInvestigator.dll" Add-Type -Path $assemblyPath # Exported cmdlets and functions Export-ModuleMember -Function * -Cmdlet *
现在当我运行import-module ./PSInvestigator.psd1并执行Get-Module时,看不到任何导出命令:
ModuleType Version PreRelease Name ExportedCommands ---------- ------- ---------- ---- ---------------- Script 0.0.1 PSInvestigator
但直接通过完整路径/home/deca/RiderProjects/PSInvestigator/PSInvestigator/bin/Debug/net8.0/PSInvestigator.dll导入模块时一切正常。请问如何正确导入PowerShell模块并配置其文档?
我在.psd1的RootModule部分添加了dll的路径:
RootModule = './bin/Debug/net8.0/PSInvestigator.dll'
之后问题就解决了。我确认PlatyPS是编写PowerShell二进制文件文档的理想工具。
内容的提问来源于stack exchange,提问作者decabytes

