# NuGet 安装脚本 - 解决 SSL 问题 Write-Host "=== 安装 NuGet ===" -ForegroundColor Green # 设置 TLS 1.2 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # 检查是否已安装 $nugetExists = Get-Command nuget -ErrorAction SilentlyContinue if ($nugetExists) { Write-Host "NuGet 已安装: $($nugetExists.Source)" -ForegroundColor Green exit 0 } # 下载 NuGet Write-Host "下载 NuGet CLI..." -ForegroundColor Yellow $downloadUrls = @( "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", "https://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe", "http://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe" ) $downloaded = $false foreach ($url in $downloadUrls) { try { Write-Host "尝试下载: $url" $webClient = New-Object System.Net.WebClient $webClient.DownloadFile($url, "C:\nuget.exe") $downloaded = $true Write-Host "下载成功!" -ForegroundColor Green break } catch { Write-Host "下载失败: $($_.Exception.Message)" -ForegroundColor Red continue } } if (-not $downloaded) { Write-Host "所有下载都失败了,尝试使用 Chocolatey..." -ForegroundColor Yellow try { choco install nuget.commandline -y if (Get-Command nuget -ErrorAction SilentlyContinue) { Write-Host "通过 Chocolatey 安装成功!" -ForegroundColor Green exit 0 } } catch { Write-Host "Chocolatey 安装也失败了: $($_.Exception.Message)" -ForegroundColor Red } Write-Host "NuGet 安装失败,但继续构建..." -ForegroundColor Yellow exit 0 } # 验证安装 if (Test-Path "C:\nuget.exe") { $env:PATH = "C:\;$env:PATH" Write-Host "NuGet 安装完成" -ForegroundColor Green # 测试 try { $version = & "C:\nuget.exe" help | Select-String -Pattern "NuGet Version" | Select-Object -First 1 Write-Host "版本信息: $version" -ForegroundColor Green } catch { Write-Host "NuGet 可用,但版本检查失败" -ForegroundColor Yellow } } else { Write-Host "NuGet 文件不存在" -ForegroundColor Red }