wingetautoupdate/Winget-AutoUpdate/Winget-Upgrade.ps1

326 lines
13 KiB
PowerShell
Raw Normal View History

2022-10-18 13:23:39 +00:00
<# LOAD FUNCTIONS #>
2023-03-31 15:56:07 +00:00
#Get the Working Dir
2022-03-14 13:55:02 +00:00
$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-05-22 22:26:00 +00:00
#Get WAU Configurations
$Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"
2022-12-28 16:28:22 +00:00
#Log running context and more...
if ($IsSystem) {
2022-12-28 16:28:22 +00:00
Write-Log "Running in System context"
2022-12-28 12:52:03 +00:00
2022-12-30 07:55:27 +00:00
#Get and set Domain/Local Policies (GPO)
2023-01-13 20:42:06 +00:00
$ActivateGPOManagement, $ChangedSettings = Get-Policies
2023-01-14 02:07:34 +00:00
if ($ActivateGPOManagement) {
2023-01-13 20:42:06 +00:00
Write-Log "Activated WAU GPO Management detected, comparing..."
2023-01-14 02:07:34 +00:00
if ($null -ne $ChangedSettings -and $ChangedSettings -ne 0) {
Write-Log "Changed settings detected and applied" "Yellow"
}
else {
Write-Log "No Changed settings detected" "Yellow"
}
2023-01-13 20:42:06 +00:00
}
2023-01-13 05:32:57 +00:00
# Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files.
$MaxLogFiles = $WAUConfig.WAU_MaxLogFiles
if ($null -eq $MaxLogFiles) {
[int32] $MaxLogFiles = 3
}
else {
[int32] $MaxLogFiles = $MaxLogFiles
}
2023-03-31 15:56:07 +00:00
2023-01-13 05:32:57 +00:00
# Maximum size of log file.
$MaxLogSize = $WAUConfig.WAU_MaxLogSize
if (!$MaxLogSize) {
[int64] $MaxLogSize = 1048576 # in bytes, default is 1048576 = 1 MB
}
else {
[int64] $MaxLogSize = $MaxLogSize
}
#LogRotation if System
2023-02-01 02:03:16 +00:00
$LogRotate = Invoke-LogRotation $LogFile $MaxLogFiles $MaxLogSize
if ($LogRotate -eq $False) {
2023-01-13 20:42:06 +00:00
Write-Log "An Exception occured during Log Rotation..."
}
2022-12-28 18:15:43 +00:00
2022-12-28 16:28:22 +00:00
#Run post update actions if necessary if run as System
if (!($WAUConfig.WAU_PostUpdateActions -eq 0)) {
Invoke-PostUpdateActions
}
#Run Scope Machine funtion if run as System
$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-12-28 16:28:22 +00:00
else {
Write-Log "Running in User context"
}
2022-05-08 12:19:41 +00:00
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-12-28 16:28:22 +00:00
#Check if WAU update feature is enabled or not if run as System
2022-10-26 20:10:44 +00:00
$WAUDisableAutoUpdate = $WAUConfig.WAU_DisableAutoUpdate
2022-12-28 16:28:22 +00:00
#If yes then check WAU update if run as System
2022-10-26 19:44:41 +00:00
if ($WAUDisableAutoUpdate -eq 1) {
2023-03-07 15:17:40 +00:00
Write-Log "WAU AutoUpdate is Disabled." "Gray"
2022-10-26 19:44:41 +00:00
}
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-28 16:28:22 +00:00
#Delete previous list_/winget_error (if they exist) if run as 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-12-28 16:28:22 +00:00
#Get External ListPath if run as System
2022-10-13 10:55:20 +00:00
if ($WAUConfig.WAU_ListPath) {
2023-03-05 17:30:33 +00:00
$ListPathClean = $($WAUConfig.WAU_ListPath.TrimEnd(" ", "\", "/"))
Write-Log "WAU uses External Lists from: $ListPathClean"
if ($ListPathClean -ne "GPO") {
$NewList = Test-ListPath $ListPathClean $WAUConfig.WAU_UseWhiteList $WAUConfig.InstallLocation.TrimEnd(" ", "\")
2023-01-19 06:11:20 +00:00
if ($ReachNoPath) {
2023-03-05 17:30:33 +00:00
Write-Log "Couldn't reach/find/compare/copy from $ListPathClean..." "Red"
if ($ListPathClean -notlike "http*") {
if (Test-Path -Path "$ListPathClean" -PathType Leaf) {
Write-Log "PATH must end with a Directory, not a File..." "Red"
}
}
else {
if ($ListPathClean -match "_apps.txt") {
Write-Log "PATH must end with a Directory, not a File..." "Red"
}
2023-03-04 04:36:03 +00:00
}
2023-01-19 06:11:20 +00:00
$Script:ReachNoPath = $False
}
2023-01-19 06:11:20 +00:00
if ($NewList) {
Write-Log "Newer List downloaded/copied to local path: $($WAUConfig.InstallLocation.TrimEnd(" ", "\"))" "Yellow"
2022-10-13 10:55:20 +00:00
}
else {
2023-01-19 06:11:20 +00:00
if ($WAUConfig.WAU_UseWhiteList -and (Test-Path "$WorkingDir\included_apps.txt")) {
Write-Log "List (white) is up to date." "Green"
}
elseif (!$WAUConfig.WAU_UseWhiteList -and (Test-Path "$WorkingDir\excluded_apps.txt")) {
Write-Log "List (black) is up to date." "Green"
}
else {
Write-Log "Critical: White/Black List doesn't exist, exiting..." "Red"
2023-01-25 23:16:47 +00:00
New-Item "$WorkingDir\logs\error.txt" -Value "White/Black List doesn't exist" -Force
2023-01-19 06:11:20 +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
}
2023-03-31 15:56:07 +00:00
2022-12-28 16:28:22 +00:00
#Get External ModsPath if run as System
2022-11-02 17:47:21 +00:00
if ($WAUConfig.WAU_ModsPath) {
2023-03-05 17:30:33 +00:00
$ModsPathClean = $($WAUConfig.WAU_ModsPath.TrimEnd(" ", "\", "/"))
Write-Log "WAU uses External Mods from: $ModsPathClean"
2023-03-07 15:17:40 +00:00
$NewMods, $DeletedMods = Test-ModsPath $ModsPathClean $WAUConfig.InstallLocation.TrimEnd(" ", "\") $WAUConfig.WAU_AzureBlobSASURL.TrimEnd(" ")
2023-01-08 17:57:47 +00:00
if ($ReachNoPath) {
2023-03-05 17:30:33 +00:00
Write-Log "Couldn't reach/find/compare/copy from $ModsPathClean..." "Red"
2023-01-08 17:57:47 +00:00
$Script:ReachNoPath = $False
}
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
2023-01-27 18:13:27 +00:00
if ($($WAUConfig.WAU_ListPath) -eq "GPO") {
2023-01-19 06:11:20 +00:00
$Script:GPOList = $True
}
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
}
2023-01-21 16:14:25 +00:00
#Fix and count the array if GPO List as ERROR handling!
if ($GPOList) {
2023-01-21 16:14:25 +00:00
if ($UseWhiteList) {
2023-01-21 21:30:09 +00:00
$WhiteList = $toUpdate.GetUpperBound(0)
if ($null -eq $WhiteList) {
Write-Log "Critical: Whitelist doesn't exist in GPO, exiting..." "Red"
2023-01-25 23:16:47 +00:00
New-Item "$WorkingDir\logs\error.txt" -Value "Whitelist doesn't exist in GPO" -Force
2023-01-21 16:14:25 +00:00
Exit 1
}
2023-01-21 16:37:03 +00:00
$toUpdate = $toUpdate.Data
2023-01-21 16:14:25 +00:00
}
else {
2023-01-21 21:30:09 +00:00
$BlackList = $toSkip.GetUpperBound(0)
if ($null -eq $BlackList) {
Write-Log "Critical: Blacklist doesn't exist in GPO, exiting..." "Red"
2023-01-25 23:16:47 +00:00
New-Item "$WorkingDir\logs\error.txt" -Value "Blacklist doesn't exist in GPO" -Force
2023-01-21 16:14:25 +00:00
Exit 1
}
2023-01-21 16:37:03 +00:00
$toSkip = $toSkip.Data
2023-01-21 16:14:25 +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
2023-02-04 00:08:38 +00:00
#If something unusual happened
if ($outdated -like "An unusual*") {
Write-Log "$outdated" "cyan"
$outdated = $False
2022-12-11 17:40:06 +00:00
}
2022-12-11 00:31:44 +00:00
2023-02-04 00:08:38 +00:00
#Run only if $outdated is populated!
if ($outdated) {
#Log list of app to update
foreach ($app in $outdated) {
#List available updates
$Log = "-> Available update : $($app.Name). Current version : $($app.Version). Available version : $($app.AvailableVersion)."
$Log | Write-host
$Log | out-file -filepath $LogFile -Append
}
2022-10-26 22:49:10 +00:00
2023-02-04 00:08:38 +00:00
#Count good update installations
$Script:InstallOK = 0
2022-04-13 08:50:24 +00:00
2023-02-04 00:08:38 +00:00
#Trick under user context when -BypassListForUsers is used
if ($IsSystem -eq $false -and $WAUConfig.WAU_BypassListForUsers -eq $true) {
Write-Log "Bypass system list in user context is Enabled."
$UseWhiteList = $false
$toSkip = $null
}
2022-10-10 14:05:36 +00:00
2023-02-04 00:08:38 +00:00
#If White List
if ($UseWhiteList) {
#For each app, notify and update
foreach ($app in $outdated) {
if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown") {
Update-App $app
}
#if current app version is unknown
elseif ($($app.Version) -eq "Unknown") {
Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray"
}
#if app is in "excluded list"
else {
Write-Log "$($app.Name) : Skipped upgrade because it is not in the included app list" "Gray"
}
2022-04-13 08:50:24 +00:00
}
2022-04-05 13:17:18 +00:00
}
2023-02-04 00:08:38 +00:00
#If Black List or default
else {
#For each app, notify and update
foreach ($app in $outdated) {
if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") {
Update-App $app
}
#if current app version is unknown
elseif ($($app.Version) -eq "Unknown") {
Write-Log "$($app.Name) : Skipped upgrade because current version is 'Unknown'" "Gray"
}
#if app is in "excluded list"
else {
Write-Log "$($app.Name) : Skipped upgrade because it is in the excluded app list" "Gray"
}
2022-04-13 08:50:24 +00:00
}
2022-04-05 13:17:18 +00:00
}
2022-10-26 22:49:10 +00:00
2023-02-04 00:08:38 +00:00
if ($InstallOK -gt 0) {
Write-Log "$InstallOK apps updated ! No more update." "Green"
}
2022-04-13 08:50:24 +00:00
}
2023-02-04 00:08:38 +00:00
2023-02-04 13:06:07 +00:00
if ($InstallOK -eq 0 -or !$InstallOK) {
2022-04-13 08:50:24 +00:00
Write-Log "No new update." "Green"
}
2023-02-01 03:06:17 +00:00
#Check if any user is logged on if System and run User task (if installed)
if ($IsSystem) {
#User check routine from: https://stackoverflow.com/questions/23219718/powershell-script-to-see-currently-logged-in-users-domain-and-machine-status
$explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
2023-03-31 15:56:07 +00:00
If ($explorerprocesses.Count -eq 0) {
2023-02-01 03:23:03 +00:00
Write-Log "No explorer process found / Nobody interactively logged on..."
2023-02-01 03:06:17 +00:00
}
2023-03-31 15:56:07 +00:00
Else {
2023-02-01 03:06:17 +00:00
#Run WAU in user context if the user task exist
$UserScheduledTask = Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue
if ($UserScheduledTask) {
2023-02-01 03:06:17 +00:00
#Get Winget system apps to excape them befor running user context
Write-Log "User logged on, get a list of installed Winget apps in System context..."
Get-WingetSystemApps
2023-02-01 03:06:17 +00:00
#Run user context scheduled task
Write-Log "Starting WAU in User context"
Start-ScheduledTask $UserScheduledTask.TaskName -ErrorAction SilentlyContinue
Exit 0
}
2023-03-31 15:56:07 +00:00
elseif (!$UserScheduledTask) {
2023-02-01 03:23:03 +00:00
Write-Log "User context execution not installed..."
2023-02-01 03:06:17 +00:00
}
2023-03-31 15:56:07 +00:00
}
2022-10-26 21:18:42 +00:00
}
}
else {
2023-01-25 23:16:47 +00:00
Write-Log "Critical: Winget not installed or detected, exiting..." "red"
New-Item "$WorkingDir\logs\error.txt" -Value "Winget not installed or detected" -Force
2023-02-01 03:23:03 +00:00
Write-Log "End of process!" "Cyan"
Exit 1
}
}
#End
Write-Log "End of process!" "Cyan"
2022-10-08 00:39:57 +00:00
Start-Sleep 3