From c595332b81d23c77821a6503375828edb4e1b098 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 11 Oct 2023 22:19:34 +0200 Subject: [PATCH 1/9] Better Functions --- Winget-AutoUpdate-Install.ps1 | 45 +++++------------ Winget-AutoUpdate/functions/Get-StoreApps.ps1 | 14 ------ .../functions/Invoke-PostUpdateActions.ps1 | 36 ++------------ Winget-AutoUpdate/functions/Update-WinGet.ps1 | 48 +++++++++++++++++++ 4 files changed, 64 insertions(+), 79 deletions(-) delete mode 100644 Winget-AutoUpdate/functions/Get-StoreApps.ps1 create mode 100644 Winget-AutoUpdate/functions/Update-WinGet.ps1 diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 794ed6a..3b84eef 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -118,6 +118,12 @@ param( <# FUNCTIONS #> +#Include external Functions +. "$PSScriptRoot\Winget-AutoUpdate\functions\Start-Init.ps1" +. "$PSScriptRoot\Winget-AutoUpdate\functions\Invoke-ModsProtect.ps1" +. "$PSScriptRoot\Winget-AutoUpdate\functions\Update-WinGet.ps1" +. "$PSScriptRoot\Winget-AutoUpdate\functions\Get-WinGetAvailableVersion.ps1" + function Install-Prerequisites { Write-Host "`nChecking prerequisites..." -ForegroundColor Yellow @@ -183,10 +189,6 @@ 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 $WinGetAvailableVersion = Get-WinGetAvailableVersion if (!$WinGetAvailableVersion) { @@ -250,41 +252,18 @@ 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/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" - $WebClient = New-Object System.Net.WebClient - $WebClient.DownloadFile($WinGetURL, "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") + Update-WinGet "1.6.2721" $WingetUpdatePath - #Install WinGet MSIXBundle in SYSTEM context - 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$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') } - 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 - } - - #Remove WinGet MSIXBundle - Remove-Item -Path "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue } 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 + $namespaceName = "root\cimv2\mdm\dmmap" + $className = "MDM_EnterpriseModernAppManagement_AppManagement01" + $wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className + $wmiObj.UpdateScanMethod() } } else { @@ -469,12 +448,10 @@ function Install-WingetAutoUpdate { #Log file and symlink initialization - . "$WingetUpdatePath\functions\Start-Init.ps1" Start-Init #Security check Write-host "`nChecking Mods Directory:" -ForegroundColor Yellow - . "$WingetUpdatePath\functions\Invoke-ModsProtect.ps1" $Protected = Invoke-ModsProtect "$WingetUpdatePath\mods" if ($Protected -eq $True) { Write-Host "The mods directory is now secured!`n" -ForegroundColor Green diff --git a/Winget-AutoUpdate/functions/Get-StoreApps.ps1 b/Winget-AutoUpdate/functions/Get-StoreApps.ps1 deleted file mode 100644 index 8e9b7f8..0000000 --- a/Winget-AutoUpdate/functions/Get-StoreApps.ps1 +++ /dev/null @@ -1,14 +0,0 @@ -#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/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index bd924fa..23953a6 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -36,44 +36,18 @@ function Invoke-PostUpdateActions { #Check if the current available WinGet isn't a Pre-release and if it's newer than the installed if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) { - 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$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" - $WebClient = New-Object System.Net.WebClient - $WebClient.DownloadFile($WinGetURL, "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") - - #Install WinGet MSIXBundle in SYSTEM context - 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$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') } - 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" - } - - #Remove WinGet MSIXBundle - Remove-Item -Path "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue + Update-WinGet $WinGetAvailableVersion $($WAUConfig.InstallLocation) $true } 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 + $namespaceName = "root\cimv2\mdm\dmmap" + $className = "MDM_EnterpriseModernAppManagement_AppManagement01" + $wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className + $wmiObj.UpdateScanMethod() } } else { diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 new file mode 100644 index 0000000..14e5356 --- /dev/null +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -0,0 +1,48 @@ +#Function to download and update the WinGet + +Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { + + #Download WinGet MSIXBundle + switch ($Log) { + $true {Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..."} + Default {Write-Host "-> Downloading WinGet MSIXBundle for App Installer..."} + } + $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" + $WebClient = New-Object System.Net.WebClient + $WebClient.DownloadFile($WinGetURL, "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") + + #Install WinGet MSIXBundle in SYSTEM context + try { + switch ($Log) { + $true {Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..."} + Default {Write-Host "-> Installing WinGet MSIXBundle for App Installer..."} + } + Add-AppxProvisionedPackage -Online -PackagePath "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null + switch ($Log) { + $true {Write-ToLog "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" "green"} + Default {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') } + if ($ResolveWingetPath) { + #If multiple version, pick last one + $WingetPath = $ResolveWingetPath[-1].Path + & $WingetPath source reset --force + #log + switch ($Log) { + $true {Write-ToLog "-> WinGet sources reset." "green"} + Default {Write-Host "-> WinGet sources reset." -ForegroundColor Green} + } + } + } + catch { + switch ($Log) { + $true {Write-ToLog "-> Failed to intall WinGet MSIXBundle for App Installer..." "red"} + Default {Write-Host "Failed to intall WinGet MSIXBundle for App Installer..." -ForegroundColor Red} + } + } + + #Remove WinGet MSIXBundle + Remove-Item -Path "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue +} From dabbf96dee4fb0c81c50e77ee1d657635107d6f4 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 11 Oct 2023 22:26:48 +0200 Subject: [PATCH 2/9] Forgot to change the test version back... --- Winget-AutoUpdate-Install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 3b84eef..3fbaa4a 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -252,7 +252,7 @@ function Install-WinGet { Remove-Item -Path $VCLibsFile -Force } - Update-WinGet "1.6.2721" $WingetUpdatePath + Update-WinGet $WinGetAvailableVersion $WingetUpdatePath } elseif ($WinGetAvailableVersion -match "-pre") { From 8297aca48ab282b4bd19318b8ffe5ea452ec4ebc Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 11 Oct 2023 22:45:50 +0200 Subject: [PATCH 3/9] Small fix --- Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 23953a6..850e63c 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -7,13 +7,13 @@ function Invoke-PostUpdateActions { # Check if Intune Management Extension Logs folder and WAU-updates.log exists, make symlink if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue) -and !(Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ErrorAction SilentlyContinue)) { - Write-ToLog -LogMsg '-> Creating SymLink for log file in Intune Management Extension log folder' -LogColor 'yellow' + Write-ToLog "-> Creating SymLink for log file in Intune Management Extension log folder" "yellow" $null = New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue } # Check if Intune Management Extension Logs folder and WAU-install.log exists, make symlink if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue) -and (Test-Path -Path ('{0}\logs\install.log' -f $WorkingDir) -ErrorAction SilentlyContinue) -and !(Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ErrorAction SilentlyContinue)) { - Write-Host -Object "`nCreating SymLink for log file (WAU-install) in Intune Management Extension log folder" -ForegroundColor Yellow + Write-ToLog "Creating SymLink for log file (WAU-install) in Intune Management Extension log folder" "yellow" $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) } From 80a80b29a11fc8b16c25d1a108ab34406faccb8e Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 11 Oct 2023 22:50:03 +0200 Subject: [PATCH 4/9] Even smaller... --- Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 850e63c..966538f 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -13,7 +13,7 @@ function Invoke-PostUpdateActions { # Check if Intune Management Extension Logs folder and WAU-install.log exists, make symlink if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue) -and (Test-Path -Path ('{0}\logs\install.log' -f $WorkingDir) -ErrorAction SilentlyContinue) -and !(Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ErrorAction SilentlyContinue)) { - Write-ToLog "Creating SymLink for log file (WAU-install) in Intune Management Extension log folder" "yellow" + Write-ToLog "-> Creating SymLink for log file (WAU-install) in Intune Management Extension log folder" "yellow" $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) } From 3f0f6da53790746eaae450a0b127ce36d8ced0b9 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Thu, 12 Oct 2023 04:22:15 +0200 Subject: [PATCH 5/9] Update-StoreApps if fail/uncertain --- Winget-AutoUpdate-Install.ps1 | 16 +++---- .../functions/Invoke-PostUpdateActions.ps1 | 13 ++---- .../functions/Update-StoreApps.ps1 | 26 +++++++++++ Winget-AutoUpdate/functions/Update-WinGet.ps1 | 44 +++++++++---------- 4 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 Winget-AutoUpdate/functions/Update-StoreApps.ps1 diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index 3fbaa4a..fa7ce86 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -121,8 +121,9 @@ param( #Include external Functions . "$PSScriptRoot\Winget-AutoUpdate\functions\Start-Init.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Invoke-ModsProtect.ps1" -. "$PSScriptRoot\Winget-AutoUpdate\functions\Update-WinGet.ps1" . "$PSScriptRoot\Winget-AutoUpdate\functions\Get-WinGetAvailableVersion.ps1" +. "$PSScriptRoot\Winget-AutoUpdate\functions\Update-WinGet.ps1" +. "$PSScriptRoot\Winget-AutoUpdate\functions\Update-StoreApps.ps1" function Install-Prerequisites { @@ -251,20 +252,13 @@ function Install-WinGet { } Remove-Item -Path $VCLibsFile -Force } - - Update-WinGet $WinGetAvailableVersion $WingetUpdatePath + + Update-WinGet $WinGetAvailableVersion $WingetUpdatePath } 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 - $namespaceName = "root\cimv2\mdm\dmmap" - $className = "MDM_EnterpriseModernAppManagement_AppManagement01" - $wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className - $wmiObj.UpdateScanMethod() - } + Update-StoreApps } else { Write-Host "-> WinGet is up to date: v$WinGetInstalledVersion" -ForegroundColor Green diff --git a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 index 966538f..4b19de5 100644 --- a/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 +++ b/Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1 @@ -7,7 +7,7 @@ function Invoke-PostUpdateActions { # Check if Intune Management Extension Logs folder and WAU-updates.log exists, make symlink if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue) -and !(Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ErrorAction SilentlyContinue)) { - Write-ToLog "-> Creating SymLink for log file in Intune Management Extension log folder" "yellow" + Write-ToLog "-> Creating SymLink for log file (WAU-updates) in Intune Management Extension log folder" "yellow" $null = New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue } @@ -37,18 +37,11 @@ function Invoke-PostUpdateActions { #Check if the current available WinGet isn't a Pre-release and if it's newer than the installed if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) { Write-ToLog "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" "red" - Update-WinGet $WinGetAvailableVersion $($WAUConfig.InstallLocation) $true + Update-WinGet $WinGetAvailableVersion $($WAUConfig.InstallLocation) $true } 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" - $namespaceName = "root\cimv2\mdm\dmmap" - $className = "MDM_EnterpriseModernAppManagement_AppManagement01" - $wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className - $wmiObj.UpdateScanMethod() - } + Update-StoreApps $true } else { Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "green" diff --git a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 new file mode 100644 index 0000000..31817c5 --- /dev/null +++ b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 @@ -0,0 +1,26 @@ +#Function to force an upgrade of Store Apps + +Function Update-StoreApps ($Log = $false) { + + #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") { + switch ($Log) { + $true {Write-ToLog "-> Forcing an upgrade of Store Apps (this can take a minute)..." "yellow"} + Default {Write-Host "-> Forcing an upgrade of Store Apps (this can take a minute)..." -ForegroundColor Yellow} + } + try { + $namespaceName = "root\cimv2\mdm\dmmap" + $className = "MDM_EnterpriseModernAppManagement_AppManagement01" + $wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className + $wmiObj.UpdateScanMethod() | Out-Null + return $true + } + catch { + switch ($Log) { + $true {Write-ToLog "-> Something went wrong..." "red"} + Default {Write-Host "-> Something went wrong..." -ForegroundColor Red} + } + return $false + } + } +} diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 index 14e5356..61a8c12 100644 --- a/Winget-AutoUpdate/functions/Update-WinGet.ps1 +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -1,46 +1,46 @@ -#Function to download and update the WinGet +#Function to download and update WinGet Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { #Download WinGet MSIXBundle - switch ($Log) { - $true {Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..."} - Default {Write-Host "-> Downloading WinGet MSIXBundle for App Installer..."} - } + switch ($Log) { + $true {Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..."} + Default {Write-Host "-> Downloading WinGet MSIXBundle for App Installer..."} + } $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile($WinGetURL, "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle") #Install WinGet MSIXBundle in SYSTEM context try { - switch ($Log) { - $true {Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..."} - Default {Write-Host "-> Installing WinGet MSIXBundle for App Installer..."} - } + switch ($Log) { + $true {Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..."} + Default {Write-Host "-> Installing WinGet MSIXBundle for App Installer..."} + } Add-AppxProvisionedPackage -Online -PackagePath "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null - switch ($Log) { - $true {Write-ToLog "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" "green"} - Default {Write-host "WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" -ForegroundColor Green} - } + switch ($Log) { + $true {Write-ToLog "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" "green"} + Default {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') } if ($ResolveWingetPath) { + switch ($Log) { + $true {Write-ToLog "-> WinGet sources reset." "green"} + Default {Write-Host "-> WinGet sources reset." -ForegroundColor Green} + } #If multiple version, pick last one $WingetPath = $ResolveWingetPath[-1].Path & $WingetPath source reset --force - #log - switch ($Log) { - $true {Write-ToLog "-> WinGet sources reset." "green"} - Default {Write-Host "-> WinGet sources reset." -ForegroundColor Green} - } } } catch { - switch ($Log) { - $true {Write-ToLog "-> Failed to intall WinGet MSIXBundle for App Installer..." "red"} - Default {Write-Host "Failed to intall WinGet MSIXBundle for App Installer..." -ForegroundColor Red} - } + switch ($Log) { + $true {Write-ToLog "-> Failed to intall WinGet MSIXBundle for App Installer..." "red"} + Default {Write-Host "Failed to intall WinGet MSIXBundle for App Installer..." -ForegroundColor Red} + } + Update-StoreApps } #Remove WinGet MSIXBundle From 59df5ada2b7ca8b132561e4a01c9d5cd77d8f54f Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Thu, 12 Oct 2023 04:40:28 +0200 Subject: [PATCH 6/9] Text change --- Winget-AutoUpdate/functions/Update-StoreApps.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 index 31817c5..a82219a 100644 --- a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 +++ b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 @@ -17,8 +17,8 @@ Function Update-StoreApps ($Log = $false) { } catch { switch ($Log) { - $true {Write-ToLog "-> Something went wrong..." "red"} - Default {Write-Host "-> Something went wrong..." -ForegroundColor Red} + $true {Write-ToLog "-> ...something went wrong!" "red"} + Default {Write-Host "-> ...something went wrong!" -ForegroundColor Red} } return $false } From 7392ec0536d69451b622149d5b01105ab1206e4f Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Thu, 12 Oct 2023 19:42:01 +0200 Subject: [PATCH 7/9] String variables --- Winget-AutoUpdate-Install.ps1 | 14 +++++----- .../functions/Update-StoreApps.ps1 | 11 +++++--- Winget-AutoUpdate/functions/Update-WinGet.ps1 | 26 ++++++++++++------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Winget-AutoUpdate-Install.ps1 b/Winget-AutoUpdate-Install.ps1 index fa7ce86..e99b671 100644 --- a/Winget-AutoUpdate-Install.ps1 +++ b/Winget-AutoUpdate-Install.ps1 @@ -170,10 +170,10 @@ function Install-Prerequisites { Write-host "-> Installing VC_redist.$OSArch.exe..." Start-Process -FilePath $Installer -Args "/quiet /norestart" -Wait Remove-Item $Installer -ErrorAction Ignore - Write-host "MS Visual C++ 2015-2022 installed successfully" -ForegroundColor Green + Write-host "-> MS Visual C++ 2015-2022 installed successfully" -ForegroundColor Green } catch { - Write-host "MS Visual C++ 2015-2022 installation failed." -ForegroundColor Red + Write-host "-> MS Visual C++ 2015-2022 installation failed." -ForegroundColor Red Start-Sleep 3 } } @@ -182,7 +182,7 @@ function Install-Prerequisites { } } else { - Write-Host "Prerequisites checked. OK" -ForegroundColor Green + Write-Host "-> Prerequisites checked. OK" -ForegroundColor Green } } @@ -228,10 +228,10 @@ function Install-WinGet { try { Write-Host "-> Installing Microsoft.UI.Xaml.2.7..." Add-AppxProvisionedPackage -Online -PackagePath "$WingetUpdatePath\extracted\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx" -SkipLicense | Out-Null - Write-host "Microsoft.UI.Xaml.2.7 installed successfully" -ForegroundColor Green + Write-host "-> Microsoft.UI.Xaml.2.7 installed successfully" -ForegroundColor Green } catch { - Write-Host "Failed to intall Wicrosoft.UI.Xaml.2.7..." -ForegroundColor Red + Write-Host "-> Failed to intall Wicrosoft.UI.Xaml.2.7..." -ForegroundColor Red } Remove-Item -Path $UiXamlZip -Force Remove-Item -Path "$WingetUpdatePath\extracted" -Force -Recurse @@ -245,10 +245,10 @@ function Install-WinGet { try { Write-Host "-> Installing Microsoft.VCLibs.140.00.UWPDesktop..." Add-AppxProvisionedPackage -Online -PackagePath $VCLibsFile -SkipLicense | Out-Null - Write-host "Microsoft.VCLibs.140.00.UWPDesktop installed successfully" -ForegroundColor Green + Write-host "-> Microsoft.VCLibs.140.00.UWPDesktop installed successfully" -ForegroundColor Green } catch { - Write-Host "Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." -ForegroundColor Red + Write-Host "-> Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." -ForegroundColor Red } Remove-Item -Path $VCLibsFile -Force } diff --git a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 index a82219a..50608cc 100644 --- a/Winget-AutoUpdate/functions/Update-StoreApps.ps1 +++ b/Winget-AutoUpdate/functions/Update-StoreApps.ps1 @@ -2,11 +2,14 @@ Function Update-StoreApps ($Log = $false) { + $force_string = "-> Forcing an upgrade of Store Apps (this can take a minute)..." + $fail_string = "-> ...something went wrong!" + #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") { switch ($Log) { - $true {Write-ToLog "-> Forcing an upgrade of Store Apps (this can take a minute)..." "yellow"} - Default {Write-Host "-> Forcing an upgrade of Store Apps (this can take a minute)..." -ForegroundColor Yellow} + $true {Write-ToLog $force_string "yellow"} + Default {Write-Host $force_string -ForegroundColor Yellow} } try { $namespaceName = "root\cimv2\mdm\dmmap" @@ -17,8 +20,8 @@ Function Update-StoreApps ($Log = $false) { } catch { switch ($Log) { - $true {Write-ToLog "-> ...something went wrong!" "red"} - Default {Write-Host "-> ...something went wrong!" -ForegroundColor Red} + $true {Write-ToLog $fail_string "red"} + Default {Write-Host $fail_string -ForegroundColor Red} } return $false } diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 index 61a8c12..197af72 100644 --- a/Winget-AutoUpdate/functions/Update-WinGet.ps1 +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -2,10 +2,16 @@ Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { + $download_string = "-> Downloading WinGet MSIXBundle for App Installer..." + $install_string = "-> Installing WinGet MSIXBundle for App Installer..." + $success_string = "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" + $reset_string = "-> WinGet sources reset." + $fail_string = "-> Failed to intall WinGet MSIXBundle for App Installer..." + #Download WinGet MSIXBundle switch ($Log) { - $true {Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..."} - Default {Write-Host "-> Downloading WinGet MSIXBundle for App Installer..."} + $true {Write-ToLog $download_string} + Default {Write-Host $download_string} } $WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" $WebClient = New-Object System.Net.WebClient @@ -14,21 +20,21 @@ Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { #Install WinGet MSIXBundle in SYSTEM context try { switch ($Log) { - $true {Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..."} - Default {Write-Host "-> Installing WinGet MSIXBundle for App Installer..."} + $true {Write-ToLog $install_string} + Default {Write-Host $install_string} } Add-AppxProvisionedPackage -Online -PackagePath "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null switch ($Log) { - $true {Write-ToLog "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" "green"} - Default {Write-host "WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" -ForegroundColor Green} + $true {Write-ToLog $success_string "green"} + Default {Write-host $success_string -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) { switch ($Log) { - $true {Write-ToLog "-> WinGet sources reset." "green"} - Default {Write-Host "-> WinGet sources reset." -ForegroundColor Green} + $true {Write-ToLog $reset_string "green"} + Default {Write-Host $reset_string -ForegroundColor Green} } #If multiple version, pick last one $WingetPath = $ResolveWingetPath[-1].Path @@ -37,8 +43,8 @@ Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { } catch { switch ($Log) { - $true {Write-ToLog "-> Failed to intall WinGet MSIXBundle for App Installer..." "red"} - Default {Write-Host "Failed to intall WinGet MSIXBundle for App Installer..." -ForegroundColor Red} + $true {Write-ToLog $fail_string "red"} + Default {Write-Host $fail_string -ForegroundColor Red} } Update-StoreApps } From 189830c09fe62d532b8778a0580c401282fca088 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Fri, 13 Oct 2023 22:43:59 +0200 Subject: [PATCH 8/9] Update-StoreApps better in context --- Winget-AutoUpdate/functions/Update-WinGet.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 index 197af72..f042cd8 100644 --- a/Winget-AutoUpdate/functions/Update-WinGet.ps1 +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -40,6 +40,7 @@ Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { $WingetPath = $ResolveWingetPath[-1].Path & $WingetPath source reset --force } + Update-StoreApps } catch { switch ($Log) { From cb82fb148e2d848fc1b10197fcb35f32bd8339a1 Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Sat, 14 Oct 2023 10:29:03 +0200 Subject: [PATCH 9/9] Update-StoreApps only if fail or uncertain --- Winget-AutoUpdate/functions/Update-WinGet.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 index f042cd8..197af72 100644 --- a/Winget-AutoUpdate/functions/Update-WinGet.ps1 +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -40,7 +40,6 @@ Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) { $WingetPath = $ResolveWingetPath[-1].Path & $WingetPath source reset --force } - Update-StoreApps } catch { switch ($Log) {