From b2ba57dfd8bc464c8685f6978b5ec8c2b5ae5e3f Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Tue, 10 Oct 2023 00:03:10 +0200 Subject: [PATCH] Better --- Winget-AutoUpdate-Install.ps1 | 36 +++++++++++-------- Winget-AutoUpdate/functions/Get-StoreApps.ps1 | 14 ++++++++ ...ion.ps1 => Get-WinGetAvailableVersion.ps1} | 2 +- .../functions/Invoke-PostUpdateActions.ps1 | 29 ++++++++------- 4 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 Winget-AutoUpdate/functions/Get-StoreApps.ps1 rename Winget-AutoUpdate/functions/{Get-AvailableWinGetVersion.ps1 => Get-WinGetAvailableVersion.ps1} (90%) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 98c0163..59f06e2 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -183,29 +183,32 @@ function Install-WinGet { Write-Host "`nChecking if WinGet is installed/up to date" -ForegroundColor Yellow + #Include external Functions + . "$PSScriptRoot\Winget-AutoUpdate\functions\Get-WinGetAvailableVersion.ps1" + . "$PSScriptRoot\Winget-AutoUpdate\functions\Get-StoreApps.ps1" + #Check available WinGet version, if fail set version to the latest version as of 2023-10-08 - . "$PSScriptRoot\Winget-AutoUpdate\functions\Get-AvailableWinGetVersion.ps1" - $AvailableWinGetVersion = Get-AvailableWinGetVersion - if (!$AvailableWinGetVersion) { - $AvailableWinGetVersion = "1.6.2771" + $WinGetAvailableVersion = Get-WinGetAvailableVersion + if (!$WinGetAvailableVersion) { + $WinGetAvailableVersion = "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') } if (!$ResolveWingetPath) { - $InstalledWinGetVersion = "0.0.0000" + $WinGetInstalledVersion = "0.0.0000" } else { #If multiple version, pick last one $WingetPath = $ResolveWingetPath[-1].Path - $InstalledWinGetVersion = & $WingetPath --version - $InstalledWinGetVersion = $InstalledWinGetVersion.Replace("v", "") + $WinGetInstalledVersion = & $WingetPath --version + $WinGetInstalledVersion = $WinGetInstalledVersion.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)) { + if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) { - Write-Host "-> WinGet is not installed/up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available:" -ForegroundColor Red + Write-Host "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" -ForegroundColor Red #Check if $WingetUpdatePath exist if (!(Test-Path $WingetUpdatePath)) { @@ -249,7 +252,7 @@ function Install-WinGet { #Download WinGet MSIXBundle Write-Host "-> Downloading WinGet MSIXBundle for App Installer..." - $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$AvailableWinGetVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" + $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile($WinGetURL, "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") @@ -257,7 +260,7 @@ function Install-WinGet { try { Write-Host "-> Installing WinGet MSIXBundle for App Installer..." Add-AppxProvisionedPackage -Online -PackagePath "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null - Write-host "Winget MSIXBundle (v$AvailableWinGetVersion) for App Installer installed successfully" -ForegroundColor Green + Write-host "Winget MSIXBundle (v$WinGetAvailableVersion) 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') } @@ -276,11 +279,16 @@ function Install-WinGet { #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 + elseif ($WinGetAvailableVersion -match "-pre") { + Write-Host "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" -ForegroundColor Yellow + #If not WSB or Server, upgrade Microsoft Store Apps! + if (!(Test-Path "${env:SystemDrive}\Users\WDAGUtilityAccount") -and (Get-CimInstance Win32_OperatingSystem).Caption -notmatch "Windows Server") { + Write-Host "-> Forcing an upgrade of Store Apps (this can take a minute)..." -ForegroundColor Yellow + Get-StoreApps + } } else { - Write-Host "-> WinGet is up to date: v$InstalledWinGetVersion" -ForegroundColor Green + Write-Host "-> WinGet is up to date: v$WinGetInstalledVersion" -ForegroundColor Green } } diff --git a/Winget-AutoUpdate/functions/Get-StoreApps.ps1 b/Winget-AutoUpdate/functions/Get-StoreApps.ps1 new file mode 100644 index 0000000..8e9b7f8 --- /dev/null +++ b/Winget-AutoUpdate/functions/Get-StoreApps.ps1 @@ -0,0 +1,14 @@ +#Function to force an upgrade of Store Apps +Function Get-StoreApps { + + try { + $namespaceName = "root\cimv2\mdm\dmmap" + $className = "MDM_EnterpriseModernAppManagement_AppManagement01" + $wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className + $wmiObj.UpdateScanMethod() + return $true + } + catch { + return $false + } +} diff --git a/Winget-AutoUpdate/functions/Get-AvailableWinGetVersion.ps1 b/Winget-AutoUpdate/functions/Get-WinGetAvailableVersion.ps1 similarity index 90% rename from Winget-AutoUpdate/functions/Get-AvailableWinGetVersion.ps1 rename to Winget-AutoUpdate/functions/Get-WinGetAvailableVersion.ps1 index 936da18..1691262 100644 --- a/Winget-AutoUpdate/functions/Get-AvailableWinGetVersion.ps1 +++ b/Winget-AutoUpdate/functions/Get-WinGetAvailableVersion.ps1 @@ -1,5 +1,5 @@ #Function to get the latest WinGet available version on Github -Function Get-AvailableWinGetVersion { +Function Get-WinGetAvailableVersion { #Get latest WinGet info $WinGeturl = 'https://api.github.com/repos/microsoft/winget-cli/releases/latest' diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 8457249..09795e0 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -20,9 +20,9 @@ function Invoke-PostUpdateActions { Write-ToLog "-> Checking if WinGet is installed/up to date" "yellow" #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" + $WinGetAvailableVersion = Get-WinGetAvailableVersion + if (!$WinGetAvailableVersion) { + $WinGetAvailableVersion = "1.6.2771" } #Check installed WinGet version @@ -30,18 +30,18 @@ function Invoke-PostUpdateActions { if ($ResolveWingetPath) { #If multiple version, pick last one $WingetPath = $ResolveWingetPath[-1].Path - $InstalledWinGetVersion = & $WingetPath --version - $InstalledWinGetVersion = $InstalledWinGetVersion.Replace("v", "") + $WinGetInstalledVersion = & $WingetPath --version + $WinGetInstalledVersion = $WinGetInstalledVersion.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)) { + if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) { - Write-ToLog "-> WinGet is not installed/up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available:" "red" + Write-ToLog "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" "red" #Download WinGet MSIXBundle Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..." - $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$AvailableWinGetVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" + $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile($WinGetURL, "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") @@ -49,7 +49,7 @@ function Invoke-PostUpdateActions { try { Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..." Add-AppxProvisionedPackage -Online -PackagePath "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null - Write-ToLog "-> Winget MSIXBundle (v$AvailableWinGetVersion) for App Installer installed successfully" "green" + Write-ToLog "-> Winget MSIXBundle (v$WinGetAvailableVersion) 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') } @@ -68,11 +68,16 @@ function Invoke-PostUpdateActions { #Remove WinGet MSIXBundle Remove-Item -Path "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue } - elseif ($AvailableWinGetVersion -match "-pre") { - Write-ToLog "-> WinGet is up to date (v$InstalledWinGetVersion) - v$AvailableWinGetVersion is available but only as a Pre-release" "yellow" + elseif ($WinGetAvailableVersion -match "-pre") { + Write-ToLog "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" "yellow" + #If not WSB or Server, upgrade Microsoft Store Apps! + if (!(Test-Path "${env:SystemDrive}\Users\WDAGUtilityAccount") -and (Get-CimInstance Win32_OperatingSystem).Caption -notmatch "Windows Server") { + Write-ToLog "-> Forcing an upgrade of Store Apps (this can take a minute)..." "yellow" + Get-StoreApps + } } else { - Write-ToLog "-> WinGet is up to date: v$InstalledWinGetVersion" "green" + Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "green" } #Create WAU Regkey if not present