wingetautoupdate/Winget-AutoUpdate/User-Run.ps1

138 lines
4.7 KiB
PowerShell
Raw Normal View History

2022-10-11 14:55:17 +00:00
<#
.SYNOPSIS
2023-03-31 15:56:07 +00:00
Handle user interaction from shortcuts and show a Toast notification
2022-10-11 14:55:17 +00:00
.DESCRIPTION
Act on shortcut run (DEFAULT: Check for updated Apps)
.PARAMETER Logs
Open the Log file from Winget-AutoUpdate installation location
.PARAMETER Help
Open the Web Help page
https://github.com/Romanitho/Winget-AutoUpdate
.EXAMPLE
.\user-run.ps1 -Logs
#>
[CmdletBinding()]
param(
2022-10-18 13:23:39 +00:00
[Parameter(Mandatory = $False)] [Switch] $Logs = $false,
2023-02-06 18:16:01 +00:00
[Parameter(Mandatory = $False)] [Switch] $Help = $false,
2023-04-23 15:00:25 +00:00
[Parameter(Mandatory = $False)] [String] $NotifApproved
2022-10-11 14:55:17 +00:00
)
function Test-WAUisRunning {
2022-10-25 00:05:50 +00:00
If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -eq 'Running')) {
Return $True
}
}
2022-10-11 14:55:17 +00:00
<# MAIN #>
#Get Working Dir
2022-10-11 16:06:21 +00:00
$Script:WorkingDir = $PSScriptRoot
2022-10-11 14:55:17 +00:00
#Load external functions
2022-10-11 16:04:25 +00:00
. $WorkingDir\functions\Get-NotifLocale.ps1
. $WorkingDir\functions\Start-NotifTask.ps1
2022-10-11 14:55:17 +00:00
2022-10-26 23:52:40 +00:00
#Get Toast Locale function
Get-NotifLocale
2022-10-11 14:55:17 +00:00
#Set common variables
2022-10-11 16:04:25 +00:00
$OnClickAction = "$WorkingDir\logs\updates.log"
2022-10-26 23:26:02 +00:00
$Button1Text = $NotifLocale.local.outputs.output[11].message
2022-10-11 14:55:17 +00:00
if ($Logs) {
2022-12-11 16:42:05 +00:00
if (Test-Path "$WorkingDir\logs\updates.log") {
2022-10-11 16:04:25 +00:00
Invoke-Item "$WorkingDir\logs\updates.log"
2022-10-11 14:55:17 +00:00
}
else {
#Not available yet
$Message = $NotifLocale.local.outputs.output[5].message
$MessageType = "warning"
2023-04-10 19:10:15 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType -UserRun
2022-10-11 14:55:17 +00:00
}
}
elseif ($Help) {
Start-Process "https://github.com/Romanitho/Winget-AutoUpdate"
}
2023-04-23 14:14:46 +00:00
elseif ($NotifApproved){
2023-04-23 23:49:20 +00:00
$MessageBody = "Do you want to update these apps ?`n`n"
$MessageBody += Get-Content "$WorkingDir/config/NotifContent.txt" -Raw
$Title = "Winget-AutoUpdate"
if ($NotifApproved -eq "wau:systemDialogBox"){
Add-Type -AssemblyName PresentationCore,PresentationFramework
$Result = [System.Windows.MessageBox]::Show($MessageBody,$Title,4,32)
if ($Result -eq "Yes") {
$NotifApproved = "wau:system"
}
}
if ($NotifApproved -eq "wau:userDialogBox"){
Add-Type -AssemblyName PresentationCore,PresentationFramework
$Result = [System.Windows.MessageBox]::Show($MessageBody,$Title,4,32)
if ($Result -eq "Yes") {
$NotifApproved = "wau:user"
}
}
if ($NotifApproved -eq "wau:system"){
#Create tag if user approve notif for requested updates
$WAUNotifApprovedPath = "$WorkingDir\config\NotifApproved.txt"
New-Item $WAUNotifApprovedPath -Force
2023-04-23 14:14:46 +00:00
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop
}
2023-04-23 23:49:20 +00:00
if ($NotifApproved -eq "wau:user"){
#Create tag if user approve notif for requested updates
$WAUNotifApprovedPath = "$WorkingDir\config\NotifApproved.txt"
New-Item $WAUNotifApprovedPath -Force
2023-04-23 14:14:46 +00:00
Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop
}
2023-02-06 18:16:01 +00:00
}
2022-10-11 14:55:17 +00:00
else {
try {
#Check if WAU is currently running
if (Test-WAUisRunning) {
$Message = $NotifLocale.local.outputs.output[8].message
$MessageType = "warning"
2023-04-23 23:49:20 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -UserRun
break
}
2022-10-17 14:47:22 +00:00
#Run scheduled task
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop
2022-10-11 14:55:17 +00:00
#Starting check - Send notification
$Message = $NotifLocale.local.outputs.output[6].message
$MessageType = "info"
2023-04-23 23:49:20 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -UserRun
#Sleep until the task is done
While (Test-WAUisRunning) {
Start-Sleep 3
}
2022-12-11 17:40:06 +00:00
2022-12-12 00:28:38 +00:00
#Test if there was a list_/winget_error
2022-12-22 21:26:34 +00:00
if (Test-Path "$WorkingDir\logs\error.txt") {
2022-12-11 16:42:05 +00:00
$MessageType = "error"
$Critical = Get-Content "$WorkingDir\logs\error.txt" -Raw
2023-01-27 18:36:06 +00:00
$Critical = $Critical.Trim()
$Critical = $Critical.Substring(0, [Math]::Min($Critical.Length, 50))
2023-01-25 23:16:47 +00:00
$Message = "Critical:`n$Critical..."
2022-12-11 17:40:06 +00:00
}
2022-12-11 16:42:05 +00:00
else {
$MessageType = "success"
$Message = $NotifLocale.local.outputs.output[9].message
2022-12-11 16:42:05 +00:00
}
2023-04-23 15:00:25 +00:00
$IsUserApprovalEnable = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\" -Name WAU_UserApproval -ErrorAction SilentlyContinue).WAU_UserApproval
if ($IsUserApprovalEnable -ne "1"){
2023-04-23 23:49:20 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -UserRun
2023-04-23 15:00:25 +00:00
}
2022-10-11 14:55:17 +00:00
}
catch {
#Check failed - Just send notification
$Message = $NotifLocale.local.outputs.output[7].message
$MessageType = "error"
2023-04-23 23:49:20 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -UserRun
2022-10-11 14:55:17 +00:00
}
}