diff --git a/windows-installer/auto-update.ps1 b/windows-installer/auto-update.ps1 index e1a54b8e1..ac1c99bcb 100644 --- a/windows-installer/auto-update.ps1 +++ b/windows-installer/auto-update.ps1 @@ -1,25 +1,21 @@ #Requires -RunAsAdministrator [CmdletBinding()] -param( - [Parameter(Mandatory=$true, ValueFromPipeline=$true)] - [string] - $InstallDir -) +param() begin {} process { Start-Transcript -Path "C:\Certbot\log\auto-update.log" - trap { - Stop-Transcript - } + trap { Stop-Transcript } $ErrorActionPreference = 'Stop' + $installDir = $PSScriptRoot $installerAuthenticodeCertificateThumbprint = "CHANGEME" # Get current local certbot version try { $currentVersion = certbot --version $currentVersion = $currentVersion -replace '^certbot (\d+\.\d+\.\d+).*$', '$1' + $currentVersion = [System.Version]"$currentVersion" } catch { "An error occured while fetching the current local certbot version:" $_.Exception @@ -31,13 +27,14 @@ process { try { $result = Invoke-RestMethod -Uri https://api.github.com/repos/certbot/certbot/releases/latest $latestVersion = $result.tag_name -replace '^v(\d+\.\d+\.\d+).*$', '$1' + $latestVersion = [System.Version]"$latestVersion" } catch { "Could not get the latest remote certbot version. Error was:" $_.Exception throw "Aborting auto-upgrade process." } - if ([System.Version]"$currentVersion" -ge [System.Version]"$latestVersion") { + if ($currentVersion -ge $latestVersion) { "No upgrade is needed, Certbot is already at the latest version ($currentVersion)." } else { # Search for the Windows installer asset @@ -73,13 +70,15 @@ process { # } # Install new version of Certbot - "Running the installer ..." - Start-Process -FilePath $installerPath -ArgumentList "/S /D=$InstallDir" + "Running the installer (installation directory: $installDir) ..." + Start-Process -FilePath $installerPath -ArgumentList "/S /D=$installDir" "Certbot $latestVersion is installed." } finally { Remove-Item $installerPath -ErrorAction 'Ignore' } } + + Stop-Transcript } end {} diff --git a/windows-installer/tasks-down.ps1 b/windows-installer/tasks-down.ps1 index 158abeaba..76144a853 100644 --- a/windows-installer/tasks-down.ps1 +++ b/windows-installer/tasks-down.ps1 @@ -1,8 +1,15 @@ -$tasks = "Certbot Renew & Auto-Update Task", "Certbot Renew Task" +#Requires -RunAsAdministrator +[CmdletBinding()] +param() +begin {} +process { + $tasks = "Certbot Renew & Auto-Update Task", "Certbot Renew Task" -foreach ($task in $tasks) { - $exists = Get-ScheduledTask | Where-Object {$_.TaskName -like $task} - if ($exists) { - Unregister-ScheduledTask -TaskName $task -Confirm:$false + foreach ($task in $tasks) { + $exists = Get-ScheduledTask | Where-Object { $_.TaskName -like $task } + if ($exists) + { + Unregister-ScheduledTask -TaskName $task -Confirm:$false + } } } diff --git a/windows-installer/tasks-up.ps1 b/windows-installer/tasks-up.ps1 index 8066219ef..1a36cb0b2 100644 --- a/windows-installer/tasks-up.ps1 +++ b/windows-installer/tasks-up.ps1 @@ -1,24 +1,29 @@ -Param( - [Parameter(Mandatory=$true)] - [string]$InstallDir +#Requires -RunAsAdministrator +[CmdletBinding()] +param( + [Parameter(Mandatory=$true, ValueFromPipeline=$true)] + [string] + $InstallDir ) +begin {} +process { + function Get-ScriptDirectory { Split-Path $MyInvocation.ScriptName } + $down = Join-Path (Get-ScriptDirectory) 'tasks-down.ps1' + & $down -Function Get-ScriptDirectory { Split-Path $MyInvocation.ScriptName } -$down = Join-Path (Get-ScriptDirectory) 'tasks-down.ps1' -& $down + $taskName = "Certbot Renew & Auto-Update Task" -$taskName = "Certbot Renew & Auto-Update Task" + $actionRenew = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -Command "certbot renew"' + $actionUpgrade = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-NoProfile -WindowStyle Hidden -File ""$InstallDir\auto-update.ps1""" -$actionRenew = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -Command "certbot renew"' -$actionUpgrade = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-NoProfile -WindowStyle Hidden -File ""%TMP%\auto-update.ps1"" -InstallDir ""$InstallDir""" - -$delay = New-TimeSpan -Hours 12 -$triggerAM = New-ScheduledTaskTrigger -Daily -At 12am -RandomDelay $delay -$triggerPM = New-ScheduledTaskTrigger -Daily -At 12pm -RandomDelay $delay -# NB: For now scheduled task is set up under Administrators account because Certbot Installer installs Certbot for all users. -# If in the future we allow the Installer to install Certbot for one specific user, the scheduled task will need to -# switch to this user, since Certbot will be available only for him. -$adminSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544") -$adminGroupID = $adminSID.Translate([System.Security.Principal.NTAccount]).Value -$principal = New-ScheduledTaskPrincipal -GroupId $adminGroupID -RunLevel Highest -Register-ScheduledTask -Action $actionRenew,$actionUpgrade -Trigger $triggerAM,$triggerPM -TaskName $taskName -Description "Execute twice a day the 'certbot renew' command, to renew managed certificates if needed." -Principal $principal + $delay = New-TimeSpan -Hours 12 + $triggerAM = New-ScheduledTaskTrigger -Daily -At 12am -RandomDelay $delay + $triggerPM = New-ScheduledTaskTrigger -Daily -At 12pm -RandomDelay $delay + # NB: For now scheduled task is set up under Administrators account because Certbot Installer installs Certbot for all users. + # If in the future we allow the Installer to install Certbot for one specific user, the scheduled task will need to + # switch to this user, since Certbot will be available only for him. + $adminSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544") + $adminGroupID = $adminSID.Translate([System.Security.Principal.NTAccount]).Value + $principal = New-ScheduledTaskPrincipal -GroupId $adminGroupID -RunLevel Highest + Register-ScheduledTask -Action $actionRenew, $actionUpgrade -Trigger $triggerAM, $triggerPM -TaskName $taskName -Description "Execute twice a day the 'certbot renew' command, to renew managed certificates if needed." -Principal $principal +} diff --git a/windows-installer/template.nsi b/windows-installer/template.nsi index fa7c3a335..024346f71 100644 --- a/windows-installer/template.nsi +++ b/windows-installer/template.nsi @@ -180,8 +180,11 @@ SectionEnd Section "Uninstall" ; CERTBOT CUSTOM BEGIN - ; Execute ps script to remove the certbot renew & auto-update task + ; Execute ps script to remove the certbot renew & auto-update task, then delete scripts nsExec::ExecToStack 'powershell -inputformat none -ExecutionPolicy RemoteSigned -File "$INSTDIR\tasks-down.ps1"' + Delete "$INSTDIR\tasks-down.ps1" + Delete "$INSTDIR\tasks-up.ps1" + Delete "$INSTDIR\auto-update.ps1" ; CERTBOT CUSTOM END SetRegView [[ib.py_bitness]]