wingetautoupdate/Winget-AutoUpdate/user-run.ps1

94 lines
2.4 KiB
PowerShell
Raw Permalink Normal View History

2022-10-11 14:55:17 +00:00
<#
.SYNOPSIS
Handle user interaction from shortcuts and show a Toast
.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,
[Parameter(Mandatory = $False)] [Switch] $Help = $false
2022-10-11 14:55:17 +00:00
)
function Test-WAUisRunning {
2022-10-18 13:23:39 +00:00
If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -ne 'Ready') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -ne 'Ready')) {
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
#Set common variables
2022-10-11 16:04:25 +00:00
$OnClickAction = "$WorkingDir\logs\updates.log"
2022-10-11 14:55:17 +00:00
$Title = "Winget-AutoUpdate (WAU)"
$Balise = "Winget-AutoUpdate (WAU)"
2022-10-11 16:04:25 +00:00
$UserRun = $True
2022-10-11 14:55:17 +00:00
#Get Toast Locale function
Get-NotifLocale
if ($Logs) {
2022-10-11 16:04:25 +00:00
if ((Test-Path "$WorkingDir\logs\updates.log")) {
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"
Start-NotifTask $Title $Message $MessageType $Balise
}
}
elseif ($Help) {
Start-Process "https://github.com/Romanitho/Winget-AutoUpdate"
}
else {
try {
#Check if WAU is currently running
if (Test-WAUisRunning) {
$Message = $NotifLocale.local.outputs.output[8].message
$MessageType = "warning"
Start-NotifTask $Title $Message $MessageType $Balise $OnClickAction
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"
Start-NotifTask $Title $Message $MessageType $Balise $OnClickAction
#Sleep until the task is done
While (Test-WAUisRunning) {
Start-Sleep 3
}
$Message = $NotifLocale.local.outputs.output[9].message
$MessageType = "success"
Start-NotifTask $Title $Message $MessageType $Balise $OnClickAction
2022-10-11 14:55:17 +00:00
}
catch {
#Check failed - Just send notification
$Message = $NotifLocale.local.outputs.output[7].message
$MessageType = "error"
Start-NotifTask $Title $Message $MessageType $Balise
}
}