Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic refactoring #398

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,201 changes: 664 additions & 537 deletions Winget-AutoUpdate-Install.ps1

Large diffs are not rendered by default.

155 changes: 85 additions & 70 deletions Winget-AutoUpdate/User-Run.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
<#
.SYNOPSIS
Handle user interaction from shortcuts and show a Toast notification
.SYNOPSIS
Handle user interaction from shortcuts and show a Toast notification

.DESCRIPTION
Act on shortcut run (DEFAULT: Check for updated Apps)
.DESCRIPTION
Act on shortcut run (DEFAULT: Check for updated Apps)

.PARAMETER Logs
Open the Log file from Winget-AutoUpdate installation location
.PARAMETER Logs
Open the Log file from Winget-AutoUpdate installation location

.PARAMETER Help
Open the Web Help page
https://github.com/Romanitho/Winget-AutoUpdate
.PARAMETER Help
Open the Web Help page
https://github.com/Romanitho/Winget-AutoUpdate

.EXAMPLE
.\user-run.ps1 -Logs
.EXAMPLE
.\user-run.ps1 -Logs

#>

[CmdletBinding()]
param(
[Parameter(Mandatory = $False)] [Switch] $Logs = $false,
[Parameter(Mandatory = $False)] [Switch] $Help = $false
param (
[Switch]
$Logs = $False,
[Switch]
$Help = $False
)

function Test-WAUisRunning {
If (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate').State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext').State -eq 'Running')) {
Return $True
}
function Test-WAUisRunning
{
if (((Get-ScheduledTask -TaskName 'Winget-AutoUpdate' -ErrorAction SilentlyContinue).State -eq 'Running') -or ((Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue).State -eq 'Running'))
{
return $True
}
}

<# MAIN #>
Expand All @@ -42,61 +46,72 @@ $Script:WorkingDir = $PSScriptRoot
Get-NotifLocale

#Set common variables
$OnClickAction = "$WorkingDir\logs\updates.log"
$OnClickAction = ('{0}\logs\updates.log' -f $WorkingDir)
$Button1Text = $NotifLocale.local.outputs.output[11].message

if ($Logs) {
if (Test-Path "$WorkingDir\logs\updates.log") {
Invoke-Item "$WorkingDir\logs\updates.log"
}
else {
#Not available yet
$Message = $NotifLocale.local.outputs.output[5].message
$MessageType = "warning"
Start-NotifTask -Message $Message -MessageType $MessageType -UserRun
}
if ($Logs)
{
if (Test-Path -Path ('{0}\logs\updates.log' -f $WorkingDir))
{
Invoke-Item -Path ('{0}\logs\updates.log' -f $WorkingDir)
}
else
{
#Not available yet
$Message = $NotifLocale.local.outputs.output[5].message
$MessageType = 'warning'
Start-NotifTask -Message $Message -MessageType $MessageType -UserRun
}
}
elseif ($Help) {
Start-Process "https://github.com/Romanitho/Winget-AutoUpdate"
elseif ($Help)
{
Start-Process -FilePath '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 -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
While (Test-WAUisRunning) {
Start-Sleep 3
}
else
{
try
{
# Check if WAU is currently running
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
while (Test-WAUisRunning)
{
Start-Sleep -Seconds 3
}

#Test if there was a list_/winget_error
if (Test-Path "$WorkingDir\logs\error.txt") {
$MessageType = "error"
$Critical = Get-Content "$WorkingDir\logs\error.txt" -Raw
$Critical = $Critical.Trim()
$Critical = $Critical.Substring(0, [Math]::Min($Critical.Length, 50))
$Message = "Critical:`n$Critical..."
}
else {
$MessageType = "success"
$Message = $NotifLocale.local.outputs.output[9].message
}
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
}
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
}
# Test if there was a list_/winget_error
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)
}
else
{
$MessageType = 'success'
$Message = $NotifLocale.local.outputs.output[9].message
}
Start-NotifTask -Message $Message -MessageType $MessageType -Button1Text $Button1Text -Button1Action $OnClickAction -ButtonDismiss -UserRun
}
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
}
}
144 changes: 78 additions & 66 deletions Winget-AutoUpdate/WAU-Uninstall.ps1
Original file line number Diff line number Diff line change
@@ -1,85 +1,97 @@
<#
.SYNOPSIS
Uninstall Winget-AutoUpdate
.SYNOPSIS
Uninstall Winget-AutoUpdate

.DESCRIPTION
Uninstalls Winget-AutoUpdate (DEFAULT: clean old install)
https://github.com/Romanitho/Winget-AutoUpdate
.DESCRIPTION
Uninstalls Winget-AutoUpdate (DEFAULT: clean old install)
https://github.com/Romanitho/Winget-AutoUpdate

.PARAMETER NoClean
Uninstall Winget-AutoUpdate (keep critical files)
.PARAMETER NoClean
Uninstall Winget-AutoUpdate (keep critical files)

.EXAMPLE
.\WAU-Uninstall.ps1 -NoClean
.EXAMPLE
.\WAU-Uninstall.ps1 -NoClean

#>

[CmdletBinding()]
param(
[Parameter(Mandatory = $False)] [Switch] $NoClean = $false
param (
[Switch]
$NoClean = $false
)

Write-Host "`n"
Write-Host "`t 888 888 d8888 888 888" -ForegroundColor Magenta
Write-Host "`t 888 o 888 d88888 888 888" -ForegroundColor Magenta
Write-Host "`t 888 d8b 888 d88P888 888 888" -ForegroundColor Magenta
Write-Host "`t 888 d888b 888 d88P 888 888 888" -ForegroundColor Magenta
Write-Host "`t 888d88888b888 d88P 888 888 888" -ForegroundColor Magenta
Write-Host "`t 88888P Y88888 d88P 888 888 888" -ForegroundColor Cyan
Write-Host "`t 8888P Y8888 d88P 888 888 888" -ForegroundColor Magenta
Write-Host "`t 888P Y888 d88P 888 Y8888888P`n" -ForegroundColor Magenta
Write-Host "`t Winget-AutoUpdate`n" -ForegroundColor Cyan
Write-Host "`t https://github.com/Romanitho/Winget-AutoUpdate`n" -ForegroundColor Magenta
Write-Host "`t________________________________________________________`n`n"
Write-Host -Object "`n"
Write-Host -Object "`t 888 888 d8888 888 888" -ForegroundColor Magenta
Write-Host -Object "`t 888 o 888 d88888 888 888" -ForegroundColor Magenta
Write-Host -Object "`t 888 d8b 888 d88P888 888 888" -ForegroundColor Magenta
Write-Host -Object "`t 888 d888b 888 d88P 888 888 888" -ForegroundColor Magenta
Write-Host -Object "`t 888d88888b888 d88P 888 888 888" -ForegroundColor Magenta
Write-Host -Object "`t 88888P Y88888 d88P 888 888 888" -ForegroundColor Cyan
Write-Host -Object "`t 8888P Y8888 d88P 888 888 888" -ForegroundColor Magenta
Write-Host -Object "`t 888P Y888 d88P 888 Y8888888P`n" -ForegroundColor Magenta
Write-Host -Object "`t Winget-AutoUpdate`n" -ForegroundColor Cyan
Write-Host -Object "`t https://github.com/Romanitho/Winget-AutoUpdate`n" -ForegroundColor Magenta
Write-Host -Object "`t________________________________________________________`n`n"

try
{
Write-Host -Object 'Uninstalling WAU...' -ForegroundColor Yellow

try {
Write-host "Uninstalling WAU..." -ForegroundColor Yellow
#Get registry install location
$InstallLocation = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\" -Name InstallLocation
# Get registry install location
$InstallLocation = (Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\' -Name InstallLocation)

#Check if installed location exists and delete
if (Test-Path ($InstallLocation)) {
# Check if installed location exists and delete
if (Test-Path -Path ($InstallLocation))
{
if (!$NoClean)
{
$null = (Remove-Item -Path ('{0}\*' -f $InstallLocation) -Force -Confirm:$false -Recurse -Exclude '*.log')
}
else
{
# Keep critical files
$null = (Get-ChildItem -Path $InstallLocation -Exclude *.txt, mods, logs | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue)
}
$null = (Get-ScheduledTask -TaskName 'Winget-AutoUpdate' -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false)
$null = (Get-ScheduledTask -TaskName 'Winget-AutoUpdate-Notify' -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false)
$null = (Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false)
$null = & "$env:windir\system32\reg.exe" delete 'HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification' /f
$null = & "$env:windir\system32\reg.exe" delete 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate' /f

if (!$NoClean) {
Remove-Item "$InstallLocation\*" -Force -Recurse -Exclude "*.log"
}
else {
#Keep critical files
Get-ChildItem -Path $InstallLocation -Exclude *.txt, mods, logs | Remove-Item -Recurse -Force
}
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
Get-ScheduledTask -TaskName "Winget-AutoUpdate-UserContext" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
& reg delete "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /f | Out-Null
& reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" /f | Out-Null
if (Test-Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate") {
& reg delete "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate" /f | Out-Null
}
if (Test-Path -Path 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate' -ErrorAction SilentlyContinue)
{
$null = & "$env:windir\system32\reg.exe" delete 'HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate' /f
}

if ((Test-Path "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs\Winget-AutoUpdate (WAU)")) {
Remove-Item -Path "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs\Winget-AutoUpdate (WAU)" -Recurse -Force | Out-Null
}
if ((Test-Path -Path "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs\Winget-AutoUpdate (WAU)"))
{
$null = (Remove-Item -Path "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs\Winget-AutoUpdate (WAU)" -Recurse -Force -Confirm:$false)
}

if ((Test-Path "${env:Public}\Desktop\WAU - Check for updated Apps.lnk")) {
Remove-Item -Path "${env:Public}\Desktop\WAU - Check for updated Apps.lnk" -Force | Out-Null
}
if ((Test-Path -Path "${env:Public}\Desktop\WAU - Check for updated Apps.lnk"))
{
$null = (Remove-Item -Path "${env:Public}\Desktop\WAU - Check for updated Apps.lnk" -Force -Confirm:$false)
}

#Remove Intune Logs if they are existing
if (Test-Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log") {
Remove-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -Force -ErrorAction SilentlyContinue | Out-Null
}
if (Test-Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log") {
Remove-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -Force -ErrorAction SilentlyContinue | Out-Null
}
# Remove Intune Logs if they are existing
if (Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log")
{
$null = (Remove-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -Force -Confirm:$false -ErrorAction SilentlyContinue)
}
if (Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ErrorAction SilentlyContinue)
{
$null = (Remove-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -Force -Confirm:$false -ErrorAction SilentlyContinue)
}

Write-host "Uninstallation succeeded!" -ForegroundColor Green
}
else {
Write-host "$InstallLocation not found! Uninstallation failed!" -ForegroundColor Red
}
Write-Host -Object 'Uninstallation succeeded!' -ForegroundColor Green
}
else
{
Write-Host -Object ('{0} not found! Uninstallation failed!' -f $InstallLocation) -ForegroundColor Red
}
}
catch {
Write-host "`nUninstallation failed! Run as admin ?" -ForegroundColor Red
catch
{
Write-Host -Object "`nUninstallation failed! Run as admin ?" -ForegroundColor Red
}

Start-sleep 2
Start-Sleep -Seconds 2
33 changes: 19 additions & 14 deletions Winget-AutoUpdate/Winget-Notify.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#Send Notify Script
# Send Notify Script

#get xml notif config
$WAUinstalledPath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\" -Name InstallLocation
[xml]$NotifConf = Get-Content "$WAUinstalledPath\config\notif.xml" -Encoding UTF8 -ErrorAction SilentlyContinue
if (!($NotifConf)) {
break
# get xml notif config
$WAUinstalledPath = (Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Winget-AutoUpdate\' -Name InstallLocation)
[xml]$NotifConf = (Get-Content -Path "$WAUinstalledPath\config\notif.xml" -Encoding UTF8 -ErrorAction SilentlyContinue)

if (!($NotifConf))
{
break
}

#Load Assemblies
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
# Load Assemblies
$null = (Add-Type -AssemblyName Windows.Data)
$null = (Add-Type -AssemblyName Windows.UI)

$null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]

#Prepare XML
# Prepare XML
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
$ToastXml.LoadXml($NotifConf.OuterXml)

#Specify Launcher App ID
$LauncherID = "Windows.SystemToast.Winget.Notification"
# Specify Launcher App ID
$LauncherID = 'Windows.SystemToast.Winget.Notification'

#Prepare and Create Toast
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXML)
# Prepare and Create Toast
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXml)
$ToastMessage.Tag = $NotifConf.toast.tag
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)
Loading