2

我正在使用 PowerShell 5.0 并尝试编写一个多文件模块。
模块的文件夹结构如下所示:

/FooModule
--- FooModule.psd1
--- FooModule.psm1
--- AnotherScriptFile.ps1

清单文件 FooModule.psd1 自定义如下:

@{
    ModuleVersion = '0.0.1'
    GUID = ...
    Author = ...
    CompanyName = ..
    Copyright = ...
    Description = ...
    PowerShellVersion = '5.0'
    ClrVersion = '4.0'
    RootModule = "FooModule.psm1"
    FunctionsToExport = '*'
    CmdletsToExport = '*'
    VariablesToExport = '*'
    AliasesToExport = '*'
    # HelpInfoURI = ''
    # DefaultCommandPrefix = ''
}

我把这些放在 FooModule.psm1 的开头:

Push-Location $PSScriptRoot
.\AnotherScriptFile.ps1
Pop-Location

AnotherScriptFile.ps1 看起来像这样:

Write-Host "Imported!"
function Get-FooFunction($val)
{
    return "Foo Bar";
}

我认为,当我导入模块时,它会将 AnotherScriptFile.ps1 的函数和变量读入模块的范围,然后导出。可悲的是,在我Import-Module .\FooModule.psm1在模块的文件夹中导入模块后,调用Get-FooFunction给了我一个错误Get-FooFunction,说不存在,'Write-Host "Imported!"但被调用了。

使这项工作的正确方法是什么?

4

1 回答 1

2

You can add the scripts in the ScriptsToProcess array within your module manifest:

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = @('.\AnotherScriptFile.ps1')
于 2016-07-05T10:40:14.720 回答