From 253977071280ca5c875bc416299669506bc87490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pl=C3=ADnio=20Larrubia?= Date: Mon, 14 Nov 2022 15:00:26 -0300 Subject: [PATCH] Potential fix to Winget installation (finally) - Source from: https://github.com/asheroto/winget-installer and ChrisTitus tech's script --- src/scripts/install-package-managers.ps1 | 57 ++++++++++++++++-------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/scripts/install-package-managers.ps1 b/src/scripts/install-package-managers.ps1 index d093cf8..29aaf74 100644 --- a/src/scripts/install-package-managers.ps1 +++ b/src/scripts/install-package-managers.ps1 @@ -82,11 +82,13 @@ function Install-WingetDependency() { Wait-Process -Id $AppInstallerId } - Remove-Item -Path $WingetDepOutput + Return $WingetDepOutput } Else { Write-Status -Types "?" -Status "$OSArch is not supported!" -Warning } } + + Return $false } function Main() { @@ -95,22 +97,46 @@ function Main() { CheckExistenceBlock = { winget --version } InstallCommandBlock = { - Install-WingetDependency + New-Item -Path "$PWD\..\tmp\" -Name "winget-install" -ItemType Directory -Force | Out-Null + Push-Location -Path "$PWD\..\tmp\winget-install\" + Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted + Install-Script -Name winget-install -Force + winget-install.ps1 + Pop-Location + Remove-Item -Path "$PWD\..\tmp\winget-install\" + } + Time = "12:00" + UpdateScriptBlock = + { + Remove-Item -Path "$env:TEMP\Win-DT-Logs\*" -Include "WingetDailyUpgrade_*.log" + Start-Transcript -Path "$env:TEMP\Win-DT-Logs\WingetDailyUpgrade_$(Get-Date -Format "yyyy-MM-dd_HH-mm-ss").log" + Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force # Only needed to run Winget + winget upgrade --all --silent | Out-Host + Stop-Transcript + } + } + + $WingetParams2 = @{ + Name = "Winget" + CheckExistenceBlock = { winget --version } + InstallCommandBlock = + { + $WingetDepOutput = Install-WingetDependency $WingetOutput = Get-APIFile -URI "https://api.github.com/repos/microsoft/winget-cli/releases/latest" -ObjectProperty "assets" -FileNameLike "*.msixbundle" -PropertyValue "browser_download_url" -OutputFile "Microsoft.DesktopAppInstaller.msixbundle" $WingetLicenseOutput = Get-APIFile -URI "https://api.github.com/repos/microsoft/winget-cli/releases/latest" -ObjectProperty "assets" -FileNameLike "*License*.xml" -PropertyValue "browser_download_url" -OutputFile "WingetLicense.xml" $AppName = Split-Path -Path $WingetOutput -Leaf Try { # Method from: https://github.com/microsoft/winget-cli/blob/master/doc/troubleshooting/README.md#machine-wide-provisioning + If ($WingetDepOutput) { + Write-Status -Types "@" -Status "Trying to install the App (w/ license + dependency): $AppName" -Warning + $InstallPackageCommand = { Add-AppxProvisionedPackage -Online -PackagePath $WingetOutput -LicensePath $WingetLicenseOutput -DependencyPackagePath $WingetDepOutput | Out-Null } + Invoke-Expression "$InstallPackageCommand" + } + Write-Status -Types "@" -Status "Trying to install the App (w/ license): $AppName" -Warning $InstallPackageCommand = { Add-AppxProvisionedPackage -Online -PackagePath $WingetOutput -LicensePath $WingetLicenseOutput | Out-Null } Invoke-Expression "$InstallPackageCommand" - If ($LASTEXITCODE) { Throw "Couldn't install automatically" } - - Write-Status -Types "@" -Status "Trying to install the App (w/out license): $AppName" -Warning - $OldInstallPackageCommand = { Add-AppxPackage -Path $WingetOutput } - Invoke-Expression "$OldInstallPackageCommand" - If ($LASTEXITCODE) { Throw "Couldn't install automatically" } } Catch { Write-Status -Types "@" -Status "Couldn't install '$AppName' automatically, trying to install the App manually..." -Warning Start-Process "ms-windows-store://pdp/?ProductId=9NBLGGH4NNS1" -Wait # GUI App installer can't install itself @@ -119,15 +145,6 @@ function Main() { Remove-Item -Path $WingetOutput Remove-Item -Path $WingetLicenseOutput } - Time = "12:00" - UpdateScriptBlock = - { - Remove-Item -Path "$env:TEMP\Win-DT-Logs\*" -Include "WingetDailyUpgrade_*.log" - Start-Transcript -Path "$env:TEMP\Win-DT-Logs\WingetDailyUpgrade_$(Get-Date -Format "yyyy-MM-dd_HH-mm-ss").log" - Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force # Only needed to run Winget - winget upgrade --all --silent | Out-Host - Stop-Transcript - } } $ChocolateyParams = @{ @@ -150,10 +167,12 @@ function Main() { PostInstallBlock = { choco install --ignore-dependencies --yes "chocolatey-core.extension" "chocolatey-fastanswers.extension" "dependency-windows10" } } - # Install Winget on Windows + # Install Winget on Windows (Method 1) Install-PackageManager -PackageManagerFullName $WingetParams.Name -CheckExistenceBlock $WingetParams.CheckExistenceBlock -InstallCommandBlock $WingetParams.InstallCommandBlock -Time $WingetParams.Time -UpdateScriptBlock $WingetParams.UpdateScriptBlock + # Install Winget on Windows (Method 2) + Install-PackageManager -PackageManagerFullName $WingetParams2.Name -CheckExistenceBlock $WingetParams2.CheckExistenceBlock -InstallCommandBlock $WingetParams2.InstallCommandBlock # Install Chocolatey on Windows Install-PackageManager -PackageManagerFullName $ChocolateyParams.Name -CheckExistenceBlock $ChocolateyParams.CheckExistenceBlock -InstallCommandBlock $ChocolateyParams.InstallCommandBlock -Time $ChocolateyParams.Time -UpdateScriptBlock $ChocolateyParams.UpdateScriptBlock -PostInstallBlock $ChocolateyParams.PostInstallBlock } -Main \ No newline at end of file +Main