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
Show file tree
Hide file tree
Changes from 4 commits
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
76 changes: 58 additions & 18 deletions Winget-AutoUpdate-Install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ param(

<# FUNCTIONS #>

#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
}

}

KnifMelti marked this conversation as resolved.
Show resolved Hide resolved
function Install-Prerequisites {

Write-Host "`nChecking prerequisites..." -ForegroundColor Yellow
Expand Down Expand Up @@ -181,20 +197,30 @@ function Install-Prerequisites {

function Install-WinGet {

Write-Host "`nChecking if Winget is installed" -ForegroundColor Yellow
Write-Host "`nChecking if WinGet is installed/up to date" -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") {

Write-Host "Winget is Installed" -ForegroundColor Green
#Check available WinGet version, if fail set version to the latest version as of 2023-10-08
$AvailableWinGetVersion = Get-AvailableWinGetVersion
if (!$AvailableWinGetVersion) {
$AvailableWinGetVersion = "1.6.2771"
}

#Check if WinGet is installed, if not set version to dummy...
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bug when running as admin (not from system). I would go for the function Get-WingetCmd. This way, it's better if it is installed from SCCM/Intune or installed by hand with admin rights.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, but do as is best!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅

if (!$ResolveWingetPath) {
$InstalledWinGetVersion = "0.0.0000"
}
Else {
else {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
$InstalledWinGetVersion = & $WingetPath --version
$InstalledWinGetVersion = $InstalledWinGetVersion.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:"
Write-Host "-> WinGet is not installed/up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available:" -ForegroundColor Red

#Check if $WingetUpdatePath exist
if (!(Test-Path $WingetUpdatePath)) {
Expand Down Expand Up @@ -235,28 +261,42 @@ 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"
Write-Host "-> Downloading WinGet MSIXBundle for App Installer..."
$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")

#Install WinGet MSIXBundle in SYSTEM context
try {
Write-Host "-> Installing Winget MSIXBundle for App Installer..."
Write-Host "-> Installing WinGet MSIXBundle for App Installer..."
Add-AppxProvisionedPackage -Online -PackagePath "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null
Write-host "Winget MSIXBundle for App Installer installed successfully" -ForegroundColor Green
Write-host "Winget MSIXBundle (v$AvailableWinGetVersion) for App Installer installed successfully" -ForegroundColor Green

#Reset WinGet Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force
#log
Write-Host "-> WinGet sources reset." -ForegroundColor Green
}
}
catch {
Write-Host "Failed to intall Winget MSIXBundle for App Installer..." -ForegroundColor Red
Write-Host "Failed to intall WinGet MSIXBundle for App Installer..." -ForegroundColor Red
}

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

}

elseif ($AvailableWinGetVersion -match "-pre") {
Write-Host "-> WinGet is up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available but only as a Pre-release" -ForegroundColor Yellow
}
else {
Write-Host "-> WinGet is up to date: v$InstalledWinGetVersion" -ForegroundColor Green
}
}

function Install-WingetAutoUpdate {
Expand Down
15 changes: 15 additions & 0 deletions Winget-AutoUpdate/functions/Get-AvailableWinGetVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#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
}

}
64 changes: 39 additions & 25 deletions Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,62 @@ function Invoke-PostUpdateActions {
$null = (New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ItemType SymbolicLink -Value ('{0}\logs\install.log' -f $WorkingDir) -Force -Confirm:$False -ErrorAction SilentlyContinue)
}

#Check Package Install
Write-ToLog "-> Checking if Winget is installed/up to date" "yellow"
$TestWinGet = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -eq "Microsoft.DesktopAppInstaller" }
Write-ToLog "-> Checking if WinGet is installed/up to date" "yellow"

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

Write-ToLog "-> WinGet is Installed/up to date" "green"
#Check available WinGet version, if fail set version to the latest version as of 2023-10-08
$AvailableWinGetVersion = Get-AvailableWinGetVersion
if (!$AvailableWinGetVersion) {
$AvailableWinGetVersion = "1.6.2771"
}

#Check installed WinGet version
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
$InstalledWinGetVersion = & $WingetPath --version
$InstalledWinGetVersion = $InstalledWinGetVersion.Replace("v", "")
}
Else {

#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-ToLog "-> WinGet is not installed/up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available:" "red"

#Download WinGet MSIXBundle
Write-ToLog "-> Not installed/up to date. Downloading WinGet..."
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v1.5.2201/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..."
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$AvailableWinGetVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($WinGetURL, "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")

#Install WinGet MSIXBundle
#Install WinGet MSIXBundle in SYSTEM context
try {
Write-ToLog "-> Installing Winget MSIXBundle for App Installer..."
Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..."
Add-AppxProvisionedPackage -Online -PackagePath "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null
Write-ToLog "-> Installed Winget MSIXBundle for App Installer" "green"
Write-ToLog "-> Winget MSIXBundle (v$AvailableWinGetVersion) for App Installer installed successfully" "green"

#Reset WinGet Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force
#log
Write-ToLog "-> WinGet sources reset." "green"
}
}
catch {
Write-ToLog "-> Failed to intall Winget MSIXBundle for App Installer..." "red"
Write-ToLog "-> Failed to intall WinGet MSIXBundle for App Installer..." "red"
}

#Remove WinGet MSIXBundle
Remove-Item -Path "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue

}

#Reset Winget Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force

#log
Write-ToLog "-> Winget sources reseted." "green"
elseif ($AvailableWinGetVersion -match "-pre") {
Write-ToLog "-> WinGet is up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available but only as a Pre-release" "yellow"
}
else {
Write-ToLog "-> WinGet is up to date: v$InstalledWinGetVersion" "green"
}

#Create WAU Regkey if not present
Expand Down