Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latest winget #412

Merged
merged 8 commits into from
Oct 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ALways check WinGet version at install
  • Loading branch information
KnifMelti committed Oct 8, 2023
commit bc9eb5ec0058a133350f244705ae3e2b162d1426
88 changes: 75 additions & 13 deletions Winget-AutoUpdate-Install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,83 @@ function Install-Prerequisites {
}
}

function Install-WinGet {
#Function to get the winget command regarding execution context (User, System...)
Function Get-WingetCmd {

#Get WinGet Path (if Admin context)
# Includes Workaround for ARM64 (removed X64 and replaces it with a wildcard)
$ResolveWingetPath = Resolve-Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }

if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
}

#If running under System or Admin context obtain Winget from Program Files
if((([System.Security.Principal.WindowsIdentity]::GetCurrent().User) -eq "S-1-5-18") -or ($WingetPath)){
if (Test-Path "$WingetPath\winget.exe") {
$Script:Winget = "$WingetPath\winget.exe"
}
}else{
#Get Winget Location in User context
$WingetCmd = Get-Command winget.exe -ErrorAction SilentlyContinue
if ($WingetCmd) {
$Script:Winget = $WingetCmd.Source
}
}

If(!($Script:Winget)){
return $false
}

#Run winget to list apps and accept source agrements (necessary on first run)
& $Winget list --accept-source-agreements -s winget | Out-Null

return $true

}

#Function to get the latest WinGet available version on Github
Function Get-AvailableWinGetVersion {

#Get latest WinGet info
$WinGeturl = 'https://api.github.com/repos/microsoft/winget-cli/releases/latest'

try {
#Return latest version
return ((Invoke-WebRequest $WinGeturl -UseBasicParsing | ConvertFrom-Json)[0].tag_name).Replace("v", "")
}
catch {
return $false
}

Write-Host "`nChecking if Winget is installed" -ForegroundColor Yellow
}

#Check Package Install
$TestWinGet = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -eq "Microsoft.DesktopAppInstaller" }

#Current: v1.5.2201 = 1.20.2201.0 = 2023.808.2243.0
If ([Version]$TestWinGet.Version -ge "2023.808.2243.0") {
function Install-WinGet {

Write-Host "Winget is Installed" -ForegroundColor Green
Write-Host "`nChecking if Winget is installed/up to date" -ForegroundColor Yellow

#Check available WinGet version, if fail set version to the latest version in 2023-10-08
$AvailableWinGetVersion = Get-AvailableWinGetVersion
if (!$AvailableWinGetVersion) {
$AvailableWinGetVersion = "1.6.2771"
}
Else {

Write-Host "-> Winget is not installed:"
#Check if WinGet is installed, if not set version to dummy...
if (!(Get-WingetCmd)) {
$InstalledWinGetVersion = "0.0.0001"
}
else {
#Get installed WinGet version
$LocalWinGet = & $Winget --version
$InstalledWinGetVersion = $LocalWinGet.Replace("v", "")
}

#Check if the current available WinGet isn't a Pre-release and if it's newer than the installed
if (!($AvailableWinGetVersion -match "-pre") -and ($AvailableWinGetVersion -gt $InstalledWinGetVersion)) {

Write-Host "-> Winget is not installed/up to date:"

#Check if $WingetUpdatePath exist
if (!(Test-Path $WingetUpdatePath)) {
Expand Down Expand Up @@ -235,10 +296,10 @@ function Install-WinGet {
}
Remove-Item -Path $VCLibsFile -Force
}

#Download WinGet MSIXBundle
Write-Host "-> Downloading Winget MSIXBundle for App Installer..."
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v1.5.2201/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$AvailableWinGetVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($WinGetURL, "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")

Expand All @@ -254,9 +315,10 @@ function Install-WinGet {

#Remove WinGet MSIXBundle
Remove-Item -Path "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue

}

else {
Write-Host "-> Winget is up to date: $InstalledWinGetVersion"
}
}

function Install-WingetAutoUpdate {
Expand Down