wingetautoupdate/Winget-AutoUpdate/User-Run.ps1

122 lines
3.3 KiB
PowerShell
Raw 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-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-31 11:37:10 +00:00
<# FUNCTIONS #>
2022-10-11 14:55:17 +00:00
#Get Working Dir
2022-10-11 16:06:21 +00:00
$Script:WorkingDir = $PSScriptRoot
2022-10-11 14:55:17 +00:00
2022-10-31 11:37:10 +00:00
Get-ChildItem "$WorkingDir\functions" | ForEach-Object { . $_.FullName }
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"
2022-10-11 14:55:17 +00:00
2022-10-26 23:52:40 +00:00
#Get Toast Locale function
2022-10-31 11:37:10 +00:00
Get-NotifLocale | Out-Null
#Get WingetCmd function
Get-WingetCmd | Out-Null
2022-10-26 23:52:40 +00:00
2022-10-31 11:37:10 +00:00
#Get WAU Configurations
$Script:WAUConfig = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate"
2022-10-11 14:55:17 +00:00
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"
2022-10-26 23:48:09 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType
2022-10-11 14:55:17 +00:00
}
}
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"
2022-10-31 11:37:10 +00:00
$Button1Text = $NotifLocale.local.outputs.output[11].message
$Button1Action = "$WorkingDir\logs\updates.log"
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $Button1Action -ButtonDismiss
break
}
2022-10-31 11:37:10 +00:00
#Get Outdated apps
$Outdated = Get-WingetOutdatedApps
$OutdatedApps = @()
#If White List
if ($WAUConfig.WAU_UseWhiteList -eq 1) {
$toUpdate = Get-IncludedApps
foreach ($app in $Outdated) {
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"
}
2022-10-11 14:55:17 +00:00
}
catch {
#Check failed - Just send notification
$Message = $NotifLocale.local.outputs.output[7].message
$MessageType = "error"
2022-10-26 23:26:02 +00:00
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss
2022-10-11 14:55:17 +00:00
}
}