Created
August 26, 2025 23:27
-
-
Save CristianoRC/ebbc973f1d6603aee609468f885c1389 to your computer and use it in GitHub Desktop.
Winget Script Install all.NET Skds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -Version 5.1 | |
| <# | |
| .SYNOPSIS | |
| Script para instalar múltiplas versões do .NET SDK usando Winget | |
| .DESCRIPTION | |
| Este script usa o Windows Package Manager (Winget) para instalar múltiplas versões do .NET SDK. | |
| Muito mais simples e confiável que o script oficial da Microsoft. | |
| .PARAMETER IncludeLTS | |
| Instala as versões LTS (Long Term Support): .NET 8.0 e 6.0 | |
| .PARAMETER IncludeSTS | |
| Instala as versões STS (Standard Term Support): .NET 9.0 e 7.0 | |
| .PARAMETER IncludeLegacy | |
| Instala versões legadas (.NET 5.0, Core 3.1) | |
| .PARAMETER InstallAll | |
| Instala TODAS as versões disponíveis | |
| .PARAMETER SpecificVersions | |
| Array de versões específicas para instalar (ex: @("8", "7", "6")) | |
| .PARAMETER Force | |
| Força a instalação mesmo se já estiver instalado | |
| .EXAMPLE | |
| .\Install-NetSdk-Winget.ps1 -IncludeLTS -IncludeSTS | |
| .EXAMPLE | |
| .\Install-NetSdk-Winget.ps1 -InstallAll | |
| .EXAMPLE | |
| .\Install-NetSdk-Winget.ps1 -SpecificVersions @("8", "7", "6") | |
| .NOTES | |
| Autor: Assistente AI | |
| Baseado na documentação oficial da Microsoft sobre Winget | |
| Requer Windows 10 1709+ ou Windows 11 | |
| #> | |
| [CmdletBinding()] | |
| param( | |
| [switch]$IncludeLTS, | |
| [switch]$IncludeSTS, | |
| [switch]$IncludeLegacy, | |
| [switch]$InstallAll, | |
| [string[]]$SpecificVersions = @(), | |
| [switch]$Force | |
| ) | |
| # Configurações globais | |
| $ErrorActionPreference = "Continue" # Continuar mesmo se uma versão falhar | |
| # Definir todas as versões disponíveis no Winget | |
| $availableVersions = @{ | |
| LTS = @( | |
| @{ Version = "8"; Description = ".NET 8.0 LTS (Current)" }, | |
| @{ Version = "6"; Description = ".NET 6.0 LTS" } | |
| ) | |
| STS = @( | |
| @{ Version = "9"; Description = ".NET 9.0 STS (Current)" }, | |
| @{ Version = "7"; Description = ".NET 7.0 STS (End of Support)" } | |
| ) | |
| Legacy = @( | |
| @{ Version = "5"; Description = ".NET 5.0 (End of Support)" }, | |
| @{ Version = "3.1"; Description = ".NET Core 3.1 LTS (End of Support)" } | |
| ) | |
| } | |
| function Write-ColoredOutput { | |
| param( | |
| [string]$Message, | |
| [ConsoleColor]$ForegroundColor = "White" | |
| ) | |
| $originalColor = $Host.UI.RawUI.ForegroundColor | |
| $Host.UI.RawUI.ForegroundColor = $ForegroundColor | |
| Write-Host $Message | |
| $Host.UI.RawUI.ForegroundColor = $originalColor | |
| } | |
| function Test-WingetAvailable { | |
| try { | |
| $wingetVersion = winget --version | |
| Write-ColoredOutput "✅ Winget encontrado: $wingetVersion" -ForegroundColor Green | |
| return $true | |
| } | |
| catch { | |
| Write-ColoredOutput "❌ Winget não encontrado. Instale o Windows Package Manager primeiro." -ForegroundColor Red | |
| Write-ColoredOutput "💡 Baixe em: https://aka.ms/getwinget" -ForegroundColor Yellow | |
| return $false | |
| } | |
| } | |
| function Install-DotNetSDK { | |
| param( | |
| [string]$Version, | |
| [string]$Description, | |
| [bool]$ForceInstall = $false | |
| ) | |
| $packageId = "Microsoft.DotNet.SDK.$Version" | |
| Write-ColoredOutput "🚀 Instalando $Description ($packageId)..." -ForegroundColor Yellow | |
| try { | |
| $installArgs = @("install", $packageId, "--silent") | |
| if ($ForceInstall) { | |
| $installArgs += "--force" | |
| } | |
| $result = & winget @installArgs | |
| $exitCode = $LASTEXITCODE | |
| if ($exitCode -eq 0) { | |
| Write-ColoredOutput "✅ $Description instalado com sucesso!" -ForegroundColor Green | |
| return $true | |
| } | |
| elseif ($exitCode -eq -1978335189) { # Package already installed | |
| Write-ColoredOutput "ℹ️ $Description já está instalado." -ForegroundColor Blue | |
| return $true | |
| } | |
| else { | |
| Write-ColoredOutput "❌ Erro ao instalar $Description (Exit Code: $exitCode)" -ForegroundColor Red | |
| return $false | |
| } | |
| } | |
| catch { | |
| Write-ColoredOutput "❌ Erro ao instalar $Description : $_" -ForegroundColor Red | |
| return $false | |
| } | |
| } | |
| function Show-AvailablePackages { | |
| Write-ColoredOutput "🔍 Pesquisando pacotes .NET SDK disponíveis no Winget..." -ForegroundColor Cyan | |
| try { | |
| winget search "Microsoft.DotNet.SDK" | |
| } | |
| catch { | |
| Write-ColoredOutput "⚠️ Não foi possível pesquisar pacotes no Winget." -ForegroundColor Yellow | |
| } | |
| } | |
| function Show-InstalledVersions { | |
| Write-ColoredOutput "`n📋 Verificando versões instaladas..." -ForegroundColor Magenta | |
| try { | |
| # Verificar pelo comando dotnet | |
| if (Get-Command dotnet -ErrorAction SilentlyContinue) { | |
| Write-ColoredOutput "`n📦 SDKs instalados:" -ForegroundColor Blue | |
| dotnet --list-sdks | |
| Write-ColoredOutput "`n🏃 Runtimes instalados:" -ForegroundColor Blue | |
| dotnet --list-runtimes | |
| } else { | |
| Write-ColoredOutput "⚠️ Comando 'dotnet' não encontrado no PATH. Reinicie o terminal." -ForegroundColor Yellow | |
| } | |
| # Verificar pelo Winget | |
| Write-ColoredOutput "`n📝 Pacotes .NET no Winget:" -ForegroundColor Blue | |
| winget list "Microsoft.DotNet.SDK" | |
| } | |
| catch { | |
| Write-ColoredOutput "⚠️ Erro ao verificar versões instaladas: $_" -ForegroundColor Yellow | |
| } | |
| } | |
| function Main { | |
| Write-ColoredOutput @" | |
| 🚀 INSTALADOR DE MÚLTIPLAS VERSÕES DO .NET SDK - WINGET | |
| ===================================================== | |
| Usando Windows Package Manager para uma instalação mais confiável! | |
| "@ -ForegroundColor Cyan | |
| # Verificar se Winget está disponível | |
| if (-not (Test-WingetAvailable)) { | |
| return | |
| } | |
| # Mostrar pacotes disponíveis | |
| Show-AvailablePackages | |
| Write-ColoredOutput "" | |
| $successCount = 0 | |
| $totalAttempts = 0 | |
| # Se InstallAll foi especificado, ativar todas as opções | |
| if ($InstallAll) { | |
| Write-ColoredOutput "🌟 Modo INSTALAR TUDO ativado!" -ForegroundColor Cyan | |
| $IncludeLTS = $true | |
| $IncludeSTS = $true | |
| $IncludeLegacy = $true | |
| } | |
| # Instalar versões LTS | |
| if ($IncludeLTS) { | |
| Write-ColoredOutput "`n🏷️ Instalando versões LTS (Long Term Support)..." -ForegroundColor Magenta | |
| foreach ($version in $availableVersions.LTS) { | |
| $totalAttempts++ | |
| if (Install-DotNetSDK -Version $version.Version -Description $version.Description -ForceInstall $Force) { | |
| $successCount++ | |
| } | |
| } | |
| } | |
| # Instalar versões STS | |
| if ($IncludeSTS) { | |
| Write-ColoredOutput "`n🏷️ Instalando versões STS (Standard Term Support)..." -ForegroundColor Magenta | |
| foreach ($version in $availableVersions.STS) { | |
| $totalAttempts++ | |
| if (Install-DotNetSDK -Version $version.Version -Description $version.Description -ForceInstall $Force) { | |
| $successCount++ | |
| } | |
| } | |
| } | |
| # Instalar versões Legacy | |
| if ($IncludeLegacy) { | |
| Write-ColoredOutput "`n🏷️ Instalando versões Legacy..." -ForegroundColor Magenta | |
| Write-ColoredOutput "⚠️ Aviso: Estas versões não têm mais suporte oficial da Microsoft" -ForegroundColor Yellow | |
| foreach ($version in $availableVersions.Legacy) { | |
| $totalAttempts++ | |
| if (Install-DotNetSDK -Version $version.Version -Description $version.Description -ForceInstall $Force) { | |
| $successCount++ | |
| } | |
| } | |
| } | |
| # Instalar versões específicas | |
| if ($SpecificVersions.Count -gt 0) { | |
| Write-ColoredOutput "`n🏷️ Instalando versões específicas..." -ForegroundColor Magenta | |
| foreach ($version in $SpecificVersions) { | |
| $totalAttempts++ | |
| if (Install-DotNetSDK -Version $version -Description ".NET SDK $version" -ForceInstall $Force) { | |
| $successCount++ | |
| } | |
| } | |
| } | |
| # Se nenhuma opção foi especificada, instalar LTS por padrão | |
| if (-not $IncludeLTS -and -not $IncludeSTS -and -not $IncludeLegacy -and $SpecificVersions.Count -eq 0) { | |
| Write-ColoredOutput "`n🏷️ Nenhuma versão especificada. Instalando versões LTS..." -ForegroundColor Yellow | |
| foreach ($version in $availableVersions.LTS) { | |
| $totalAttempts++ | |
| if (Install-DotNetSDK -Version $version.Version -Description $version.Description -ForceInstall $Force) { | |
| $successCount++ | |
| } | |
| } | |
| } | |
| # Mostrar versões instaladas | |
| Show-InstalledVersions | |
| # Relatório final | |
| Write-ColoredOutput @" | |
| 📊 RELATÓRIO FINAL | |
| ================= | |
| ✅ Instalações bem-sucedidas: $successCount | |
| ❌ Falhas: $($totalAttempts - $successCount) | |
| 📦 Total de tentativas: $totalAttempts | |
| 🎉 VANTAGENS DO WINGET: | |
| • ✅ Instalação mais rápida e confiável | |
| • ✅ Gerenciamento automático de dependências | |
| • ✅ Integração nativa com Windows | |
| • ✅ Atualizações automáticas com 'winget upgrade' | |
| 🔧 PRÓXIMOS PASSOS: | |
| 1. Use 'dotnet --list-sdks' para ver todas as versões | |
| 2. Use 'winget list Microsoft.DotNet' para ver pacotes instalados | |
| 3. Use 'winget upgrade Microsoft.DotNet.SDK.*' para atualizar | |
| 4. Reinicie o terminal se o comando 'dotnet' não funcionar | |
| 💡 COMANDOS ÚTEIS: | |
| • winget list Microsoft.DotNet.SDK # Ver SDKs instalados | |
| • winget upgrade --all # Atualizar tudo | |
| • winget uninstall Microsoft.DotNet.SDK.8 # Remover versão específica | |
| "@ -ForegroundColor Green | |
| } | |
| # Executar função principal | |
| Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment