简体   繁体   English

在 PowerShell 脚本中,如何还原包以解决错误:此项目引用此计算机上缺少的 NuGet 包

[英]In PowerShell script, how can I restore packages to resolve error: This project references NuGet package(s) that are missing on this computer

I am tasked with writing a PowerShell script to download the latest source code for a given branch, rebuild it and deploy it.我的任务是编写一个 PowerShell 脚本来下载给定分支的最新源代码,重建它并部署它。 The script I've written, is able to download projects, and in most cases has been able to rebuild them.我编写的脚本能够下载项目,并且在大多数情况下能够重建它们。 But with one web project I get this error:但是在一个网络项目中,我收到了这个错误:

error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.Net.Compilers.2.0.1\build\Microsoft.Net.Compilers.props.

I've researched if PowerShell has a Update-Package command like the one available in the VS command prompt but have been unable to find the equivalent.我已经研究过 PowerShell 是否具有类似于 VS 命令提示符中可用的更新包命令,但无法找到等效命令。

I know there's a cmdlet for Packages but from what I've seen they're used to update a specific package...what I need is to be able to have it download/update all packages referenced in the project.我知道有一个 Packages cmdlet,但据我所知,它们用于更新特定的包……我需要的是能够下载/更新项目中引用的所有包。

Some points that might be of interest...一些可能感兴趣的点......

When I get latest on the solution to a new empty folder, the only thing in the packages folder is Modernizr.2.6.2.当我获得新的空文件夹的最新解决方案时,包文件夹中唯一的东西是 Modernizr.2.6.2。 This is the same whether I'm getting latest in VS or in my PowerShell script.无论我是在 VS 还是在我的 PowerShell 脚本中获得最新版本,这都是一样的。

If I open the solution within VS 2017, I am able to rebuild the solution with no problems.如果我在 VS 2017 中打开解决方案,我可以毫无问题地重建解决方案。 It downloads/installs over a dozen other packages...one of which is the Microsoft.Net.Compilers.props package referred to in the error message.它下载/安装十多个其他包……其中一个是错误消息中提到的 Microsoft.Net.Compilers.props 包。

But if I delete everything and re-download the source code and then through my PowerShell script I call MSBuild to rebuild the solution I get the error mentioned above.但是,如果我删除所有内容并重新下载源代码,然后通过我的 PowerShell 脚本调用 MSBuild 来重建解决方案,则会出现上述错误。 It never seems to download/install the missing packages.它似乎永远不会下载/安装丢失的软件包。

Anyone have any ideas how I can use MSBuild within my PowerShell script to rebuild the project and have it automatically update/install any packages it needs?任何人都知道如何在我的 PowerShell 脚本中使用 MSBuild 来重建项目并让它自动更新/安装它需要的任何包?

Thanks谢谢

I was able to find a solution to my problem on this page : Quickly Restore NuGet Packages With PowerShell我能够在此页面上找到解决我的问题的方法: 使用 PowerShell 快速恢复 NuGet 包

On that page is a script that uses Nuget.exe to download the packages based on the packages.config:在该页面上有一个脚本,它使用 Nuget.exe 下载基于 packages.config 的包:

#This will be the root folder of all your solutions - we will search all children of 
this folder
$SOLUTIONROOT = "C:\Projects\"
#This is where your NuGet.exe is located
$NUGETLOCATION = "C:\Projects\NuGet\NuGet.exe"

Function RestoreAllPackages ($BaseDirectory)
    {
        Write-Host "Starting Package Restore - This may take a few minutes ..."
        $PACKAGECONFIGS = Get-ChildItem -Recurse -Force $BaseDirectory -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq     "packages.config")}
        ForEach($PACKAGECONFIG in $PACKAGECONFIGS)
            {
                Write-Host $PACKAGECONFIG.FullName
                $NugetRestore = $NUGETLOCATION + " install " + " '" + $PACKAGECONFIG.FullName + "' -OutputDirectory '" +     $PACKAGECONFIG.Directory.parent.FullName + "\packages'"
                Write-Host $NugetRestore
                Invoke-Expression $NugetRestore
            }
    }

RestoreAllPackages $SOLUTIONROOT
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

I modified and added this function to my PS script and call it first to download all the packages and that does the job!我修改了这个函数并将其添加到我的 PS 脚本中,并首先调用它来下载所有包,这就完成了工作!

You need to call the restore target of MSBuild to download NuGet packages.您需要调用 MSBuild 的restore目标来下载 NuGet 包。 You can do that by running something like:你可以通过运行类似的东西来做到这一点:

git clone [your repo]
cd [your repo]
msbuild /target:Restore [Your Solution]
msbuild [Your Solution]
function buildVS
{
    param
    (
        [parameter(Mandatory=$true)]
        [String] $path,

        [parameter(Mandatory=$false)]
        [bool] $nuget = $true,
        
        [parameter(Mandatory=$false)]
        [bool] $clean = $true
    )
    process
    {
        $msBuildExe = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe'
        if ($nuget) {
            Write-Host "Restoring NuGet packages" -foregroundcolor green
            & "$($msBuildExe)" "$($path)"  /p:Configuration=Release /p:platform=x64 /t:restore
        }

        if ($clean) {
            Write-Host "Cleaning $($path)" -foregroundcolor green
            & "$($msBuildExe)" "$($path)" /t:Clean /m
        }

        Write-Host "Building $($path)" -foregroundcolor green
        & "$($msBuildExe)" "$($path)" /t:Build /p:Configuration=Release /p:platform=x64

        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从PowerShell脚本引用NuGet软件包文件? - How can I reference NuGet package files from a PowerShell script? 无法在脚本 Powershell 中安装 package NuGet - Can't install the package NuGet in a Script Powershell Artifactory通过PowerShell的身份验证还原nuget软件包 - Artifactory restore nuget packages with authentication from powershell 如何在PowerShell脚本中使用NuGet包? - How to use a NuGet package within a PowerShell script? 如何在PowerShell脚本中运行NuGet命令? - How can I run NuGet commands in a PowerShell script? 如何从 PowerShell 下载项目引用的 Nuget 包? - How to download Nuget packages referenced by project from PowerShell? Powershell: How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line? - Powershell: How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line? 如何在Powershell中查询多个域的计算机帐户 - how can I query to multiple domain's computer account in powershell 如何在正常的PowerShell会话中(不在Visual Studio中)运行Nuget PowerShell cmdlet Install-Package? - How can I run Nuget PowerShell cmdlet Install-Package in normal PowerShell session (not inside Visual Studio)? 如何列出丢失的NuGet软件包 - How to list missing NuGet packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM