如何使 Install-Module 也安装任何所需的模块?

2024-01-25

我正在开发一个 PS 模块,它依赖于 SqlServer。

这是模块清单文件:

@{
    ModuleVersion = "1.0.19103.11"
    GUID = "59bc8fa6-b480-4226-9bcc-ec243102f3cc"
    Author = "..."
    CompanyName = "..."
    Copyright = "..."
    Description = "..."
    RequiredModules = ""
    ScriptsToProcess = "vsts\config.ps1"
    NestedModules = @( ... )
    PrivateData = @{
        PSData = @{
            ExternalModuleDependencies = @(
                "SqlServer"
            )
        }
    }
}

以下是我用来发布模块的命令:

if (Get-PSRepository $PSRepositoryName -ErrorAction SilentlyContinue)
{
    Unregister-PSRepository $PSRepositoryName
}
Register-PSRepository $PSRepositoryName `
    -SourceLocation $NuGetRepoUrl `
    -PublishLocation $NuGetRepoUrl `
    -InstallationPolicy Trusted `
    -PackageManagementProvider nuget

Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force

问题是,当我安装此模块时,我收到以下警告:

WARNING: The externally managed, dependent module 'SqlServer' is not installed on this computer. To use the current module 'xyz.PS.Core', ensure that its dependent
module 'SqlServer' is installed.

如何让 SqlServer 在安装此软件包时自动安装而没有任何警告。

我尝试设置RequiredModules属性,但是如果构建机器没有安装 SqlServer 模块,那么这将无法首先发布模块。我可以在那里安装它,但是有没有更好的方法而不强迫我们在构建代理上安装软件包?

EDIT 1

因此,我在构建代理上安装了 SqlServer 模块并添加了RequiredModules财产。显示相同的警告。但现在我什至无法导入该模块:

Import-Module : The required module 'SqlServer' is not loaded. Load the module or remove the module from 'RequiredModules' in the file

EDIT 2

我仍然无法让它发挥作用。让我展示所有的组合。在所有情况下,测试机都会not上面安装了 SqlServer 模块。

尝试1

RequiredModules = @(
    @{
        ModuleName = "SqlServer"
        ModuleVersion = "21.1.18068"
    }
)
PrivateData = @{
    PSData = @{
        ExternalModuleDependencies = "SqlServer"
    }
}

Yields

C:\> Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force
PowerShellGet cannot resolve the module dependency 'SqlServer' of the module 'xyz.PS.Core' on the repository 'xyz-QA'. Verify that the dependent module 'SqlServer' is available in the repository 'xyz-QA'. If this dependent module 'SqlServer' is managed externally, add it to the ExternalModuleDependencies entry in the PSData section of the module manifest.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1227 char:17
+                 Publish-PSArtifactUtility -PSModuleInfo $moduleInfo `
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Publish-PSArtifactUtility], InvalidOperationException
    + FullyQualifiedErrorId : UnableToResolveModuleDependency,Publish-PSArtifactUtility
C:\>

尝试2

RequiredModules = @("SqlServer")
PrivateData = @{
    PSData = @{
        ExternalModuleDependencies = "SqlServer"
    }
}

Yields

C:\> Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force
PowerShellGet cannot resolve the module dependency 'SqlServer' of the module 'xyz.PS.Core' on the repository 'xyz-QA'. Verify that the dependent module 'SqlServer' is available in the repository 'xyz-QA'. If this dependent module 'SqlServer' is managed externally, add it to the ExternalModuleDependencies entry in the PSData section of the module manifest.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1227 char:17
+                 Publish-PSArtifactUtility -PSModuleInfo $moduleInfo `
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Publish-PSArtifactUtility], InvalidOperationException
    + FullyQualifiedErrorId : UnableToResolveModuleDependency,Publish-PSArtifactUtility
C:\>

尝试3

PrivateData = @{
    PSData = @{
        ExternalModuleDependencies = "SqlServer"
    }
}

(no RequiredModules)产量:

C:\> Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force
C:\> Get-Module SqlServer -ListAvailable
C:\> Install-Module xyz.PS.Core -Scope CurrentUser -Force -AllowClobber
WARNING: The externally managed, dependent module 'SqlServer' is not installed on this computer. To use the current module 'xyz.PS.Core', ensure that its dependent module 'SqlServer' is installed.
C:\> Get-Module SqlServer -ListAvailable
C:\>

如您所见,发布成功,但安装模块会输出警告。并且没有安装SqlServer。

尝试4

RequiredModules = @(
    @{
        ModuleName = "SqlServer"
        ModuleVersion = "21.1.18068"
    }
)

(没有外部模块依赖项)产量:

C:\> Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force
The specified RequiredModules entry 'SqlServer' in the module manifest 'C:\Users\mkharitonov\AppData\Local\Temp\2144858157\xyz.PS.Core\xyz.PS.Core.psd1' is invalid. Try again after updating this entry with valid values.At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1144 char:27+ ... oduleInfo = Microsoft.PowerShell.Core\Test-ModuleManifest -Path $mani ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\mkhari...ce.PS.Core.psd1:String) [Test-ModuleManifest], DirectoryNotFoundException
    + FullyQualifiedErrorId : Modules_InvalidRequiredModulesinModuleManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand
C:\>

尝试5

RequiredModules = @("SqlServer")

(没有外部模块依赖项)产量:

C:\> Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force
The specified RequiredModules entry 'SqlServer' in the module manifest 'C:\Users\mkharitonov\AppData\Local\Temp\1208648280\xyz.PS.Core\xyz.PS.Core.psd1' is invalid. Try again after updating this entry with valid values.At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1144 char:27+ ... oduleInfo = Microsoft.PowerShell.Core\Test-ModuleManifest -Path $mani ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\mkhari...ce.PS.Core.psd1:String) [Test-ModuleManifest], DirectoryNotFoundException
    + FullyQualifiedErrorId : Modules_InvalidRequiredModulesinModuleManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand
C:\>

所以,我所做的一切都不起作用。到目前为止,Powershell 似乎无法安装所需的模块。

我究竟做错了什么?

EDIT 3

即使我在构建计算机上安装 SqlServer,RequiredModules 属性也不起作用。观察:

RequiredModules = @(
    "SqlServer"
)
PrivateData = @{
    PSData = @{
        ExternalModuleDependencies = "SqlServer"
    }
}

结果是:

C:\> Get-Module SqlServer -ListAvailable


    Directory: C:\Users\mkharitonov\Documents\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     21.1.18102 SqlServer                           {Add-RoleMember, Add-SqlAvailabilityDatabase, Add-SqlAvailabilityGroupListenerStaticIp, Ad...


C:\> Publish-Module -Name $ModuleManifestFile -NuGetApiKey $NuGetApiKey -Repository $PSRepositoryName -Force
PowerShellGet cannot resolve the module dependency 'SqlServer' of the module 'xyz.PS.Core' on the repository 'xyz-QA'. Verify that the dependent module 'SqlServer' is available in the repository 'xyz-QA'. If this dependent module 'SqlServer' is managed externally, add it to the ExternalModuleDependencies entry in the PSData section of the module manifest.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1227 char:17
+                 Publish-PSArtifactUtility -PSModuleInfo $moduleInfo `
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Publish-PSArtifactUtility], InvalidOperationException
    + FullyQualifiedErrorId : UnableToResolveModuleDependency,Publish-PSArtifactUtility
C:\>

Powershell 似乎在我的存储库中查找 SqlServer 模块,而不是在 PSGallery 中。我不明白方法。以下是机器上注册的 PS 存储库:

C:\> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2
xyz-QA                    Trusted             http://devstatic.xyz.com/nugetserver/nuget


C:\>

所以,我真的不明白我该怎么办?


Update:

  • 指定just RequiredModules for intra- 存储库依赖项(不是also PrivateData.PSData.ExternalModuleDependencies)从 PackageManagement v1.4.8.1 / PowerShellGet v2.2.5 开始可能就足够了 - 请参阅下面的第一条评论。

  • 不再依赖于 PackageManagement 模块的 PowerShellGet 重写版本即将推出(截至撰写本文时),名为PS资源获取 https://github.com/PowerShell/PSResourceGet.


注:以下内容为整理形式PackageManagement (OneGet) GitHub 存储库中的讨论 https://github.com/OneGet/oneget/issues/164, 尤其这条评论 https://github.com/OneGet/oneget/issues/164#issuecomment-314508621来自团队成员:

Install-Module适用于同一存储库内的依赖项。
由于依赖模块托管在与要安装的模块不同的存储库上,我建议您将它们发布到您的内部存储库。
如果您不希望将它们发布到您的内部存储库中,您可以将模块保存到您的公共文件共享中,Register-PSRepository -Name local -SourceLocation \server\commonshare,然后首先从文件共享安装这些模块。”

As of 包管理模块 https://www.powershellgallery.com/packages/PackageManagement/1.3.1版本 1.3,我从你的症状中得出结论,上述内容仍然适用,即使评论是从 2017 年中期开始的(我找不到任何官方文档)。

总结一下:

  • 自动安装跨存储库不支持依赖项; the 解决方法是将外部依赖项也发布到您的内部存储库.

  • 通话时Publish-Module, 看似,中列出的模块RequiredModules entry must存在于同一目标仓库中.

    • 也就是说,仅在您的机器上存在此类模块即可not足够的。 (我不清楚他们是否also需要在场 - 他们不应该在场。)
  • 支持自动安装存储库内依赖关系,PrivateData.PSData.ExternalModuleDependencies条目必须列出这些依赖项也——除了RequiredModules - 仅按名称- 见下文。

指定存储库内自动依赖安装的模块依赖(未验证):

# Specify what other modules this module requires to work, using a FQMN,
# (a Fully Qualified Module Name), via a hashtable.
# The 'ModuleVersion' entry version numbers specifies a *minimum* required version 
# number; 'RequiredVersion' specifies an *exact* version, and  
# 'MaximumVersion' specifies a maximum version.
# IMPORTANT:
#   If you publish a module whose manifest has a 'RequiredModules'
#   entry to a repository with Publish-Module, all referenced modules
#   seemingly *must exist in that repository.*
RequiredModules = @( @{ModuleName = 'SqlServer'; ModuleVersion = '21.1.18068' } )


PrivateData = @{
  PSData = @{

    # ... 

    # !! This field is *ancillary* to the more detailed 'RequiredModules' field and
    # !! must reference the *same modules*, but by *names only*,
    # !! in order to automatically install other modules
    # !! *from the same repository* that this module depends on.
    # !! To be safe, specify even a *single* name as an *array*
    # !! (While this is not a general requirement in manifests, 
    # !!  it may be necessary here due to a bug.)
    ExternalModuleDependencies = @('SqlServer')
}

顺便说一句:一个错误New-ModuleManifest目前阻止直接创建PrivateData.PSData.ExternalModuleDependencies entry:

# !! BROKEN as of Windows PowerShell v5.1 / PowerShell Core 6.2.0;
# The inner hashtable ends up represented as 'System.Collections.Hashtable'
New-ModuleManifest someManifest.psd1 -PrivateData @{ 
  PSData = @{
    ExternalModuleDependencies = @('SqlServer')
  }
}

See 这个 GitHub 问题 https://github.com/PowerShell/PowerShell/issues/5922.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使 Install-Module 也安装任何所需的模块? 的相关文章

随机推荐