You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在默认NTLM认证的Windows域中让Find-Module用PAT基础认证?

让Find-Module/Install-Module使用Basic认证+PAT访问本地NuGet仓库

问题背景

  • 当前无需-Credential参数即可正常调用Find-Module,原因是目标模块存放在Windows域内的本地NuGet仓库,默认采用NTLM认证
  • 现在需要改用PAT(个人访问令牌)访问,必须传入-Credential参数且切换为Basic认证,但遇到配置障碍
  • 已成功配置NuGet源的ValidAuthenticationTypes = basic,nuget.exe可正常使用,但PowerShell的Find-Module/Install-Module命令无法正常工作
  • 问题在Windows PowerShell和PowerShell Core中均存在,当前PS Core的模块版本如下:
C:\Users\p11f70f> get-module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest   7.0.0.0               Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script     1.4.8.1               PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package,…
Script     1.0.0                 Posh-Git                            {Add-PoshGitToProfile, Expand-GitCommand, Format-…
Script     2.2.5                 PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Fin…
Script     2.1.0                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …

解决方案步骤

1. 配置PackageManagement源的认证类型

PowerShellGet依赖PackageManagement管理源,需先将目标NuGet源的认证类型设置为Basic:

# 查看当前所有PackageManagement源
Get-PackageSource

# 修改目标源的ValidAuthenticationTypes为basic(替换<你的仓库名>为实际名称)
Set-PackageSource -Name "<你的仓库名>" -ValidAuthenticationTypes basic -Force

2. 确认并同步PowerShellGet仓库配置

确保PowerShellGet的仓库指向上述配置的源,且信任该仓库:

# 查看当前PowerShellGet仓库
Get-PSRepository

# 如果仓库未标记为可信,执行以下命令(替换<你的仓库名>)
Set-PSRepository -Name "<你的仓库名>" -InstallationPolicy Trusted

3. 创建PAT凭据对象

将PAT转换为PowerShell可识别的凭据对象(部分仓库要求用户名为空或特定值,如Azure DevOps可任意填写用户名,密码为PAT):

# 替换<你的PAT字符串>为实际令牌
$pat = "<你的PAT字符串>"
$securePat = ConvertTo-SecureString $pat -AsPlainText -Force
# 用户名可根据仓库要求填写,此处用"dummy"示例
$credential = New-Object System.Management.Automation.PSCredential ("dummy", $securePat)

4. 使用凭据调用Find-Module/Install-Module

调用命令时指定仓库和凭据参数,此时PowerShell会使用Basic认证访问仓库:

# 查找模块示例(替换<你的仓库名>和<目标模块名>)
Find-Module -Repository "<你的仓库名>" -Name "<目标模块名>" -Credential $credential

# 安装模块示例
Install-Module -Repository "<你的仓库名>" -Name "<目标模块名>" -Credential $credential -Force

注意事项

  • 如果仍出现认证问题,可尝试清除已缓存的凭据:Remove-Item "$env:APPDATA\Microsoft\Windows\Credentials\*" -Force(注意:此操作会清除所有Windows凭据,谨慎执行)
  • 部分本地NuGet仓库可能需要额外配置允许Basic认证,需确认仓库端的设置是否开启Basic认证支持

内容的提问来源于stack exchange,提问作者mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:01:06