Various corrections

This commit is contained in:
Adrien Ferrand 2019-11-19 21:26:56 +01:00
parent a896d16625
commit 56ad2a3346
4 changed files with 51 additions and 37 deletions

View file

@ -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 {}

View file

@ -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
}
}
}

View file

@ -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
}

View file

@ -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]]