如何使用 Powershell 强制 Nuget Restore?

How to force Nuget Restore using Powershell?

我想强制 NuGet 恢复我的 NuGet 包。转到我的解决方案目录并键入 NuGet restore,即使是最新下载的 NuGet.exe 也不起作用。

here 所述,我可以在 Powershell 提示符下使用 Update-Package 命令执行此操作(因为我不想使用我的 Visual Studio 环境)。

但是,我的Powershell似乎不理解Update-Package,虽然我有一个+5版本:

PowerShell Prompt> Get-Host | Select-Object Version

Version
-------
5.1.19041.1023


PowerShell Prompt> Update-Package -reinstall
Update-Package : The term 'Update-Package' is not recognized as the name of a cmdlet, function,
script file, or operable program.
Check the spelling of the name, or if a path was included, verify that 
the path is correct and try again.
At line:1 char:1
+ Update-Package -reinstall
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Update-Package:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如何才能在我的 Powershell 中成功启动 Update-Package

提前致谢

我不使用 Update-Package,但使用以下:

 & "C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin\MSBuild.exe" /t:restore  C:\code\projectdirectory

您可能需要将路径更改为 MSBuild.exe

不是本机 PowerShell,但它可以工作。

标准 powershell 不提供 NuGet 恢复功能。

如果你参考https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-visual-studio, and https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore,你会看到有5种使用NuGet的方法。

  • Visual Studio(和MSBuild.exe)
  • Visual Studio 对于 Mac
  • dotnet CLI
  • NuGet.exe CLI
  • 包管理器控制台(在 Visual Studio 中)

powershell 中没有内置功能。

您唯一的选择是在上述工具之上构建。

  • 调用dotnet.exe恢复
  • 调用nuget.exe恢复
  • 调用msbuild.exe恢复。