我正在使用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!"
被调用了
使这项工作的正确方法是什么?
答案 0 :(得分:2)
您可以在模块清单中的ScriptsToProcess
数组中添加脚本:
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = @('.\AnotherScriptFile.ps1')