如何使用 PAT 从本地 Azure 工件存储库在 docker 容器中安装 powershell 模块?
How to install a powershell module within a docker container from an on-prem Azure Artifacts repository using PAT?
我有以下情况:
- 本地 Azure DevOps Server 2019。
- 在 (1) 中发布到 Azure Artifacts 的内部 Powershell 模块。
- docker 容器需要安装来自 (2) 的模块。
我可以从容器中卷曲 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.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 模块。有可能吗?
P.S.
我正在使用 Windows10。交互式容器的基本图像是从这个 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 参数可能有误。
-Credential
参数需要 PSCredential
,如 Install-Module 中所述。
但是根据您发布的 $credential 的输出,我们知道它不是 pscredential 对象。
请尝试以下脚本 create a Pscredential。
$secpasswd = ConvertTo-SecureString "PAT" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("username", $secpasswd)
然后 运行 Install-Module
使用 pscredential $mycreds
命令
Install-Module @Params @RepoParam -Scope CurrentUser -Credential $mycreds
Please make sure the PAT is valid and have Packaging: Read, write & manage permission. You can also check the detailed example here.
确认为 Azure DevOps 2019 中的错误 - https://developercommunity.visualstudio.com/comments/885584/view.html
我有以下情况:
- 本地 Azure DevOps Server 2019。
- 在 (1) 中发布到 Azure Artifacts 的内部 Powershell 模块。
- docker 容器需要安装来自 (2) 的模块。
我可以从容器中卷曲 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.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 模块。有可能吗?
P.S.
我正在使用 Windows10。交互式容器的基本图像是从这个 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 参数可能有误。
-Credential
参数需要 PSCredential
,如 Install-Module 中所述。
但是根据您发布的 $credential 的输出,我们知道它不是 pscredential 对象。
请尝试以下脚本 create a Pscredential。
$secpasswd = ConvertTo-SecureString "PAT" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("username", $secpasswd)
然后 运行 Install-Module
使用 pscredential $mycreds
Install-Module @Params @RepoParam -Scope CurrentUser -Credential $mycreds
Please make sure the PAT is valid and have Packaging: Read, write & manage permission. You can also check the detailed example here.
确认为 Azure DevOps 2019 中的错误 - https://developercommunity.visualstudio.com/comments/885584/view.html