wingetautoupdate/Winget-AutoUpdate/Winget-Upgrade.ps1

295 lines
13 KiB
PowerShell
Raw Normal View History

2022-10-18 13:23:39 +00:00
<# LOAD FUNCTIONS #>
2022-03-14 13:55:02 +00:00
#Get Working Dir
$Script:WorkingDir = $PSScriptRoot
#Get Functions
2022-06-10 08:26:41 +00:00
Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName }
2022-03-22 13:39:01 +00:00
<# MAIN #>
#Check if running account is system or interactive logon
2022-10-08 00:53:52 +00:00
$Script:IsSystem = [System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem
2022-03-14 13:55:02 +00:00
#Run log initialisation function
Start-Init
2022-10-08 00:39:57 +00:00
#Log running context
if ($IsSystem) {
Write-Log "Running in System context"
}
2022-10-18 13:23:39 +00:00
else {
2022-10-08 00:44:57 +00:00
Write-Log "Running in User context"
2022-10-08 00:39:57 +00:00
}
2022-05-22 22:26:00 +00:00
#Get WAU Configurations
$Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"
if ($IsSystem) {
#Get WAU Policies and set the Configurations Registry Accordingly
$WAUPolicies = Get-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Romanitho\Winget-AutoUpdate" -ErrorAction SilentlyContinue
2022-12-27 23:41:11 +00:00
if ($WAUPolicies) {
2022-12-28 02:31:26 +00:00
if ($WAUPolicies.WAU_ActivateGPOManagement -eq 1) {
2022-12-28 14:08:10 +00:00
Write-Log "Activated WAU GPO Management detected, comparing..."
2022-12-28 02:31:26 +00:00
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"
if ($null -ne $WAUPolicies.WAU_BypassListForUsers -and ($WAUPolicies.WAU_BypassListForUsers -ne $WAUConfig.WAU_BypassListForUsers)) {
New-ItemProperty $regPath -Name WAU_BypassListForUsers -Value $WAUPolicies.WAU_BypassListForUsers -PropertyType DWord -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_BypassListForUsers) {
Remove-ItemProperty $regPath"\" -Name WAU_BypassListForUsers -Force -ErrorAction SilentlyContinue | Out-Null
}
if ($null -ne $WAUPolicies.WAU_DisableAutoUpdate -and ($WAUPolicies.WAU_DisableAutoUpdate -ne $WAUConfig.WAU_DisableAutoUpdate)) {
New-ItemProperty $regPath -Name WAU_DisableAutoUpdate -Value $WAUPolicies.WAU_DisableAutoUpdate -PropertyType DWord -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_DisableAutoUpdate) {
Remove-ItemProperty $regPath"\" -Name WAU_DisableAutoUpdate -Force -ErrorAction SilentlyContinue | Out-Null
}
if ($null -ne $WAUPolicies.WAU_DoNotRunOnMetered -and ($WAUPolicies.WAU_DoNotRunOnMetered -ne $WAUConfig.WAU_DoNotRunOnMetered)) {
New-ItemProperty $regPath -Name WAU_DoNotRunOnMetered -Value $WAUPolicies.WAU_DoNotRunOnMetered -PropertyType DWord -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_DoNotRunOnMetered) {
New-ItemProperty $regPath -Name WAU_DoNotRunOnMetered -Value 1 -PropertyType DWord -Force | Out-Null
}
if ($null -ne $WAUPolicies.WAU_UpdatePrerelease -and ($WAUPolicies.WAU_UpdatePrerelease -ne $WAUConfig.WAU_UpdatePrerelease)) {
New-ItemProperty $regPath -Name WAU_UpdatePrerelease -Value $WAUPolicies.WAU_UpdatePrerelease -PropertyType DWord -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_UpdatePrerelease) {
New-ItemProperty $regPath -Name WAU_UpdatePrerelease -Value 0 -PropertyType DWord -Force | Out-Null
}
if ($null -ne $WAUPolicies.WAU_UseWhiteList -and ($WAUPolicies.WAU_UseWhiteList -ne $WAUConfig.WAU_UseWhiteList)) {
New-ItemProperty $regPath -Name WAU_UseWhiteList -Value $WAUPolicies.WAU_UseWhiteList -PropertyType DWord -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_UseWhiteList) {
Remove-ItemProperty $regPath"\" -Name WAU_UseWhiteList -Force -ErrorAction SilentlyContinue | Out-Null
}
if ($null -ne $WAUPolicies.WAU_ListPath -and ($WAUPolicies.WAU_ListPath -ne $WAUConfig.WAU_ListPath)) {
New-ItemProperty $regPath -Name WAU_ListPath -Value $WAUPolicies.WAU_ListPath -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_ListPath) {
Remove-ItemProperty $regPath"\" -Name WAU_ListPath -Force -ErrorAction SilentlyContinue | Out-Null
}
if ($null -ne $WAUPolicies.WAU_ModsPath -and ($WAUPolicies.WAU_ModsPath -ne $WAUConfig.WAU_ModsPath)) {
New-ItemProperty $regPath -Name WAU_ModsPath -Value $WAUPolicies.WAU_ModsPath -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_ModsPath) {
Remove-ItemProperty $regPath"\" -Name WAU_ModsPath -Force -ErrorAction SilentlyContinue | Out-Null
}
2022-12-28 12:52:03 +00:00
if ($null -ne $WAUPolicies.WAU_NotificationLevel -and ($WAUPolicies.WAU_NotificationLevel -ne $WAUConfig.WAU_NotificationLevel)) {
New-ItemProperty $regPath -Name WAU_NotificationLevel -Value $WAUPolicies.WAU_NotificationLevel -Force | Out-Null
}
elseif ($null -eq $WAUPolicies.WAU_NotificationLevel) {
New-ItemProperty $regPath -Name WAU_NotificationLevel -Value "Full" -Force | Out-Null
}
2022-12-28 00:47:04 +00:00
}
}
}
2022-05-22 22:39:57 +00:00
#Run post update actions if necessary
2022-06-10 08:26:41 +00:00
if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) {
2022-05-22 22:35:33 +00:00
Invoke-PostUpdateActions
2022-05-22 22:26:00 +00:00
}
2022-05-08 12:19:41 +00:00
#Run Scope Machine funtion if run as system
if ($IsSystem) {
$SettingsPath = "$Env:windir\system32\config\systemprofile\AppData\Local\Microsoft\WinGet\Settings\defaultState\settings.json"
2022-05-08 12:19:41 +00:00
Add-ScopeMachine $SettingsPath
}
2022-03-14 13:55:02 +00:00
#Get Notif Locale function
2022-10-11 08:41:35 +00:00
$LocaleDisplayName = Get-NotifLocale
Write-Log "Notification Level: $($WAUConfig.WAU_NotificationLevel). Notification Language: $LocaleDisplayName" "Cyan"
#Check network connectivity
2022-06-10 08:26:41 +00:00
if (Test-Network) {
2022-04-13 16:50:06 +00:00
#Check if Winget is installed and get Winget cmd
2022-04-13 08:50:24 +00:00
$TestWinget = Get-WingetCmd
2022-10-26 22:49:10 +00:00
2022-06-10 08:26:41 +00:00
if ($TestWinget) {
2022-04-13 08:50:24 +00:00
#Get Current Version
2022-05-22 13:27:45 +00:00
$WAUCurrentVersion = $WAUConfig.DisplayVersion
Write-Log "WAU current version: $WAUCurrentVersion"
2022-10-26 19:44:41 +00:00
if ($IsSystem) {
2022-10-26 20:10:44 +00:00
#Check if WAU update feature is enabled or not
$WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate
#If yes then check WAU update if System
2022-10-26 19:44:41 +00:00
if ($WAUDisableAutoUpdate -eq 1) {
Write-Log "WAU AutoUpdate is Disabled." "Grey"
}
else {
Write-Log "WAU AutoUpdate is Enabled." "Green"
#Get Available Version
$WAUAvailableVersion = Get-WAUAvailableVersion
#Compare
if ([version]$WAUAvailableVersion -gt [version]$WAUCurrentVersion) {
#If new version is available, update it
Write-Log "WAU Available version: $WAUAvailableVersion" "Yellow"
Update-WAU
}
2022-10-18 13:23:39 +00:00
else {
2022-10-26 19:44:41 +00:00
Write-Log "WAU is up to date." "Green"
}
2022-04-13 08:50:24 +00:00
}
2022-12-12 00:28:38 +00:00
#Delete previous list_/winget_error (if they exist) if System
2022-12-22 21:26:34 +00:00
if (Test-Path "$WorkingDir\logs\error.txt") {
Remove-Item "$WorkingDir\logs\error.txt" -Force
2022-12-12 00:28:38 +00:00
}
2022-10-26 20:10:44 +00:00
#Get External ListPath if System
2022-10-13 10:55:20 +00:00
if ($WAUConfig.WAU_ListPath) {
2022-12-28 15:07:18 +00:00
Write-Log "WAU uses External Lists from: $($WAUConfig.WAU_ListPath.TrimEnd(" ", "\", "/"))"
$NewList = Test-ListPath $WAUConfig.WAU_ListPath.TrimEnd(" ", "\", "/") $WAUConfig.WAU_UseWhiteList $WAUConfig.InstallLocation.TrimEnd(" ", "\")
2022-10-13 10:55:20 +00:00
if ($NewList) {
2022-12-28 15:07:18 +00:00
Write-Log "Newer List downloaded/copied to local path: $($WAUConfig.InstallLocation.TrimEnd(" ", "\"))" "Yellow"
2022-10-11 21:54:17 +00:00
}
else {
2022-10-13 10:55:20 +00:00
if ((Test-Path "$WorkingDir\included_apps.txt") -or (Test-Path "$WorkingDir\excluded_apps.txt")) {
Write-Log "List is up to date." "Green"
}
else {
2022-12-11 21:22:22 +00:00
Write-Log "Critical: List doesn't exist, exiting..." "Red"
2022-12-22 21:26:34 +00:00
New-Item "$WorkingDir\logs\error.txt" -Value "List doesn't exist!" -Force
2022-12-11 00:31:44 +00:00
Exit 1
2022-10-13 10:55:20 +00:00
}
2022-10-11 21:54:17 +00:00
}
2022-09-25 14:54:58 +00:00
}
2022-11-02 17:47:21 +00:00
#Get External ModsPath if System
if ($WAUConfig.WAU_ModsPath) {
2022-12-28 15:07:18 +00:00
Write-Log "WAU uses External Mods from: $($WAUConfig.WAU_ModsPath.TrimEnd(" ", "\", "/"))"
$NewMods, $DeletedMods = Test-ModsPath $WAUConfig.WAU_ModsPath.TrimEnd(" ", "\", "/") $WAUConfig.InstallLocation.TrimEnd(" ", "\")
2022-11-03 00:19:04 +00:00
if ($NewMods -gt 0) {
2022-12-28 15:07:18 +00:00
Write-Log "$NewMods newer Mods downloaded/copied to local path: $($WAUConfig.InstallLocation.TrimEnd(" ", "\"))\mods" "Yellow"
2022-11-02 17:47:21 +00:00
}
else {
if (Test-Path "$WorkingDir\mods\*.ps1") {
2022-11-02 18:58:38 +00:00
Write-Log "Mods are up to date." "Green"
2022-11-02 17:47:21 +00:00
}
else {
2022-11-02 19:30:07 +00:00
Write-Log "No Mods are implemented..." "Yellow"
2022-11-02 17:47:21 +00:00
}
}
2022-11-05 00:12:13 +00:00
if ($DeletedMods -gt 0) {
2022-12-28 15:07:18 +00:00
Write-Log "$DeletedMods Mods deleted (not externally managed) from local path: $($WAUConfig.InstallLocation.TrimEnd(" ", "\"))\mods" "Red"
2022-11-05 00:12:13 +00:00
}
2022-11-02 17:47:21 +00:00
}
2022-12-11 17:40:06 +00:00
}
2022-12-11 16:42:05 +00:00
2022-04-13 08:50:24 +00:00
#Get White or Black list
2022-06-10 08:26:41 +00:00
if ($WAUConfig.WAU_UseWhiteList -eq 1) {
2022-04-24 08:51:51 +00:00
Write-Log "WAU uses White List config"
2022-04-13 08:50:24 +00:00
$toUpdate = Get-IncludedApps
2022-05-22 13:27:45 +00:00
$UseWhiteList = $true
2022-03-22 13:39:01 +00:00
}
2022-06-10 08:26:41 +00:00
else {
2022-04-24 08:51:51 +00:00
Write-Log "WAU uses Black List config"
2022-04-13 08:50:24 +00:00
$toSkip = Get-ExcludedApps
2022-03-26 21:48:56 +00:00
}
2022-04-13 08:50:24 +00:00
#Get outdated Winget packages
2022-10-30 15:21:25 +00:00
Write-Log "Checking application updates on Winget Repository..." "yellow"
2022-04-13 08:50:24 +00:00
$outdated = Get-WingetOutdatedApps
2022-04-05 13:17:18 +00:00
2022-12-12 00:28:38 +00:00
#If something is wrong with the winget source, exit
if ($outdated -like "Problem:*") {
2022-12-12 01:52:46 +00:00
Write-Log "Critical: An error occured, exiting..." "red"
2022-12-11 00:31:44 +00:00
Write-Log "$outdated" "red"
2022-12-22 21:26:34 +00:00
New-Item "$WorkingDir\logs\error.txt" -Value "$outdated" -Force
2022-12-11 17:40:06 +00:00
Exit 1
}
2022-12-11 00:31:44 +00:00
2022-04-13 08:50:24 +00:00
#Log list of app to update
2022-06-10 08:26:41 +00:00
foreach ($app in $outdated) {
2022-04-13 08:50:24 +00:00
#List available updates
2022-05-23 07:35:49 +00:00
$Log = "-> Available update : $($app.Name). Current version : $($app.Version). Available version : $($app.AvailableVersion)."
2022-04-13 08:50:24 +00:00
$Log | Write-host
$Log | out-file -filepath $LogFile -Append
2022-02-21 13:26:30 +00:00
}
2022-10-26 22:49:10 +00:00
2022-04-13 08:50:24 +00:00
#Count good update installations
$Script:InstallOK = 0
2022-10-10 14:05:36 +00:00
#Trick under user context when -BypassListForUsers is used
2022-10-18 13:23:39 +00:00
if ($IsSystem -eq $false -and $WAUConfig.WAU_BypassListForUsers -eq $true) {
2022-10-10 15:41:33 +00:00
Write-Log "Bypass system list in user context is Enabled."
2022-10-10 14:05:36 +00:00
$UseWhiteList = $false
$toSkip = $null
}
2022-04-13 08:50:24 +00:00
#If White List
2022-06-10 08:26:41 +00:00
if ($UseWhiteList) {
2022-04-13 08:50:24 +00:00
#For each app, notify and update
2022-06-10 08:26:41 +00:00
foreach ($app in $outdated) {
if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown") {
2022-04-13 08:50:24 +00:00
Update-App $app
}
#if current app version is unknown
2022-06-10 08:26:41 +00:00
elseif ($($app.Version) -eq "Unknown") {
2022-04-13 08:50:24 +00:00
Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray"
}
#if app is in "excluded list"
2022-06-10 08:26:41 +00:00
else {
2022-04-13 08:50:24 +00:00
Write-Log "$($app.Name) : Skipped upgrade because it is not in the included app list" "Gray"
}
2022-04-05 13:17:18 +00:00
}
2022-04-13 08:50:24 +00:00
}
2022-05-22 13:27:45 +00:00
#If Black List or default
2022-06-10 08:26:41 +00:00
else {
2022-04-13 08:50:24 +00:00
#For each app, notify and update
2022-06-10 08:26:41 +00:00
foreach ($app in $outdated) {
if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") {
2022-04-13 08:50:24 +00:00
Update-App $app
}
#if current app version is unknown
2022-06-10 08:26:41 +00:00
elseif ($($app.Version) -eq "Unknown") {
2022-04-13 08:50:24 +00:00
Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray"
}
#if app is in "excluded list"
2022-06-10 08:26:41 +00:00
else {
2022-04-13 08:50:24 +00:00
Write-Log "$($app.Name) : Skipped upgrade because it is in the excluded app list" "Gray"
}
2022-04-05 13:17:18 +00:00
}
}
2022-10-26 22:49:10 +00:00
2022-06-10 08:26:41 +00:00
if ($InstallOK -gt 0) {
2022-04-13 08:50:24 +00:00
Write-Log "$InstallOK apps updated ! No more update." "Green"
}
2022-06-10 08:26:41 +00:00
if ($InstallOK -eq 0) {
2022-04-13 08:50:24 +00:00
Write-Log "No new update." "Green"
}
2022-10-26 20:43:13 +00:00
#Run WAU in user context if currently as system and the user task exist
$UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue
if ($IsSystem -and $UserScheduledTask) {
2022-10-26 19:58:36 +00:00
#Get Winget system apps to excape them befor running user context
2022-10-26 21:18:42 +00:00
Write-Log "Get list of installed Winget apps in System context..."
2022-10-26 19:58:36 +00:00
Get-WingetSystemApps
2022-10-26 19:58:36 +00:00
#Run user context scheduled task
2022-10-26 20:43:13 +00:00
Write-Log "Starting WAU in User context"
Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue
Exit 0
2022-10-26 19:44:41 +00:00
}
2022-10-26 21:18:42 +00:00
elseif (!$UserScheduledTask){
Write-Log "User context execution not installed"
}
}
else {
Write-Log "Critical: An error occured, exiting..." "red"
2022-12-22 21:26:34 +00:00
New-Item "$WorkingDir\logs\error.txt" -Value "Winget not installed or detected!" -Force
Exit 1
}
}
#End
Write-Log "End of process!" "Cyan"
2022-10-08 00:39:57 +00:00
Start-Sleep 3