better way

draft-user-approval
Romain 2023-05-10 17:51:18 +02:00 committed by GitHub
parent ddcd23b2d6
commit 25e02c23d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 70 deletions

View File

@ -62,7 +62,7 @@ elseif ($Help) {
}
elseif ($NotifApproved){
$MessageBody = "Do you want to update these apps ?`n`n"
$MessageBody += Get-Content "$WorkingDir/config/NotifContent.txt" -Raw
$MessageBody += (Import-Csv "$WorkingDir/Config/NotifAppList.csv").name
$Title = "Winget-AutoUpdate"
if ($NotifApproved -eq "wau:systemDialogBox"){
Add-Type -AssemblyName PresentationCore,PresentationFramework

View File

@ -236,29 +236,6 @@ if (Test-Network) {
$Log | out-file -filepath $LogFile -Append
}
#Ask user to approve, if configured
if ($WAUConfig.WAU_UserApproval -eq 1){
Write-ToLog "User Approval feature enabled."
#Check for approved tag
$WAUNotifApproved = "$WorkingDir/Config/NotifApproved.txt"
if (Test-Path $WAUNotifApproved) {
Write-ToLog "-> User approved update notification."
Remove-Item $WAUNotifApproved -Force -Confirm:$false
}
else {
$UserApprovalReturn = Invoke-UserApproval $outdated
if ($UserApprovalReturn -eq 0){
Write-ToLog "-> User approval requested. Waiting for user to approve available updates... Closing for now."
#Closing job, waiting for user approval
Exit 0
}
else{
Write-ToLog "-> No update to request to user."
}
}
}
#Count good update installations
$Script:InstallOK = 0
@ -269,12 +246,14 @@ if (Test-Network) {
$toSkip = $null
}
#Generate App List to update
$Script:AppListToUpdate = @()
#If White List
if ($UseWhiteList) {
#For each app, notify and update
foreach ($app in $outdated) {
if (($toUpdate -contains $app.Id) -and $($app.Version) -ne "Unknown") {
Update-App $app
$AppListToUpdate += $app
}
#if current app version is unknown
elseif ($($app.Version) -eq "Unknown") {
@ -291,7 +270,7 @@ if (Test-Network) {
#For each app, notify and update
foreach ($app in $outdated) {
if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") {
Update-App $app
$AppListToUpdate += $app
}
#if current app version is unknown
elseif ($($app.Version) -eq "Unknown") {
@ -304,6 +283,19 @@ if (Test-Network) {
}
}
#Ask user to approve, if configured
if ($WAUConfig.WAU_UserApproval -eq 1){
Write-ToLog "User Approval feature enabled."
if ($AppListToUpdate){
Invoke-UserApproval $AppListToUpdate
}
}
#Update apps
foreach ($App in $AppListToUpdate){
Update-App $App
}
if ($InstallOK -gt 0) {
Write-ToLog "$InstallOK apps updated ! No more update." "Green"
}

View File

@ -1,58 +1,41 @@
#Function to ask user consent before updating apps
function Invoke-UserApproval ($outdated){
function Invoke-UserApproval ($Apps){
#Create / Update WAU Class for notification action
if ($IsSystem) {
$WAUClass = "HKLM:\Software\Classes\WAU"
$WAUClassCmd = "$WAUClass\shell\open\command"
$WAUClassRun = "Wscript.exe ""$WorkingDir\Invisible.vbs"" ""powershell.exe -NoProfile -ExecutionPolicy Bypass -Command & '$WorkingDir\User-Run.ps1' -NotifApproved %1"""
New-Item $WAUClassCmd -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name 'URL Protocol' -Value '' -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name '(default)' -Value "URL:WAU" -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClass -Name 'EditFlags' -Value '2162688' -PropertyType DWord -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -LiteralPath $WAUClassCmd -Name '(default)' -Value $WAUClassRun -PropertyType String -Force -ErrorAction SilentlyContinue | Out-Null
$Button1Action = "wau:system"
$OnClickAction = "wau:systemDialogBox"
}
else{
$Button1Action = "wau:user"
$OnClickAction = "wau:userDialogBox"
}
$WAUNotifApproved = "$WorkingDir/Config/NotifApproved.txt"
$WAUNotifAppList = "$WorkingDir/Config/NotifAppList.csv"
$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)"
}
}
#Check for approved file
if (Test-Path $WAUNotifApproved) {
Write-ToLog "-> User approved update notification."
$AppListToUpdate = Import-Csv $WAUNotifAppList
Remove-Item $WAUNotifApproved -Force -Confirm:$false
Remove-Item $WAUNotifAppList -Force -Confirm:$false
}
#If Black List or default
#Otherwise generate AppList and send notif
else {
$toSkip = Get-ExcludedApps
foreach ($app in $Outdated) {
if (-not ($toSkip -contains $app.Id) -and $($app.Version) -ne "Unknown") {
$OutdatedApps += "- $($app.Name)"
}
}
}
Write-ToLog "-> Creating AppList user must approve"
$Apps | Export-Csv -Path $WAUNotifAppList -NoTypeInformation -Encoding UTF8
if ($IsSystem) {
$Button1Action = "wau:system"
$OnClickAction = "wau:systemDialogBox"
}
else{
$Button1Action = "wau:user"
$OnClickAction = "wau:userDialogBox"
}
$body = $OutdatedApps | Out-String
if ($body) {
#Ask user to update apps
$body = $Apps.Name | Out-String
$Message = "Do you want to update these apps ?"
$body += "`nPlease save your work and close theses apps"
$WAUNotifContent = "$WorkingDir\config\NotifContent.txt"
New-Item -Path $WAUNotifContent -ItemType File -Force | Out-Null
Set-Content -Path $WAUNotifContent -Value $body
Start-NotifTask -Title "New available updates" -Message $Message -Body $body -ButtonDismiss -Button1Text "Yes" -Button1Action $Button1Action -OnClickAction $OnClickAction -MessageType "info"
Return 0
}
else {
Return 1
Write-ToLog "-> User approval requested. Waiting for user to approve available updates... Closing for now."
#Closing job, waiting for user approval
Exit 0
}
}