我试图在两个PowerShell模块之间创建一个简单的依赖项,但我得到的语法或某些错误。
Module1.psd1
:
@{
RootModule = 'Module1.psm1'
ModuleVersion = '1.0'
GUID = '11111111-1111-1111-1111-111111111111'
Author = 'uw'
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = '*'
AliasesToExport = @()
}
Module2.psd1
:
@{
RootModule = 'Module2.psm1'
ModuleVersion = '1.0'
GUID = '22222222-2222-2222-2222-222222222222'
Author = 'uw'
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = '*'
AliasesToExport = @()
RequiredModules = @(
@{
ModuleName = "Module1";
ModuleVersion = "1.0";
Guid = "11111111-1111-1111-1111-111111111111"
}
)
}
Module2
的模块清单定义Module2
取决于Module1
。
运行Test-ModuleManifest Module2.psd1
时,出现以下错误:
Test-ModuleManifest : The specified RequiredModules entry 'Module1' in the module manifest 'Module2.psd1' is invalid.
Try again after updating this entry with valid values.
答案 0 :(得分:3)
答案 1 :(得分:2)
您的问题启发了 this GitHub issue ,其中建议引入一个选项 restricting 检查模块是否(在语法上) ,而不是是否可以找到并加载所有引用的模块 。
链接的问题主要是与一个相关的 bug 有关的:Test-ModuleManifest
当前忽略了对所需模块的特定版本的依赖-
作为您自己的解决方法的替代方法(首先在本地安装所有必需的模块),以下方法是更简单的权宜之计:
# Run Test-ModuleManifest and collect any errors in variable $errs while
# suppressing immediate error output.
Test-ModuleManifest ./Module1.psd1 -ErrorVariable errs 2>$null
# Remove the errors relating to the 'RequiredModules' key, which we want to ignore.
$errs = $errs | ? { $_.ToString() -notmatch '\bRequiredModules\b' }
# Output any remaining errors.
$errs | % { Write-Error -ErrorRecord $_ }
# Determine success:
# Testing the manifest succeeded, if no errors other than the anticipated
# one relating to 'RequiredModules' occurred.
$ok = $errs.Count -eq 0