1

TL;博士

在 Linux (Fedora 32) 上运行 PowerShell 7.0.2,模块加载,但没有导出任何函数。

细节

我在 Linux 上编写了一个非常基本的测试模块,它不会导出任何功能。我已经在 Windows PowerShell 上尝试过了,它确实有效。我错过了什么?

在 Windows 上,.psd1 文件的位置无关紧要,我希望这同样适用于 Linux,但要确保模块位于 $env:PSModulePath: /home/john/ 中列出的路径之一中。 local/share/powershell/Modules/testmodule/testmodule.psd1`

我曾经New-ModuleManifest创建psd1文件:

New-ModuleManifest -Guid ((New-Guid).Guid) -CompanyName "SomeCompany" -Author "John" -Description "Test" -Path ./testmodule.psd1

并完成其余部分,RootModuleCompatiblePSEditionsFunctionsToExport

#
# Module manifest for module 'testmodule'
#
# Generated by: John
#
# Generated on: 08/07/2020
#

@{

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

# Version number of this module.
ModuleVersion = '0.0.1'

# Supported PSEditions
CompatiblePSEditions = @('Core', 'Desktop')

# ID used to uniquely identify this module
GUID = '372adc51-3efb-4854-9deb-39ea0f9a41eb'

# Author of this module
Author = 'John'

# Company or vendor of this module
CompanyName = 'SomeCompany'

# Copyright statement for this module
Copyright = '(c) John. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Test'

# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# 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 = @('Get-HelloWorld')

# 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 = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

    PSData = @{

        # Tags applied to this module. These help with module discovery in online galleries.
        # Tags = @()

        # A URL to the license for this module.
        # LicenseUri = ''

        # A URL to the main website for this project.
        # ProjectUri = ''

        # A URL to an icon representing this module.
        # IconUri = ''

        # ReleaseNotes of this module
        # ReleaseNotes = ''

        # Prerelease string of this module
        # Prerelease = ''

        # Flag to indicate whether the module requires explicit user acceptance for install/update/save
        # RequireLicenseAcceptance = $false

        # External dependent modules of this module
        # ExternalModuleDependencies = @()

    } # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

我还创建了以下非常基本的testmodule.psm1文件:

function Get-HelloWord($Name)
{
  Write-Host "Hello '$Name'"
}

Export-ModuleMember -Function *

运行时Import-Module,不导出任何函数:

PS /> Import-Module -FullyQualifiedName "/home/john/.local/share/powershell/Modules/testmodule/testmodule.psd1" -force -verbose
VERBOSE: Loading module from path '/home/john/.local/share/powershell/Modules/testmodule/testmodule.psd1'.
PS /> 

并尝试使用 Get-Command 列出 cmdlet,但什么也不返回:

PS /> Get-Command -Module testmodule
PS /> 

有趣的是,我可以用制表符完成模块名称,但是,Get-Module -ListAvailable不会返回对testmodule.

从 PowerShell Gallery 安装的模块可以Az正常工作,所以我一定是做错了什么。我以为是CompatiblePSEditions,但添加core没有任何区别。

如果有人能指出我正确的方向,我将不胜感激。

TIA

4

1 回答 1

0

这很令人困惑,我从反复试验中学到了我要说的内容。我不确定它是否在任何地方说明。要导出的函数只是使模块中的函数可用。要从您的模块中导出它,您还应该将它添加到 NestedModules。请参见下面的示例。

编辑

我上面所说的可能只有在编写带有 manifest 的模块时才需要*.psd1。我相信你也可以摆脱清单,功能就会出现。然而,没有提供清单有一些缺点。

测试模块.psd1

#
# Module manifest for module 'testmodule'
#
# Generated by: John
#
# Generated on: 08/07/2020
#

@{

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

    # Version number of this module.
    ModuleVersion = '0.0.1'

    # Supported PSEditions
    CompatiblePSEditions = @('Core', 'Desktop')

    # ID used to uniquely identify this module
    GUID = '372adc51-3efb-4854-9deb-39ea0f9a41eb'

    # Author of this module
    Author = 'John'

    # Company or vendor of this module
    CompanyName = 'SomeCompany'

    # Copyright statement for this module
    Copyright = '(c) John. All rights reserved.'

    # Description of the functionality provided by this module
    Description = 'Test'

    # Minimum version of the PowerShell engine required by this module
    # PowerShellVersion = ''

    # Name of the PowerShell host required by this module
    # PowerShellHostName = ''

    # Minimum version of the PowerShell host required by this module
    # PowerShellHostVersion = ''

    # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
    # DotNetFrameworkVersion = ''

    # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
    # ClrVersion = ''

    # Processor architecture (None, X86, Amd64) required by this module
    # ProcessorArchitecture = ''

    # Modules that must be imported into the global environment prior to importing this module
    # RequiredModules = @()

    # Assemblies that must be loaded prior to importing this module
    # RequiredAssemblies = @()

    # Script files (.ps1) that are run in the caller's environment prior to importing this module.
    # ScriptsToProcess = @()

    # Type files (.ps1xml) to be loaded when importing this module
    # TypesToProcess = @()

    # Format files (.ps1xml) to be loaded when importing this module
    # FormatsToProcess = @()

    # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
    NestedModules = @('Get-HelloWorld')

    # 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 = @()

    # DSC resources to export from this module
    # DscResourcesToExport = @()

    # List of all modules packaged with this module
    # ModuleList = @()

    # List of all files packaged with this module
    # FileList = @()

    # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
    PrivateData = @{

        PSData = @{

            # Tags applied to this module. These help with module discovery in online galleries.
            # Tags = @()

            # A URL to the license for this module.
            # LicenseUri = ''

            # A URL to the main website for this project.
            # ProjectUri = ''

            # A URL to an icon representing this module.
            # IconUri = ''

            # ReleaseNotes of this module
            # ReleaseNotes = ''

            # Prerelease string of this module
            # Prerelease = ''

            # Flag to indicate whether the module requires explicit user acceptance for install/update/save
            # RequireLicenseAcceptance = $false

            # External dependent modules of this module
            # ExternalModuleDependencies = @()

        } # End of PSData hashtable

    } # End of PrivateData hashtable

    # HelpInfo URI of this module
    # HelpInfoURI = ''

    # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
    # DefaultCommandPrefix = ''

    }
Import-Module ./testmodule.psm1

Get-Command -Module testmodule                

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-HelloWord                                      0.0        testmodule
于 2020-08-26T17:32:01.177 回答