wingetautoupdate/Winget-AutoUpdate/User-Run.ps1

118 lines
3.5 KiB
PowerShell
Raw Normal View History

2022-10-11 14:55:17 +00:00
<#
.SYNOPSIS
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)
2022-10-11 14:55:17 +00:00
.PARAMETER Logs
Open the Log file from Winget-AutoUpdate installation location
2022-10-11 14:55:17 +00:00
.PARAMETER Help
Open the Web Help page
https://github.com/Romanitho/Winget-AutoUpdate
2022-10-11 14:55:17 +00:00
.EXAMPLE
.\user-run.ps1 -Logs
2022-10-11 14:55:17 +00:00
#>
[CmdletBinding()]
2023-09-15 14:38:54 +00:00
param (
[Switch]
$Logs = $False,
[Switch]
$Help = $False
2022-10-11 14:55:17 +00:00
)
2023-09-15 14:38:54 +00:00
function Test-WAUisRunning
{
2023-09-15 14:38:54 +00:00
if (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate' -ErrorAction SilentlyContinue).State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue).State -eq 'Running'))
{
2023-09-15 14:38:54 +00:00
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
$OnClickAction = ('{0}\logs\updates.log' -f $WorkingDir)
2022-10-26 23:26:02 +00:00
$Button1Text = $NotifLocale.local.outputs.output[11].message
2022-10-11 14:55:17 +00:00
2023-09-15 14:38:54 +00:00
if ($Logs)
{
2023-09-15 14:38:54 +00:00
if (Test-Path -Path ('{0}\logs\updates.log' -f $WorkingDir))
{
Invoke-Item -Path ('{0}\logs\updates.log' -f $WorkingDir)
}
2023-09-15 14:38:54 +00:00
else
{
#Not available yet
$Message = $NotifLocale.local.outputs.output[5].message
$MessageType = 'warning'
Start-NotifTask -Message $Message -MessageType $MessageType -UserRun
}
2022-10-11 14:55:17 +00:00
}
2023-09-15 14:38:54 +00:00
elseif ($Help)
{
Start-Process -FilePath 'https://github.com/Romanitho/Winget-AutoUpdate'
2022-10-11 14:55:17 +00:00
}
2023-09-15 14:38:54 +00:00
else
{
2023-09-15 14:38:54 +00:00
try
{
# Check if WAU is currently running
2023-09-15 14:38:54 +00:00
if (Test-WAUisRunning)
{
$Message = $NotifLocale.local.outputs.output[8].message
$MessageType = 'warning'
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
break
}
# Run scheduled task
Get-ScheduledTask -TaskName 'Winget-AutoUpdate' -ErrorAction Stop | Start-ScheduledTask -ErrorAction Stop
# Starting check - Send notification
$Message = $NotifLocale.local.outputs.output[6].message
$MessageType = 'info'
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
# Sleep until the task is done
2023-09-15 14:38:54 +00:00
while (Test-WAUisRunning)
{
Start-Sleep -Seconds 3
}
# Test if there was a list_/winget_error
2023-09-15 14:38:54 +00:00
if (Test-Path -Path ('{0}\logs\error.txt' -f $WorkingDir) -ErrorAction SilentlyContinue)
{
$MessageType = 'error'
$Critical = Get-Content -Path ('{0}\logs\error.txt' -f $WorkingDir) -Raw
$Critical = $Critical.Trim()
$Critical = $Critical.Substring(0, [Math]::Min($Critical.Length, 50))
$Message = ("Critical:`n{0}..." -f $Critical)
}
2023-09-15 14:38:54 +00:00
else
{
$MessageType = 'success'
$Message = $NotifLocale.local.outputs.output[9].message
}
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
}
2023-09-15 14:38:54 +00:00
catch
{
# Check failed - Just send notification
$Message = $NotifLocale.local.outputs.output[7].message
$MessageType = 'error'
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
}
2022-10-11 14:55:17 +00:00
}