Improved user notification

pull/228/head
romanitho 2022-10-31 12:37:10 +01:00
parent 0b4495e930
commit 9bccba7bcc
4 changed files with 74 additions and 26 deletions

View File

@ -252,9 +252,16 @@ function Install-WingetAutoUpdate {
} }
} }
# Set dummy regkeys for notification name and icon # Set regkeys for notification name, icon and actions
& reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null
& reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null & reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null
$WAUClass = "HKLM:\Software\Classes\WAU"
$WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate"""
New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath "$WAUClass\shell\open\command" -Name '(default)' -Value $WAUClassRun -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
# Settings for the scheduled task for Updates # Settings for the scheduled task for Updates
$taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`"" $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`""

View File

@ -29,24 +29,33 @@ function Test-WAUisRunning {
} }
} }
<# MAIN #> <# FUNCTIONS #>
#Get Working Dir #Get Working Dir
$Script:WorkingDir = $PSScriptRoot $Script:WorkingDir = $PSScriptRoot
#Load external functions Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName }
. $WorkingDir\functions\Get-NotifLocale.ps1
. $WorkingDir\functions\Start-NotifTask.ps1 function Test-WAUisRunning {
If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -eq 'Running')) {
Return $True
}
}
<# MAIN #>
#Run log initialisation function
Start-Init
Write-Log "User run initiated"
#Get Toast Locale function #Get Toast Locale function
Get-NotifLocale Get-NotifLocale | Out-Null
#Set common variables #Get WingetCmd function
$OnClickAction = "$WorkingDir\logs\updates.log" Get-WingetCmd | Out-Null
$Button1Text = $NotifLocale.local.outputs.output[11].message
$Title = "Winget-AutoUpdate (WAU)" #Get WAU Configurations
$Balise = "Winget-AutoUpdate (WAU)" $Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"
$UserRun = $True
if ($Logs) { if ($Logs) {
if ((Test-Path "$WorkingDir\logs\updates.log")) { if ((Test-Path "$WorkingDir\logs\updates.log")) {
@ -68,22 +77,40 @@ else {
if (Test-WAUisRunning) { if (Test-WAUisRunning) {
$Message = $NotifLocale.local.outputs.output[8].message $Message = $NotifLocale.local.outputs.output[8].message
$MessageType = "warning" $MessageType = "warning"
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss $Button1Text = $NotifLocale.local.outputs.output[11].message
$Button1Action = "$WorkingDir\logs\updates.log"
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $Button1Action -ButtonDismiss
break break
} }
#Run scheduled task
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop #Get Outdated apps
#Starting check - Send notification $Outdated = Get-WingetOutdatedApps
$Message = $NotifLocale.local.outputs.output[6].message $OutdatedApps = @()
$MessageType = "info" #If White List
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss if ($WAUConfig.WAU_UseWhiteList -eq 1) {
#Sleep until the task is done $toUpdate = Get-IncludedApps
While (Test-WAUisRunning) { foreach ($app in $Outdated) {
Start-Sleep 3 if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown") {
$OutdatedApps += $app.Name
}
}
}
#If Black List or default
else {
$toSkip = Get-ExcludedApps
foreach ($app in $Outdated) {
if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") {
$OutdatedApps += $app.Name
}
}
}
$body = $OutdatedApps | Out-String
if ($body) {
Start-NotifTask -Title "New available updates" -Message "Do you want to update these apps ?" -Body $body -ButtonDismiss -Button1Text "Yes" -Button1Action "wau:" -MessageType "info"
}
else {
Start-NotifTask -Title "All good." -Message "No new update available" -MessageType "success"
} }
$Message = $NotifLocale.local.outputs.output[9].message
$MessageType = "success"
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss
} }
catch { catch {
#Check failed - Just send notification #Check failed - Just send notification

View File

@ -89,6 +89,20 @@ function Invoke-PostUpdateActions {
} }
} }
#Add WAU Class to run if not exesting
$WAUClass = "HKLM:\Software\Classes\WAU"
if (!(Test-Path $WAUClass)) {
$WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""C:\Windows\System32\schtasks.exe /run /tn Winget-AutoUpdate"""
New-Item "HKLM:\Software\Classes\$($ActionType)\shell\open\command" -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:$($ActionType)" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath "$WAUClass\shell\open\command" -Name '(default)' -Value $WAUClassRun -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
#log
Write-Log "-> WAU Notification Action declared." "green"
}
#Reset WAU_UpdatePostActions Value #Reset WAU_UpdatePostActions Value
$WAUConfig | New-ItemProperty -Name WAU_PostUpdateActions -Value 0 -Force $WAUConfig | New-ItemProperty -Name WAU_PostUpdateActions -Value 0 -Force

View File

@ -14,7 +14,7 @@ function Start-NotifTask {
[Switch]$ButtonDismiss = $false [Switch]$ButtonDismiss = $false
) )
if (($WAUConfig.WAU_NotificationLevel -eq "Full") -or ($WAUConfig.WAU_NotificationLevel -eq "SuccessOnly" -and $MessageType -eq "Success") -or ($UserRun)) { if (($WAUConfig.WAU_NotificationLevel -eq "Full") -or ($WAUConfig.WAU_NotificationLevel -eq "SuccessOnly" -and $MessageType -eq "Success") -or (!$IsSystem)) {
# XML Toast template creation # XML Toast template creation
[xml]$ToastTemplate = New-Object system.Xml.XmlDocument [xml]$ToastTemplate = New-Object system.Xml.XmlDocument