![](/img/trans.png)
[英]How to install PowerShell module pushed to Azure Artifacts in Pipelines?
[英]How to install a powershell module within a docker container from an on-prem Azure Artifacts repository using PAT?
我有以下情况:
我可以从容器内卷曲 Azure Artifacts NuGet 存储库:
PS C:\azp> $Uri
https://tfs.xyz.com/tfs/DefaultCollection/_packaging/xyz@Release/nuget/v2
PS C:\azp> $headers
Name Value
---- -----
Authorization Basic ***
PS C:\azp> (curl $Uri -Headers $headers -UseBasicParsing).StatusCode
200
但我无法从中安装模块:
PS C:\azp> $Name
xyz
PS C:\azp> $credential
UserName Password
-------- --------
a System.Security.SecureString
PS C:\azp> Register-PSRepository -Name $Name -SourceLocation $Uri -PublishLocation $Uri -InstallationPolicy Trusted -Credential $credential
PS C:\azp> $Params
Name Value
---- -----
Name xyz.PS.Core
Force True
ErrorAction Stop
PS C:\azp> $RepoParam
Name Value
---- -----
Repository xyz
PS C:\azp> Install-Module @Params @RepoParam -Scope CurrentUser -Credential $credential
WARNING: Cannot access 'https://tfs.xyz.com/tfs/DefaultCollection/_packaging/xyz@Release/nuget/v2'. Are you missing 'Credential' parameter in the cmdlet?
WARNING: Unable to resolve package source 'https://tfs.xyz.com/tfs/DefaultCollection/_packaging/xyz@Release/nuget/v2'.
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'xyz.PS.Core'. Try Get-PSRepository to see all available registered
module repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
PS C:\azp>
$credential
对象包含 PAT 作为密码,但由于我无法指定空用户名,所以我改为提供 'a'。 但这似乎无关紧要。
因此,我不明白在容器内运行时如何从 Azure Artifacts 安装 Powershell 模块。 有可能吗?
聚苯乙烯
我使用的是 Windows 10。交互式容器的基本映像是从此 docker 文件创建的:
FROM mcr.microsoft.com/windows/servercore:ltsc2019
COPY certificates certificates
WORKDIR /azp
COPY test.ps1 .
COPY Token.txt .token
CMD powershell .\test.ps1
其中 test.ps1 是:
Get-ChildItem /certificates | ForEach-Object {
$null = Import-Certificate -FilePath $_.FullName -CertStoreLocation Cert:\LocalMachine\Root
}
编辑 1
PS C:\azp> Get-PSRepository
Name InstallationPolicy SourceLocation
---- ------------------ --------------
xyz Trusted http://tfs.xyz.com:8080/tfs/DefaultCollection/_packaging/xyz@Release/nuget/v2
PSGallery Trusted https://www.powershellgallery.com/api/v2
PS C:\azp> Get-PackageProvider
Name Version DynamicOptions
---- ------- --------------
msi 3.0.0.0 AdditionalArguments
msu 3.0.0.0
NuGet 2.8.5.208 Destination, ExcludeVersion, Scope, SkipDependencies, Headers, FilterOnTag, Contains, AllowPrereleaseVersions, ConfigFile, SkipValidate
PowerShellGet 1.0.0.1 PackageManagementProvider, Type, Scope, AllowClobber, SkipPublisherCheck, InstallUpdate, NoPathUpdate, Filter, Tag, Includes, DscResou...
Programs 3.0.0.0 IncludeWindowsInstaller, IncludeSystemComponent
PS C:\azp>
编辑 2
有一个重要的细节我忘了提及 - 我想使用 PAT 进行身份验证。
缺少“凭据”,此错误表明您传递的 $credential 参数可能有误。
-Credential
参数需要安装模块中记录的PSCredential
。 但是从您发布的 $credential 的输出中,我们知道它不是一个 pscredential 对象。
请尝试下面的脚本来创建一个 Pscredential 。
$secpasswd = ConvertTo-SecureString "PAT" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("username", $secpasswd)
然后使用 pscredential $mycreds 运行Install-Module
命令
Install-Module @Params @RepoParam -Scope CurrentUser -Credential $mycreds
请确保 PAT 是有效的,并且具有包装:读取、写入和管理权限。 您还可以在此处查看详细示例。
确认为 Azure DevOps 2019 中的错误 - https://developercommunity.visualstudio.com/comments/885584/view.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.