Le client Nextcloud pour Windows possède un système de mise à jour automatique, mais le problème c’est que l’utilisateur doit avoir les droits d’installations ce qui n’est pas toujours le cas. Pour contourner le problème, on va utiliser un script PowerShell qui se chargera de l’installation en tant que compte “Système”.
Descriptif
- On récupère le dernier package msi directement du site de Nextcloud : https://download.nextcloud.com/desktop/releases/Windows/latest
- On utilise le msi pour personnaliser l’installation : https://docs.nextcloud.com/desktop/latest/installing.html#customizing-the-windows-installation
- On fait appel au module RunAsUser : https://github.com/KelvinTegelaar/RunAsUser
- On procède au téléchargement et au traitement uniquement s’il y a une différence entre la version en ligne et la version installée
Script
# VARIABLES
$Site = "https://download.nextcloud.com/desktop/releases/Windows/"
$Uri = "https://download.nextcloud.com/desktop/releases/Windows/latest"
$FilePath = "C:\Nextcloud-Update\Nextcloud.msi"
$LogPath = "C:\Nextcloud-Update\Nextcloud-Update-Log.txt"
$MSIArguments = @(
"/i"
"$FilePath"
"SKIPAUTOUPDATE=1"
"REBOOT=ReallySuppress"
"/qn"
)
$StopNextCloud = {Stop-Process -Name "nextcloud" -Force}
$StartNextCloud = {Start-Process -FilePath "nextcloud.exe" -WorkingDirectory "$env:ProgramFiles\Nextcloud"}
$Notification = {
Add-Type -AssemblyName System.Windows.Forms
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Remove-Event BalloonClosed_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$SysTrayIconPath = "C:\Program Files\Nextcloud\nextcloud.exe"
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$balloon.BalloonTipTitle = "NextCloud"
$balloon.BalloonTipText = "Mise à jour en cours de l'application."
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
}
# SCRIPT
Clear-Host
Remove-Item $LogPath -Verbose -ErrorAction Ignore
Start-Transcript -Path $LogPath
if ( -not (Get-InstalledModule "RunAsUser" -ErrorAction SilentlyContinue)) {
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Install-Module -Name RunAsUser -Force -Scope CurrentUser
}
if (Test-Connection -ComputerName $Site.Split("/")[2] -Count 4 -quiet) {
Write-Host "Serveur distant: ACCESSIBLE"
$HttpContent = Invoke-WebRequest -Uri $Site -UseBasicParsing
$FileLastVersion = $HttpContent.Links | Where-Object {$_.href -like "*.msi"} | Select-Object -Last 1 -ExpandProperty href
$OnlineVersion = $FileLastVersion.Split("-")[1]
Write-Host "Online version: $OnlineVersion"
$InstalledVersionBuild = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | Get-ItemProperty | Where-Object {$_.DisplayName -match 'Nextcloud' } | Select-Object -ExpandProperty DisplayVersion
$InstalledVersion = $InstalledVersionBuild.Substring(0, $InstalledVersionBuild.LastIndexOf("."))
Write-Host "Local version: $InstalledVersion"
if ($OnlineVersion -ne $InstalledVersion) {
Write-Host "Nouvelle version détectée. Téléchargement en cours..."
try {
Invoke-WebRequest -Uri $Uri -OutFile $FilePath -UseBasicParsing
Write-Host "Fichier téléchargé. Lancement de la maj de Nextcloud."
Invoke-AsCurrentUser -Scriptblock $Notification
Invoke-AsCurrentUser -Scriptblock $StopNextCloud
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow
Write-Host "Installation terminée."
Invoke-AsCurrentUser -Scriptblock $StartNextCloud
Remove-Item $FilePath -Verbose -ErrorAction Ignore
} catch {
$StatusCode = $_.Exception.Response.StatusCode.value__
Write-Host "Erreur: $StatusCode"
}
} else {
Write-Host "Version identique. Pas de traitement."
}
} else {
Write-Host "Serveur distant INACCESSIBLE. Pas de traitement."
}
Stop-Transcript
Exécution
Il ne reste plus qu’à créer une tâche planifiée sur le poste pour procéder à l’exécution du script de manière automatique et quotidienne :
$TaskName = "Nextcloud-Update" $User= "NT AUTHORITY\SYSTEM" $Trigger= New-ScheduledTaskTrigger -Daily -At 12:30 $Action= New-ScheduledTaskAction -Execute "powershell" -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\AdminTools\Nextcloud-Update\Nextcloud-Update.ps1"' $Settings= New-ScheduledTaskSettingsSet -Compatibility Win8 -WakeToRun -RestartInterval "00:10:00" -restartCount 3 -ExecutionTimeLimit "01:00:00" -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $User -Action $Action -Settings $Settings -RunLevel Highest -Force
